egisss633

CudaA

Jan 16th, 2018
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.19 KB | None | 0 0
  1. #include "cuda_runtime.h"
  2. #include "device_launch_parameters.h"
  3. #include <cuda.h>
  4.  
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <iostream>
  8. #include <string>
  9. #include <fstream>
  10. #include <iomanip>
  11.  
  12. using namespace std;
  13.  
  14. //Konstantos
  15. const char DataFile[] = "input.txt";
  16. const int MAX_THREADS = 5;
  17. const int MAX_ARRAY_SIZE = 5;
  18.  
  19. //Struktura gamintojo modeliams saugoti
  20. struct model {
  21. char name[75];
  22. int quantity;
  23. double price;
  24. };
  25.  
  26. //Konteinerine struktura saugoti modeliams
  27. struct manufacturer {
  28. char name[15];
  29. int quantity;
  30. model models[MAX_ARRAY_SIZE];
  31. };
  32.  
  33. //Funkciju prototipai
  34. void ReadFile(string filename, manufacturer (&AllModels)[MAX_THREADS]);
  35. void PrintTable(manufacturer(&printOut)[MAX_THREADS]);
  36. void PrintResults(model (&printOut)[MAX_ARRAY_SIZE]);
  37.  
  38. __global__ void RunOnGPU(manufacturer *printOut, model *resultsArray);
  39. __device__ char * my_strcpy(char *dest, const char *src);
  40. __device__ char * my_strcat(char *dest, const char *src);
  41.  
  42. int main() {
  43. manufacturer AllModels[MAX_THREADS];
  44. model results[MAX_ARRAY_SIZE];
  45.  
  46. ReadFile(DataFile, AllModels);
  47. PrintTable(AllModels);
  48. //Nusinuliname rezultatu masyvo elementus
  49. for (int i = 0; i < MAX_ARRAY_SIZE; i++){
  50. model data;
  51. strcpy(data.name, "");
  52. data.price = 0.0;
  53. data.quantity = 0;
  54. results[i] = data;
  55. }
  56.  
  57. //Paruosiame GPU, t.y. perkeliame duomenis is RAM i VRAM
  58. manufacturer *manu;
  59. model *resultsArray;
  60.  
  61. int i = MAX_THREADS * sizeof(manufacturer);
  62. int j = MAX_ARRAY_SIZE * sizeof(model);
  63.  
  64. cudaMalloc((void**)&resultsArray , j);
  65. cudaMalloc((void**)&manu, i);
  66.  
  67. cudaMemcpy(resultsArray, results, j, cudaMemcpyHostToDevice);
  68. cudaMemcpy(manu, AllModels, i, cudaMemcpyHostToDevice);
  69.  
  70. //Iskvieciame GPU metoda, sumuojanti masyvu elementus
  71. RunOnGPU << <1, MAX_THREADS >> >(manu, resultsArray);
  72. cudaDeviceSynchronize();
  73.  
  74. //Perkeliame rezultatus is VRAM (GPU) i RAM (CPU) atminti
  75. cudaMemcpy(results, resultsArray, j, cudaMemcpyDeviceToHost);
  76.  
  77. //Atlaisviname VRAM atminti
  78. cudaFree(manu);
  79. cudaFree(resultsArray);
  80.  
  81. //Atspausdiname rezultatus
  82. PrintResults(results);
  83. system("Pause");
  84. return 0;
  85. }
  86.  
  87. /*
  88. ============================================================================
  89. ReadFile
  90. Pradiniu duomenu nuskaitymo funkcija, per nuoroda grazina manufacturer
  91. strukturos masyva
  92. ============================================================================
  93. */
  94. void ReadFile(string filename, manufacturer (&AllModels)[MAX_THREADS]) {
  95. string title;
  96. int count, j;
  97. j = 0;
  98. ifstream fin(filename);
  99. if (!fin) {
  100. cerr << "Couldn't open file!\n";
  101. }
  102. else {
  103. while (!fin.eof()){
  104. fin >> title >> count;
  105. strcpy(AllModels[j].name, title.c_str());
  106. //AllModels[j].name = title;
  107. AllModels[j].quantity = count;
  108. model models[MAX_ARRAY_SIZE];
  109. for (int i = 0; i < count; i++){
  110. model modelis;
  111. fin >> modelis.name >> modelis.quantity >> modelis.price;
  112. AllModels[j].models[i] = modelis;
  113. }
  114. j++;
  115. }
  116. fin.close();
  117. }
  118. }
  119.  
  120. /*
  121. ============================================================================
  122. PrintTable
  123. Atspausdina pradinius duomenis lentelemis
  124. ============================================================================
  125. */
  126. void PrintTable(manufacturer(&printOut)[MAX_THREADS]){
  127. cout << "-----------------------------------------------------------------------------\n";
  128. for (manufacturer & manu : printOut){
  129. cout << right << setw(35) << manu.name << "\n";
  130. cout << "-----------------------------------------------------------------------------\n";
  131. cout << left << setw(63) << "Modelio Pavadinimas"
  132. << setw(8) << "Kiekis"
  133. << setw(5) << "Kaina" << "\n";
  134. cout << "-----------------------------------------------------------------------------\n";
  135. for (int i = 0; i < manu.quantity; i++){
  136. model forPrinting = manu.models[i];
  137. cout << left << setw(3) << to_string(i + 1) + ")"
  138. << setw(60) << forPrinting.name
  139. << setw(8) << forPrinting.quantity
  140. << setw(5) << setprecision(4) << forPrinting.price
  141. << "\n";
  142. }
  143. cout << "-----------------------------------------------------------------------------\n";
  144. }
  145. }
  146.  
  147. /*
  148. ============================================================================
  149. PrintResults
  150. Atspausdina rezultatu masyva lenteleje
  151. ============================================================================
  152. */
  153. void PrintResults(model(&printOut)[MAX_ARRAY_SIZE]) {
  154. cout << "-----------------------------------------------------------------------------\n";
  155. cout << left << setw(63) << "Modelio Pavadinimas"
  156. << setw(8) << "Kiekis"
  157. << setw(5) << "Kaina" << "\n";
  158. cout << "-----------------------------------------------------------------------------\n";
  159. for (int i = 0; i < MAX_ARRAY_SIZE; i++) {
  160. model forPrinting = printOut[i];
  161. cout << left << setw(3) << to_string(i + 1) + ")"
  162. << setw(60) << forPrinting.name
  163. << setw(8) << forPrinting.quantity
  164. << setw(5) << setprecision(4) << forPrinting.price
  165. << "\n";
  166. }
  167. }
  168.  
  169. /*
  170. ============================================================================
  171. RunOnGPU
  172. Susumuoja pradiniu duomenu masyvus i viena masyva. Rezultatas lieka GPU
  173. atmintyje, is kur veliau ji paimsime.
  174. ============================================================================
  175. */
  176. __global__ void RunOnGPU(manufacturer *printOut, model *resultsArray) {
  177. int gija = threadIdx.x;
  178. for (int i = 0; i < MAX_THREADS; i++) {
  179. my_strcat(resultsArray[gija].name, printOut[i].models[gija].name);
  180. resultsArray[gija].price += printOut[i].models[gija].price;
  181. resultsArray[gija].quantity += printOut[i].models[gija].quantity;
  182. }
  183. }
  184.  
  185. /*
  186. ============================================================================
  187. Pagalbines funkcijos, my_strcat skirta apjungti dviems simboliu eilutems,
  188. ji naudoja my_strcpy. Ju reikia tam, nes CUDA nepalaiko string.h
  189. bibliotekos, kurioje yra strcat() funkcija.
  190. ============================================================================
  191. */
  192. __device__ char * my_strcpy(char *dest, const char *src){
  193. int i = 0;
  194. do {
  195. dest[i] = src[i];
  196. } while (src[i++] != 0);
  197. return dest;
  198. }
  199.  
  200. __device__ char * my_strcat(char *dest, const char *src){
  201. int i = 0;
  202. while (dest[i] != 0) i++;
  203. my_strcpy(dest + i, src);
  204. return dest;
  205. }
Advertisement
Add Comment
Please, Sign In to add comment