Advertisement
Domy131097

Lv5

Jan 19th, 2022
674
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <iostream>
  2. #include <omp.h>
  3. #include <time.h>
  4.  
  5. using namespace std;
  6.  
  7. int main()
  8. {
  9.  
  10.     int const size = 2000;
  11.     int const thread_count = 8;
  12.     clock_t t1, t2;
  13.  
  14.     double** mat1 = new double* [size];
  15.     double** mat2 = new double* [size];
  16.     double** matRes = new double* [size];
  17.  
  18.     // init matrix
  19.     for (int i = 0; i < size; i++) {
  20.         mat1[i] = new double[size];
  21.         mat2[i] = new double[size];
  22.         matRes[i] = new double[size];
  23.     }
  24.  
  25.     srand((unsigned)time(NULL));
  26.  
  27.     // populate matrix
  28.     for (int i = 0; i < size; i++) {
  29.         for (int j = 0; j < size; j++) {
  30.             mat1[i][j] = ceil(((double)rand() / (double)RAND_MAX * 10) * 100) / 100;
  31.             mat2[i][j] = ceil(((double)rand() / (double)RAND_MAX * 10) * 100) / 100;
  32.             matRes[i][j] = 0;
  33.         }
  34.     }
  35.  
  36.     // print matrix
  37.     /*for (int i = 0; i < size; i++) {
  38.         for (int j = 0; j < size; j++) {
  39.             cout << mat1[i][j] << " ";
  40.         }
  41.         cout << endl;
  42.     }
  43.  
  44.     cout << endl;
  45.  
  46.     for (int i = 0; i < size; i++) {
  47.         for (int j = 0; j < size; j++) {
  48.             cout << mat2[i][j] << " ";
  49.         }
  50.         cout << endl;
  51.     }*/
  52.  
  53.     omp_set_num_threads(thread_count);
  54.  
  55.     /*
  56.     cout << "Thread count before loop: " << omp_get_num_threads() << endl;
  57.     */
  58.  
  59.     cout << "Mnozenje" << endl;
  60.  
  61.     // multiply
  62.     t1 = clock();
  63.     #pragma omp parallel
  64.     {
  65.         /*if (omp_get_thread_num() == 0) {
  66.             cout << "Thread count inside loop: " << omp_get_num_threads() << endl;
  67.         }*/
  68.         #pragma omp for
  69.         for (int i = 0; i < size; i++) {
  70.             for (int j = 0; j < size; j++) {
  71.                 for (int k = 0; k < size; k++)
  72.                     matRes[i][j] += mat1[i][k] * mat2[k][j];
  73.             }
  74.         }
  75.     }
  76.     t2 = clock();
  77.  
  78.     cout << endl;
  79.  
  80.     /*for (int i = 0; i < size; i++) {
  81.         for (int j = 0; j < size; j++) {
  82.             cout << matRes[i][j] << " ";
  83.  
  84.         }
  85.         cout << endl;
  86.     }*/
  87.  
  88.    
  89.     cout << "User CPU time: " << (t2 - t1) << " ms with " << thread_count << " threads" << endl;
  90.     unsigned int realTime = (t2 - t1) * thread_count;
  91.     cout << "Real CPU time: " << realTime << " ms" << endl;
  92.     unsigned int nFlop = size + size * size + size * size * size * 2;
  93.     double tFlop = realTime / 1000;
  94.     double mflops = nFlop / (tFlop * 1000000);
  95.     cout << "MFLOPS: " << mflops << endl;
  96.  
  97.     return 0;
  98. }
Advertisement
Advertisement
Advertisement
RAW Paste Data Copied
Advertisement