SHARE
TWEET

Untitled

a guest Apr 24th, 2019 63 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include "pch.h"
  2. #include <iostream>
  3. #include <omp.h>
  4. #include <cstdlib>
  5. #include <ctime>
  6. #include <time.h>
  7. #include <stdio.h>
  8.  
  9. using namespace std;
  10.  
  11. #define N 600
  12. #define R 100
  13. #define NUM_OF_THREADS 4
  14.  
  15. FILE *result_file;
  16.  
  17. float matrix_a[N][N];
  18. float matrix_b[N][N];
  19. float matrix_r[N][N];
  20. auto threeForTempMatrix = new float[10][N][N];
  21.  
  22. void initialize_matrices()
  23. {
  24.     // zdefiniowanie zawarosci poczatkowej macierzy
  25.     //#pragma omp parallel for
  26.     for (int i = 0; i < N; i++) {
  27.         for (int j = 0; j < N; j++) {
  28.             matrix_a[i][j] = (float)rand() / RAND_MAX;
  29.             matrix_b[i][j] = (float)rand() / RAND_MAX;
  30.             matrix_r[i][j] = 0.0;
  31.         }
  32.     }
  33. }
  34.  
  35. void initialize_matricesZ()
  36. {
  37.     // zdefiniowanie zawarosci poczatkowej macierzy
  38. #pragma omp parallel for
  39.     for (int i = 0; i < N; i++) {
  40.         for (int j = 0; j < N; j++) {
  41.             matrix_r[i][j] = 0.0;
  42.         }
  43.     }
  44. }
  45.  
  46. void initialize_temp()
  47. {
  48.     // zdefiniowanie zawarosci poczatkowej macierzy
  49. #pragma omp parallel for
  50.     for (int k = 0; k < 6; k++) {
  51.         for (int i = 0; i < N; i++) {
  52.             for (int j = 0; j < N; j++) {
  53.                 threeForTempMatrix[k][i][j] = 0.0;
  54.             }
  55.         }
  56.     }
  57. }
  58.  
  59. void threeLoops() {
  60. #pragma omp parallel for
  61.     for (int k = 0; k < N; k++)
  62.         for (int i = 0; i < N; i++)
  63.             for (int j = 0; j < N; j++)
  64.                 matrix_r[i][j] += matrix_a[i][k] * matrix_b[k][j];
  65. }
  66.  
  67. void sixLoops() {
  68.     for (int i = 0; i < N; i += R)
  69. #pragma omp parallel for
  70.         for (int j = 0; j < N; j += R)
  71.             for (int k = 0; k < N; k += R)
  72.                 for (int ii = i; ii < i + R; ii++)
  73.                     for (int kk = k; kk < k + R; kk++)
  74.                         for (int jj = j; jj < j + R; jj++)
  75.                             matrix_r[ii][jj] += matrix_a[ii][kk] * matrix_b[kk][jj];
  76. }
  77.  
  78. void sequenceLoopsSmall() {
  79.     for (int i = 0; i < N; i++)
  80.         for (int k = 0; k < N; k++)
  81.             for (int j = 0; j < N; j++)
  82.                 matrix_r[i][j] += matrix_a[i][k] * matrix_b[k][j];
  83. }
  84.  
  85. void sequenceLoopsBig() {
  86.     for (int i = 0; i < N; i += R)
  87.         for (int j = 0; j < N; j += R)
  88.             for (int k = 0; k < N; k += R)
  89.                 for (int ii = i; ii < i + R; ii++)
  90.                     for (int kk = k; kk < k + R; kk++)
  91.                         for (int jj = j; jj < j + R; jj++)
  92.                             matrix_r[ii][jj] += matrix_a[ii][kk] * matrix_b[kk][jj];
  93. }
  94.  
  95. int main() {
  96.     srand(time(NULL));
  97.  
  98.     if (fopen_s(&result_file, "results.txt", "a") < 0) {
  99.         fprintf(stderr, "nie mozna otworzyc pliku wyniku \n");
  100.         perror("results");
  101.         return(EXIT_FAILURE);
  102.     }
  103.  
  104.     omp_set_num_threads(NUM_OF_THREADS);
  105.  
  106.     fprintf(result_file, "N for 3-LOOP: %d\n", N);
  107.     fprintf(result_file, "N for 6-LOOP: %d\n", N);
  108.     fprintf(result_file, "R for 6-LOOP: %d\n", R);
  109.     fprintf(result_file, "NUM_OF_THREADS : %d\n", NUM_OF_THREADS);
  110.  
  111.     initialize_matrices();
  112.     cout << "Matrices are prepared..." << endl;
  113.  
  114.     initialize_matricesZ();
  115.     double start = (double)clock() / CLK_TCK;
  116.     sequenceLoopsSmall();
  117.     std::cout << "\n" << matrix_r[0][0] << "\n";
  118.     double end = (double)clock() / CLK_TCK;
  119.     printf("3-loops sequence processing: %8.4f sec \n", end - start);
  120.     fprintf(result_file,
  121.         "3-loops sequence processing : %8.4f sec (%6.4f sec rozdzielczosc pomiaru)\n",
  122.         end - start, 1.0 / CLK_TCK);
  123.  
  124.     initialize_matricesZ();
  125.     start = (double)clock() / CLK_TCK;
  126.     sequenceLoopsBig();
  127.     std::cout << "\n" << matrix_r[0][0] << "\n";
  128.     end = (double)clock() / CLK_TCK;
  129.     printf("6-loops sequence processing: %8.4f sec \n", end - start);
  130.     fprintf(result_file,
  131.         "6-loops sequence processing : %8.4f sec (%6.4f sec rozdzielczosc pomiaru)\n",
  132.         end - start, 1.0 / CLK_TCK);
  133.  
  134.     initialize_matricesZ();
  135.     initialize_temp();
  136.     start = (double)clock() / CLK_TCK;
  137.     threeLoops();
  138.     std::cout << "\n" << matrix_r[0][0] << "\n";
  139.     end = (double)clock() / CLK_TCK;
  140.     printf("3-loops parallel processing: %8.4f sec \n", end - start);
  141.     fprintf(result_file,
  142.         "3-loops parallel processing : %8.4f sec (%6.4f sec rozdzielczosc pomiaru)\n",
  143.         end - start, 1.0 / CLK_TCK);
  144.  
  145.     initialize_matricesZ();
  146.     start = (double)clock() / CLK_TCK;
  147.     sixLoops();
  148.     std::cout << "\n" << matrix_r[0][0] << "\n";
  149.     end = (double)clock() / CLK_TCK;
  150.     printf("6-loops parallel processing: %8.4f sec \n", end - start);
  151.     fprintf(result_file,
  152.         "6-loops parallel processing : %8.4f sec (%6.4f sec rozdzielczosc pomiaru)\n",
  153.         end - start, 1.0 / CLK_TCK);
  154.  
  155.     fprintf(result_file, "===========================================\n");
  156.     char c;
  157.     std::cin >> c;
  158.     fclose(result_file);
  159.     return 0;
  160. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top