Advertisement
Guest User

Untitled

a guest
Mar 18th, 2019
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.96 KB | None | 0 0
  1. #include<iostream>
  2. #include<thread>
  3. #include<cstdlib>
  4. #include<ctime>
  5. using namespace std;
  6. static const int MATRIX_SIZE = 100;
  7. static const int THREADS_NUMBER = 4;
  8.  
  9. struct Matrix
  10. {
  11. int** elements;
  12. void obnulenie_mtr() {
  13. elements = new int*[MATRIX_SIZE];
  14. for (int i = 0; i < MATRIX_SIZE; ++i) {
  15. elements[i] = new int[MATRIX_SIZE];
  16. for (int j = 0; j < MATRIX_SIZE; ++j) {
  17. elements[i][j] = 0.;
  18. }
  19. }
  20. for (int i = 0; i < MATRIX_SIZE; i++)
  21. {
  22. delete[] elements[i];
  23. }
  24. delete[] elements;
  25. }
  26. void zapolnenie_mtr()
  27. {
  28. srand(static_cast<int>(time(0)));
  29. elements = new int*[MATRIX_SIZE];
  30. for (int i = 0; i < MATRIX_SIZE; ++i) {
  31. elements[i] = new int[MATRIX_SIZE];
  32. for (int j = 0; j < MATRIX_SIZE; ++j) {
  33. elements[i][j] = rand() % 10;
  34. }
  35. }
  36. for (int i = 0; i < MATRIX_SIZE; i++)
  37. {
  38. delete[] elements[i];
  39. }
  40. delete[] elements;
  41. }
  42. void show()
  43. {
  44. for (int i = 0; i < MATRIX_SIZE; i++)
  45. {
  46. for (int j = 0; j < MATRIX_SIZE; j++)
  47. {
  48. cout << elements[i][j] << " ";
  49. }
  50. cout << endl;
  51. }
  52. }
  53. };
  54. void mult(const Matrix& A, const Matrix& B, Matrix& C)
  55. {
  56. for (int i = 0; i < MATRIX_SIZE; ++i) {
  57. for (int j = 0; j < MATRIX_SIZE; ++j) {
  58. int result = 0;
  59. for (int k = 0; k < MATRIX_SIZE; ++k) {
  60. const int e1 = A.elements[i][k];
  61. const int e2 = B.elements[k][j];
  62. result += e1 * e2;
  63. }
  64. C.elements[i][j] = result;
  65. }
  66. }
  67. }
  68. void multiply_threading(const Matrix& A, const Matrix& B, Matrix& C, const int thread_number) {
  69.  
  70. const int n_elements = (MATRIX_SIZE * MATRIX_SIZE);
  71. const int n_operations = n_elements / THREADS_NUMBER;
  72. const int rest_operations = n_elements % THREADS_NUMBER;
  73.  
  74. int start_op, end_op;
  75.  
  76. if (thread_number == 0) {
  77.  
  78. start_op = n_operations * thread_number;
  79. end_op = (n_operations * (thread_number + 1)) + rest_operations;
  80. }
  81. else {
  82. start_op = n_operations * thread_number + rest_operations;
  83. end_op = (n_operations * (thread_number + 1)) + rest_operations;
  84. }
  85.  
  86. for (int op = start_op; op < end_op; ++op) {
  87. const int row = op % MATRIX_SIZE;
  88. const int col = op / MATRIX_SIZE;
  89. int r = 0;
  90. for (int i = 0; i < MATRIX_SIZE; ++i) {
  91. const int e1 = A.elements[row][i];
  92. const int e2 = B.elements[i][col];
  93. r += e1 * e2;
  94. }
  95. C.elements[row][col] = r;
  96. }
  97. }
  98. void multithred_vipolnenie(const Matrix& A, const Matrix& B,Matrix& C) {
  99. std::thread threads[THREADS_NUMBER];
  100. for (int i = 0; i < THREADS_NUMBER; ++i) {
  101. threads[i] = std::thread(multiply_threading, std::ref(A),std::ref(B), std::ref(C),i);
  102. }
  103. for (int i = 0; i < THREADS_NUMBER; ++i) {
  104. threads[i].join();
  105. }
  106. }
  107. void obyavlenie_mtr(void(*execution_function)(const Matrix& A,const Matrix& B,Matrix& C)) {
  108. Matrix A, B, C;
  109. A.zapolnenie_mtr();
  110. B.zapolnenie_mtr();
  111. C.obnulenie_mtr();
  112. mult(A, B, C);
  113. }
  114.  
  115.  
  116.  
  117. int main()
  118. {
  119.  
  120. //obyavlenie_mtr(single_execution);
  121.  
  122. obyavlenie_mtr(multithred_vipolnenie);
  123.  
  124.  
  125.  
  126. system("pause");
  127. return 0;
  128.  
  129. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement