Advertisement
dark-Matter

vyom_lodu

Feb 27th, 2021
272
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.39 KB | None | 0 0
  1. // CPP Program to multiply two matrix using pthreads
  2. #include <bits/stdc++.h>
  3. using namespace std;
  4.  
  5. // maximum size of matrix
  6. #define MAX 4
  7.  
  8. // maximum number of threads
  9. #define MAX_THREAD 4
  10.  
  11.  
  12. int MX = 100;
  13.  
  14. int **matA, **matB, **matC;
  15.  
  16. int step_i = 0;
  17. int n;
  18.  
  19. void* multi(void* arg)
  20. {
  21.     int core = step_i++;
  22.  
  23.     // Each thread computes 1/4th of matrix multiplication
  24.     for (int i = core * n / 4; i < (core + 1) * n / 4; i++)
  25.         for (int j = 0; j < n; j++)
  26.             for (int k = 0; k < n; k++)
  27.                 matC[i][j] += matA[i][k] * matB[k][j];
  28. }
  29.  
  30.  
  31. // Driver Code
  32. double job(int n)
  33. {
  34.     time_t start, end;
  35.    
  36.     matA = (int**)malloc(n*sizeof(int*));
  37.     matB = (int**)malloc(n*sizeof(int*));
  38.     matC = (int**)malloc(n*sizeof(int*));
  39.  
  40.     for (int i = 0; i < n; i++) {
  41.         matA[i] = (int*)malloc(n*sizeof(int));
  42.         matB[i] = (int*)malloc(n*sizeof(int));
  43.         matC[i] = (int*)malloc(n*sizeof(int));
  44.         for (int j = 0; j < n; j++) {
  45.             matA[i][j] = rand() % MX;
  46.             matB[i][j] = rand() % MX;
  47.         }
  48.     }
  49.  
  50. /** // Displaying matA
  51.     cout << endl
  52.         << "Matrix A" << endl;
  53.     for (int i = 0; i < n; i++) {
  54.         for (int j = 0; j < n; j++)
  55.             cout << matA[i][j] << " ";
  56.         cout << endl;
  57.     }
  58.  
  59.     // Displaying matB
  60.     cout << endl
  61.         << "Matrix B" << endl;
  62.     for (int i = 0; i < n; i++) {
  63.         for (int j = 0; j < n; j++)
  64.             cout << matB[i][j] << " ";       
  65.         cout << endl;
  66.     }
  67. **/
  68.     // declaring four threads
  69.     pthread_t threads[MAX_THREAD];
  70.  
  71.     time(&start);
  72.     clock_t st, ed;
  73.     st = clock();
  74.     clock_t tic = clock();
  75.     // Creating four threads, each evaluating its own part
  76.     for (int i = 0; i < MAX_THREAD; i++) {
  77.         int* p;
  78.         pthread_create(&threads[i], NULL, multi, (void*)(p));
  79.     }
  80.  
  81.     // joining and waiting for all threads to complete
  82.     for (int i = 0; i < MAX_THREAD; i++)
  83.         pthread_join(threads[i], NULL);  
  84.     clock_t toc = clock();
  85.     ed = clock();
  86.     time(&end);
  87.     auto x =double(ed-st)/double(CLOCKS_PER_SEC);
  88.     return (double)(toc - tic) / CLOCKS_PER_SEC;
  89.  
  90.     // Displaying the result matrix
  91.     /**
  92.     cout << endl
  93.         << "Multiplication of A and B" << endl;
  94.     for (int i = 0; i < n; i++) {
  95.         for (int j = 0; j < n; j++)
  96.             cout << matC[i][j] << " ";       
  97.         cout << endl;
  98.     }
  99.     **/
  100.  
  101. }
  102.  
  103. int main()
  104. {
  105.     for (int i = 1; i < 55; i++)
  106.     {
  107.         cout << "n : " << i*i << ", time :" << job(i*i) << endl;
  108.     }
  109.    
  110. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement