Advertisement
eniodordan

RRS - LV5

Jan 28th, 2022
606
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <iostream>
  2. #include <time.h>
  3. #include "omp.h"
  4. #include <stdlib.h>
  5. #include <math.h>
  6.  
  7. using namespace std;
  8.  
  9.  
  10. int main()
  11. {
  12.     int n = 2000,j,k,i;
  13.     double** matrica1 = new double* [n];
  14.     double** matrica2 = new double* [n];
  15.     double** matricaFinal = new double* [n];
  16.     double StartTime, EndTime, mflop, threads[4] = {};
  17.     double realcpu = 0;
  18.  
  19.     for (int i = 0; i < n; i++) {
  20.         matrica1[i] = new double[n];
  21.         matrica2[i] = new double[n];
  22.         matricaFinal[i] = new double[n];
  23.  
  24.     }
  25.     for (int i = 0; i < n; i++) {
  26.         for (int j = 0; j < n; j++) {
  27.  
  28.             matricaFinal[i][j] = 0;
  29.             matrica1[i][j] = (double)1;
  30.             matrica2[i][j] = (double)2;
  31.         }
  32.     }
  33.     omp_set_num_threads(4);
  34.  
  35.     StartTime = omp_get_wtime();
  36.  
  37. #pragma omp parallel private (i,j,k)
  38.  {
  39.  
  40. #pragma omp for nowait
  41.     for (int i = 0; i < n; i++) {
  42.         for (j = 0; j < n; j++) {
  43.            matricaFinal[i][j] = 0;
  44.             for (k = 0; k < n; k++)
  45.             {
  46.                 matricaFinal[i][j] += matrica1[i][k] * matrica2[k][j];
  47.             }
  48.  
  49.         }
  50.     }
  51.     threads [omp_get_thread_num()] = omp_get_wtime() - StartTime;
  52. }
  53.  
  54.  
  55. EndTime = omp_get_wtime();
  56. double cputime = EndTime - StartTime;
  57.  
  58. cout << "user cpu time" << cputime <<endl;
  59. for (i = 0; i < 4; i++)
  60. {
  61.     cout << " kraj !! " << threads[i] << endl;
  62.     realcpu += threads[i];
  63.     }
  64.  
  65. cout << "real cpu time :" << realcpu << endl;
  66.  
  67. mflop = pow(n, 3) * 2;
  68.  
  69. cout << "mflops" << (mflop / (cputime * 1000000)) << endl;
  70. return 0;
  71.    
  72. }
Advertisement
Advertisement
Advertisement
RAW Paste Data Copied
Advertisement