Advertisement
egisss633

CudaB

Jan 16th, 2018
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.36 KB | None | 0 0
  1.  
  2. #include "cuda_runtime.h"
  3. #include "device_launch_parameters.h"
  4. #include <cuda.h>
  5.  
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <iostream>
  9. #include <string>
  10. #include <fstream>
  11. #include <iomanip>
  12. #include <thrust/host_vector.h>
  13. #include <thrust/device_vector.h>
  14.  
  15. using namespace std;
  16.  
  17. //Konstantos
  18. const char DataFile[] = "input.txt";
  19. const int MAX_THREADS = 5;
  20. const int MAX_ARRAY_SIZE = 5;
  21.  
  22. //Struktura gamintojo modeliams saugoti
  23. struct model {
  24. char name[75];
  25. int quantity;
  26. double price;
  27. };
  28.  
  29. //Konteinerine struktura saugoti modeliams
  30. struct manufacturer {
  31. char name[15];
  32. int quantity;
  33. model models[MAX_ARRAY_SIZE];
  34. };
  35.  
  36. //Funkciju prototipai
  37. void ReadFile(string filename, thrust::host_vector<manufacturer> &AllModels);
  38. void PrintTable(thrust::host_vector<manufacturer> printOut);
  39. void PrintResults(thrust::host_vector<model> printOut);
  40. void Plus(thrust::device_vector<manufacturer> &manu, thrust::device_vector<model> &resultsArray);
  41.  
  42.  
  43. int main() {
  44. thrust::host_vector<manufacturer> AllModels(MAX_THREADS);
  45. thrust::host_vector<model> results(MAX_ARRAY_SIZE);
  46.  
  47. ReadFile(DataFile, AllModels);
  48. PrintTable(AllModels);
  49. //Nusinuliname rezultatu masyvo elementus
  50. for (int i = 0; i < MAX_ARRAY_SIZE; i++){
  51. model data;
  52. strcpy(data.name, "");
  53. data.price = 0.0;
  54. data.quantity = 0;
  55. results[i] = data;
  56. }
  57.  
  58. //Perkeliame duomenis is RAM i VRAM
  59. thrust::device_vector<manufacturer> manu = AllModels;
  60. thrust::device_vector<model> resultsArray = results;
  61.  
  62. //Iskvieciame sumuojancia funkcija
  63. Plus(manu, resultsArray);
  64. cudaDeviceSynchronize();
  65.  
  66. //Persikeliame rezultatus is VRAM(GPU) i RAM(CPU)
  67. results = resultsArray;
  68.  
  69. //Atspausdiname rezultatus
  70. PrintResults(results);
  71. system("Pause");
  72. return 0;
  73. }
  74.  
  75. /*
  76. ============================================================================
  77. ReadFile
  78. Pradiniu duomenu nuskaitymo funkcija, per nuoroda grazina manufacturer
  79. strukturos host vektoriu (CPU atmintyje)
  80. ============================================================================
  81. */
  82. void ReadFile(string filename, thrust::host_vector<manufacturer> &AllModels) {
  83. string title;
  84. int count, j;
  85. j = 0;
  86. ifstream fin(filename);
  87. if (!fin) {
  88. cerr << "Couldn't open file!\n";
  89. }
  90. else {
  91. while (!fin.eof()){
  92. fin >> title >> count;
  93. strcpy(AllModels[j].name, title.c_str());
  94. //AllModels[j].name = title;
  95. AllModels[j].quantity = count;
  96. model models[MAX_ARRAY_SIZE];
  97. for (int i = 0; i < count; i++){
  98. model modelis;
  99. fin >> modelis.name >> modelis.quantity >> modelis.price;
  100. AllModels[j].models[i] = modelis;
  101. }
  102. j++;
  103. }
  104. fin.close();
  105. }
  106. }
  107.  
  108. /*
  109. ============================================================================
  110. PrintTable
  111. Atspausdina pradinius duomenis lentelemis
  112. ============================================================================
  113. */
  114. void PrintTable(thrust::host_vector<manufacturer> printOut){
  115. cout << "-----------------------------------------------------------------------------\n";
  116. for (manufacturer & manu : printOut){
  117. cout << right << setw(35) << manu.name << "\n";
  118. cout << "-----------------------------------------------------------------------------\n";
  119. cout << left << setw(63) << "Modelio Pavadinimas"
  120. << setw(8) << "Kiekis"
  121. << setw(5) << "Kaina" << "\n";
  122. cout << "-----------------------------------------------------------------------------\n";
  123. for (int i = 0; i < manu.quantity; i++){
  124. model forPrinting = manu.models[i];
  125. cout << left << setw(3) << to_string(i + 1) + ")"
  126. << setw(60) << forPrinting.name
  127. << setw(8) << forPrinting.quantity
  128. << setw(5) << setprecision(4) << forPrinting.price
  129. << "\n";
  130. }
  131. cout << "-----------------------------------------------------------------------------\n";
  132. }
  133. }
  134.  
  135. /*
  136. ============================================================================
  137. PrintResults
  138. Atspausdina rezultatu vektoriu lenteleje
  139. ============================================================================
  140. */
  141. void PrintResults(thrust::host_vector<model> printOut) {
  142. cout << "-----------------------------------------------------------------------------\n";
  143. cout << left << setw(63) << "Modelio Pavadinimas"
  144. << setw(8) << "Kiekis"
  145. << setw(5) << "Kaina" << "\n";
  146. cout << "-----------------------------------------------------------------------------\n";
  147. for (int i = 0; i < MAX_ARRAY_SIZE; i++) {
  148. model forPrinting = printOut[i];
  149. cout << left << setw(3) << to_string(i + 1) + ")"
  150. << setw(60) << forPrinting.name
  151. << setw(8) << forPrinting.quantity
  152. << setw(5) << setprecision(4) << forPrinting.price
  153. << "\n";
  154. }
  155. }
  156.  
  157. /*
  158. ============================================================================
  159. Plus
  160. Susumuoja pradiniu duomenu masyvus i viena masyva ir grazina rezultata per
  161. nuoroda. Naudojami device vektoriai, reiskias naudojama atmintis priklauso
  162. GPU.
  163. ============================================================================
  164. */
  165. void Plus(thrust::device_vector<manufacturer> &manu, thrust::device_vector<model> &resultsArray) {
  166. for (int i = 0; i < manu.size(); i++) {
  167. for (int j = 0; j < resultsArray.size(); j++) {
  168. model data = resultsArray[i];
  169. manufacturer addData = manu[j];
  170. strcat(data.name, addData.models[i].name);
  171. data.price += addData.models[i].price;
  172. data.quantity += addData.models[i].quantity;
  173. resultsArray[i] = data;
  174. }
  175. }
  176. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement