Advertisement
Guest User

Untitled

a guest
Mar 14th, 2019
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.66 KB | None | 0 0
  1. #include<iostream>
  2. #include<cstdlib>
  3. #include<ctime>
  4. #include<thread>
  5. const int MAX = 4;
  6. const int x = 2;
  7. int step_i = 0;
  8. using namespace std;
  9. void mult(int **A,int **B,int **C)
  10. {
  11. int core = step_i++;
  12. for (int i = core * MAX / 4; i < (core + 1) * MAX / 4; i++) {
  13. for (int j = 0; j < MAX; j++) {
  14. C[i][j] = 0;
  15. for (int k = 0; k < MAX; k++) {
  16. C[i][j] += A[i][k] * B[k][j];
  17. }
  18.  
  19. }
  20.  
  21. }
  22.  
  23. cout << "mtr C" << endl;
  24. for (int i = 0; i < MAX; i++) {
  25. for (int j = 0; j < MAX; j++) {
  26. cout << C[i][j] << " ";
  27.  
  28. }
  29. cout << endl;
  30. }
  31. }
  32.  
  33. int main()
  34. {
  35. srand(static_cast<unsigned int>(time(0)));
  36. int **A, **B, **C;
  37.  
  38.  
  39. A = new int*[MAX];
  40. B = new int*[MAX];
  41. C = new int*[MAX];
  42. for (int i = 0; i < MAX; i++) {
  43. A[i] = new int [MAX];
  44. B [i]= new int [MAX];
  45. C [i]= new int [MAX];
  46. }
  47.  
  48. for (int i = 0; i < MAX; i++) {
  49. for (int j = 0; j < MAX; j++) {
  50. A[i][j] = rand() % 5;
  51. B[i][j] = rand() % 5;
  52. }
  53. }
  54.  
  55. cout << "mtr A" << endl;
  56. for (int i = 0; i < MAX; i++) {
  57. for (int j = 0; j < MAX; j++) {
  58.  
  59. cout << A[i][j] << " ";
  60.  
  61. }
  62. cout << endl;
  63. }
  64.  
  65. cout << "mtr B" << endl;
  66. for (int i = 0; i < MAX; i++) {
  67. for (int j = 0; j < MAX; j++) {
  68. cout << B[i][j] << " ";
  69.  
  70. }
  71. cout << endl;
  72. }
  73.  
  74. std::thread threads[x];
  75. for (int i = 0; i < x; i++) {
  76. threads[i] = std::thread(mult,i);
  77. }
  78. for (int i = 0; i < x; i++) {
  79. threads[i].join();
  80. }
  81. for (int i = 0; i < MAX; i++)
  82. {
  83. delete[] A[i];
  84. }
  85. delete[] A;
  86. for (int i = 0; i < MAX; i++)
  87. {
  88. delete[] B[i];
  89. }
  90. delete[] B;
  91. for (int i = 0; i < MAX; i++)
  92. {
  93. delete[] C[i];
  94. }
  95. delete[] C;
  96.  
  97. system("pause");
  98. return 0;
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement