Advertisement
Guest User

Untitled

a guest
May 20th, 2019
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.81 KB | None | 0 0
  1. #include<iostream>
  2. #include<thread>
  3. #include<cstdlib>
  4. #include<ctime>
  5. #include <chrono>
  6. using namespace std;
  7.  
  8. static const int MATRIX_SIZE = 1000;
  9.  
  10. typedef chrono::time_point<chrono::steady_clock> my_clock;
  11.  
  12. my_clock startExecution(string title) {
  13. my_clock start = chrono::high_resolution_clock::now();
  14. printf("Start: %s\n", title.c_str());
  15. return start;
  16. }
  17.  
  18. void stopExecution(string title, my_clock start) {
  19. my_clock end = chrono::high_resolution_clock::now();
  20. chrono::duration<double, std::milli> diff = end - start;
  21. printf("Stop: %s. Total: %f ms\n", title.c_str(), diff.count());
  22. }
  23.  
  24. double MatrixMult(double**A, double**B, double**C)
  25. {
  26. double checkSum = 0;
  27. for (int i = 0; i < MATRIX_SIZE; i++)
  28. {
  29. for (int j = 0; j < MATRIX_SIZE; j++)
  30. {
  31. for (int k = 0; k < MATRIX_SIZE; k++)
  32. {
  33. C[i][j] += A[i][k] * B[k][j];
  34. }
  35. }
  36. }
  37. cout << "kontrolnaya summa" << endl;
  38. for (int i = 0; i < MATRIX_SIZE; i++)
  39. {
  40. checkSum += C[i][i];
  41. }
  42. cout << checkSum << endl;
  43. return checkSum;
  44. }
  45.  
  46. void threadMain(double** A, double**B, double** C, int firstIndex, int lastIndex)
  47. {
  48.  
  49. double checkSum = 0;
  50. for (int index = firstIndex; index < lastIndex; ++index) {
  51. double sum = 0.0;
  52. for (int i = 0; i < MATRIX_SIZE; ++i) {
  53. int fRow = index / MATRIX_SIZE;
  54. int sCol = index % MATRIX_SIZE;
  55. sum += A[fRow][i] * B[i][sCol];
  56. }
  57. int rRow = index / MATRIX_SIZE;
  58. int rCol = index % MATRIX_SIZE;
  59. C[rRow][rCol] = sum;
  60. }
  61.  
  62. }
  63.  
  64.  
  65. void MatrixMultMT(double**A, double**B, double**C, int threadCount)
  66. {
  67. int cellsForThread = (MATRIX_SIZE * MATRIX_SIZE) / threadCount;
  68. int firstIndex = 0;
  69. double checkSum = 0;
  70. thread* threadList = new thread[threadCount];
  71.  
  72. for (int threadIndex = threadCount - 1; threadIndex >= 0; --threadIndex) {
  73. int lastIndex = firstIndex + cellsForThread;
  74. if (threadIndex == 0) {
  75.  
  76. lastIndex = MATRIX_SIZE * MATRIX_SIZE;
  77. }
  78. threadList[threadIndex] = thread(threadMain, ref(A), ref(B), ref(C), firstIndex, lastIndex);
  79. firstIndex = lastIndex;
  80.  
  81. }
  82.  
  83.  
  84.  
  85. for (int i = 0; i < threadCount; i++) {
  86. threadList[i].join();
  87. }
  88. cout << "kontrolnaya summa" << endl;
  89. for (int i = 0; i < MATRIX_SIZE; i++)
  90. {
  91. checkSum += C[i][i];
  92. }
  93. cout << checkSum << endl;
  94.  
  95.  
  96.  
  97.  
  98. }
  99.  
  100. int main()
  101. {
  102. srand(time(NULL));
  103. int threadsCount;
  104. cout << "vvedite kol-vo potokov" << endl;
  105. cin >> threadsCount;
  106. double**A, **B, **C;
  107. A = new double*[MATRIX_SIZE];
  108. for (int i = 0; i < MATRIX_SIZE; ++i) {
  109. A[i] = new double[MATRIX_SIZE];
  110. for (int j = 0; j < MATRIX_SIZE; ++j) {
  111. A[i][j] = (rand() % 901 + 100) / 100.0;
  112. }
  113. }
  114. B = new double*[MATRIX_SIZE];
  115. for (int i = 0; i < MATRIX_SIZE; ++i) {
  116. B[i] = new double[MATRIX_SIZE];
  117. for (int j = 0; j < MATRIX_SIZE; ++j) {
  118. B[i][j] = (rand() % 901 + 100) / 100.0;
  119. }
  120. }
  121. C = new double*[MATRIX_SIZE];
  122. for (int i = 0; i < MATRIX_SIZE; ++i) {
  123. C[i] = new double[MATRIX_SIZE];
  124. for (int j = 0; j < MATRIX_SIZE; ++j) {
  125. C[i][j] = 0.0;
  126. }
  127. }
  128. /*cout << "first matrix"<<endl;
  129. for (int i = 0; i < MATRIX_SIZE; i++)
  130. {
  131. for (int j = 0; j < MATRIX_SIZE; j++)
  132. {
  133. cout << A[i][j] << " ";
  134. }
  135. cout << endl;
  136. }
  137. cout <<"second matrix"<< endl;
  138. for (int i = 0; i < MATRIX_SIZE; i++)
  139. {
  140. for (int j = 0; j < MATRIX_SIZE; j++)
  141. {
  142. cout << B[i][j] << " ";
  143. }
  144. cout << endl;
  145. }*/
  146.  
  147. my_clock timer;
  148. timer = startExecution("single-thread");
  149. stopExecution("single-thread", timer);
  150.  
  151.  
  152. MatrixMult(A, B, C);
  153.  
  154. timer = startExecution("multi-threads");
  155.  
  156. MatrixMultMT(A, B, C, threadsCount);
  157. stopExecution("multi-thread", timer);
  158.  
  159.  
  160. for (int i = 0; i < MATRIX_SIZE; i++) {
  161. delete[] A[i];
  162. }
  163. delete[] A;
  164. for (int i = 0; i < MATRIX_SIZE; i++) {
  165. delete[] B[i];
  166. }
  167. delete[] B;
  168. for (int i = 0; i < MATRIX_SIZE; i++) {
  169. delete[] C[i];
  170. }
  171. delete[] C;
  172. system("pause");
  173. return 0;
  174. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement