Advertisement
Artem_Chepurov

Untitled

Oct 31st, 2022
708
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.40 KB | None | 0 0
  1. #include <iostream>
  2. #include <math.h>
  3. #include <omp.h>
  4. #include <random>
  5. using namespace std;
  6.  
  7. #pragma comment(linker, "/STACK:1024000000")
  8. int main()
  9. {
  10.    
  11.     /*int N = 100;
  12.     int n = 1;
  13.     double sum = 0.0;
  14.     double start, end;
  15.     start = omp_get_wtime();
  16.     #pragma omp parallel for private(n) reduction(+:sum) num_threads(8) schedule(guided, 10000)
  17.     for(n = 1; n <= N; n++)
  18.     {
  19.         sum+= pow(-1, n) / (pow((2 * n + 1), 3) - 1);
  20.     }
  21.     end = omp_get_wtime();
  22.     cout << "Sum = " << sum << endl;
  23.     cout << "Num Thread = " << omp_get_thread_num() << endl;
  24.     cout << "Work took seconds  " << end - start;*/
  25.    
  26.     srand(time(0));
  27.     cout << "count repeat" << endl;
  28.     int q;
  29.     cin >> q;
  30.  
  31.     const int N = 5;
  32.     int i, j;
  33.     float A[N][N];
  34.     float B[N][N];
  35.     float C[N][N];
  36.     float sum;
  37.     for (i = 0; i < N; i++) {
  38.         for (j = 0; j < N; j++) {
  39.             A[i][j] = (floorf((rand() * 1.0 / RAND_MAX - 0.5) * 100)) / 100;
  40.             B[i][j] = (floorf((rand() * 1.0 / RAND_MAX - 0.5) * 100)) / 100;
  41.             C[i][j] = 0.0;
  42.         }
  43.     }
  44.     double start, end;
  45.  
  46.     start = omp_get_wtime();
  47.     for (int l = 0; l < q; l++)
  48.         for (i = 0; i < N; i++) {
  49.             for (j = 0; j < N; j++) {
  50.                 sum = 0;
  51.                 for (int k = 0; k < N; k++)
  52.                     sum += A[i][k] * B[k][j];
  53.                 C[i][j] = sum;
  54.             }
  55.         }
  56.     end = omp_get_wtime();
  57.     cout << "Matrix C:" << endl << C[0][0] << " " << C[N - 1][0] << endl << C[0][N - 1] << " " << C[N - 1][N - 1] << endl;
  58.     cout << "Work took seconds non parallel  " << end - start << endl;
  59.  
  60.     start = omp_get_wtime();
  61.     for (int l = 0; l < q; l++)
  62.     {
  63. #pragma omp parallel for private(i,j)
  64.         for (i = 0; i < N; i++) {
  65.             for (j = 0; j < N; j++) {
  66.                 sum = 0;
  67.                 for (int k = 0; k < N; k++)
  68.                     sum += A[i][k] * B[k][j];
  69.                 C[i][j] = sum;
  70.             }
  71.         }
  72.     }
  73.     end = omp_get_wtime();
  74.     cout << "Matrix C:" << endl << C[0][0] << " " << C[N - 1][0] << endl << C[0][N - 1] << " " << C[N - 1][N - 1] << endl;
  75.     cout << "Work took seconds parallel i  " << end - start << endl;
  76.  
  77.  
  78.     start = omp_get_wtime();
  79.     for (int l = 0; l < q; l++)
  80.     {
  81.         for (i = 0; i < N; i++) {
  82. #pragma omp parallel for
  83.             for (j = 0; j < N; j++) {
  84.                 sum = 0;
  85.                 for (int k = 0; k < N; k++)
  86.                     sum += A[i][k] * B[k][j];
  87.                 C[i][j] = sum;
  88.             }
  89.         }
  90.     }
  91.     end = omp_get_wtime();
  92.     cout << "Matrix C:" << endl << C[0][0] << " " << C[N - 1][0] << endl << C[0][N - 1] << " " << C[N - 1][N - 1] << endl;
  93.     cout << "Work took seconds parallel j  " << end - start << endl;
  94.  
  95.  
  96.     start = omp_get_wtime();
  97.     for (int l = 0; l < q; l++)
  98.     {
  99.         for (i = 0; i < N; i++) {
  100.             for (j = 0; j < N; j++) {
  101.                 sum = 0;
  102.                 #pragma omp parallel for reduction(+:sum)
  103.                 for (int k = 0; k < N; k++)
  104.                     sum += A[i][k] * B[k][j];
  105.                 C[i][j] = sum;
  106.             }
  107.         }
  108.     }
  109.     end = omp_get_wtime();
  110.     cout << "Matrix C:" << endl << C[0][0] << " " << C[N - 1][0] << endl << C[0][N - 1] << " " << C[N - 1][N - 1] << endl;
  111.     cout << "Work took seconds parallel k  " << end - start << endl;
  112.  
  113. }
  114.  
  115.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement