Advertisement
tomas862

stackoverflow thrust vektoriai paprastesnis be tranform

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