Advertisement
Guest User

Untitled

a guest
Nov 20th, 2019
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.99 KB | None | 0 0
  1. #include <mutex>
  2. #include <condition_variable>
  3. #include <thread>
  4. #include <iostream>
  5. #include <fstream>
  6. #include <queue>
  7. #include <tuple>
  8. #include <chrono>
  9. #include <string>
  10.  
  11.  
  12. using namespace std;
  13. using namespace std::chrono;
  14.  
  15. const int maxn = 1005;
  16. const string data[] = {"mult0.in", "mult1.in", "mult2.in", "mult3.in", "mult4.in", "mult5.in", "mult6.in"};
  17.  
  18. mutex mx;
  19. condition_variable cv;
  20. queue <tuple<int, int, int>> q;
  21. const int T[] = {1, 3, 5, 10, 25, 50, 100};
  22. const int L[] = {3, 5, 10, 25, 50, 100};
  23. int noElemLine, a[maxn][maxn], b[maxn][maxn], c[maxn][maxn], rez[maxn][maxn];
  24. bool finished;
  25.  
  26. string fullPath = "/Users/claudiuhiticas/Desktop/University/PPD/Lab 4/Lab 4/";
  27.  
  28. void loadData(string name) {
  29. ifstream fin(fullPath + name);
  30. fin >> noElemLine;
  31. cout << noElemLine << endl;
  32. // cout << endl;
  33.  
  34. for(int i = 0; i < noElemLine; ++ i)
  35. for(int j = 0; j < noElemLine; ++ j)
  36. fin >> a[i][j];
  37.  
  38.  
  39. for(int i = 0; i < noElemLine; ++ i)
  40. for(int j = 0; j < noElemLine; ++ j)
  41. fin >> b[i][j];
  42.  
  43.  
  44. for(int i = 0; i < noElemLine; ++ i)
  45. for(int j = 0; j < noElemLine; ++ j)
  46. fin >> c[i][j];
  47.  
  48. }
  49.  
  50.  
  51. inline void producer(int line, int T)
  52. {
  53. for(int i = line; i < noElemLine; i+=T)
  54. {
  55. for(int j = 0; j < noElemLine; ++j)
  56. {
  57. int aux_i_j = 0;
  58. for(int k = 0; k < noElemLine; ++k)
  59. aux_i_j += a[i][k] * b[k][j];
  60.  
  61. {
  62. lock_guard<mutex> lk(mx);
  63. //cout << "producing (" << i << ", " << j << ", " << aux_i_j << ")\n";
  64. q.push(make_tuple(i, j, aux_i_j));
  65.  
  66. }
  67. cv.notify_all();
  68. }
  69. }
  70. }
  71.  
  72. inline void consumer(int tid, int T)
  73. {
  74. while(true)
  75. {
  76. unique_lock<mutex> lk(mx);
  77. cv.wait(lk, []{return finished || !q.empty();});
  78. if(finished)
  79. break;
  80. tuple<int, int, int> el = q.front();
  81. q.pop();
  82. int i = get<0>(el);
  83. int j = get<1>(el);
  84. int x = get<2>(el);
  85. //cout << tid << " consuming (" << i << ", " << j << ", " << x << ")";
  86. for(int k = 0; k < noElemLine; ++k)
  87. rez[i][k] += x * c[j][k];
  88.  
  89. }
  90. }
  91.  
  92. int main()
  93. {
  94. cout << "size, number_of_threads, duration\n";
  95. for(int di = 0; di < 7; ++di)
  96. {
  97.  
  98. loadData(data[di]);
  99. noElemLine = L[di];
  100. for(int tj = 0; tj < 7; ++tj)
  101. {
  102. //cout << di << " " << noElemLine << " " << T[tj] << "\n";
  103. int noTh = T[tj];
  104.  
  105. auto start = chrono::high_resolution_clock::now();
  106.  
  107. vector<thread> producers, consumers;
  108.  
  109. for(int i = 0; i < min(noElemLine, noTh); ++ i)
  110. {
  111. producers.push_back(thread(producer, i, noTh));
  112. }
  113.  
  114. for(int i = 0; i < min(noElemLine, noTh); ++ i)
  115. {
  116. consumers.push_back(thread(consumer, i, noTh));
  117. }
  118.  
  119. for(int i = 0; i < producers.size(); ++ i)
  120. {
  121. producers[i].join();
  122. }
  123. {
  124. lock_guard<mutex> lk(mx);
  125. finished = true;
  126. }
  127. cv.notify_all();
  128. for(int i = 0; i < consumers.size(); ++ i)
  129. {
  130. consumers[i].join();
  131. }
  132.  
  133. cout << '\n';
  134. for(int i = 0; i < noElemLine; ++ i)
  135. {
  136. for(int j = 0; j < noElemLine; ++ j)
  137. {
  138. cout << rez[i][j] << ' ';
  139. }
  140. cout << '\n';
  141. }
  142. auto finish = chrono::high_resolution_clock::now();
  143. chrono::duration<double> elapsed = finish - start;
  144.  
  145. cout << noElemLine << ", " << noTh << ", " << elapsed.count() << "\n";
  146.  
  147. }
  148.  
  149. }
  150. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement