Advertisement
sepi0l

hpc_paramatrix

Apr 25th, 2024
697
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.95 KB | None | 0 0
  1. #include<iostream>
  2. #include<omp.h>
  3. #include<math.h>
  4. using namespace std;
  5. int main()
  6. {
  7.     cout<<"Number of rows in first matrix: ";
  8.     int row1;
  9.     cin>>row1;
  10.  
  11.     cout<<"Number of columns in first matrix: ";
  12.     int col1;
  13.     cin>>col1;
  14.  
  15.     int row2 = col1;
  16.  
  17.     cout<<"Number of columns in second matrix: ";
  18.     int col2;
  19.     cin>>col2;
  20.  
  21.     cout<<endl;
  22.     cout<<"Matrix 1:"<<endl;
  23.     int mat1[row1][col1];
  24.     for(int i=0; i<row1; i++)
  25.     {
  26.         for(int j=0; j<col1; j++)
  27.         {
  28.             mat1[i][j] = rand()%10;
  29.             cout<<mat1[i][j]<<" ";
  30.         }
  31.         cout<<endl;
  32.     }
  33.     cout<<endl;
  34.  
  35.     cout<<"Matrix 2:"<<endl;
  36.     int mat2[row2][col2];
  37.     for(int i=0; i<row2; i++)
  38.     {
  39.         for(int j=0; j<col2; j++)
  40.         {
  41.             mat2[i][j] = rand()%10;
  42.             cout<<mat2[i][j]<<" ";
  43.         }
  44.         cout<<endl;
  45.     }
  46.     cout<<endl;
  47.  
  48.     cout<<"Multiplication: "<<endl;
  49.     int mat3[row1][col2];
  50.     #pragma omp parallel for
  51.     for(int i=0;i<row1;i++)
  52.     {
  53.         #pragma omp parallel for
  54.         for(int j=0;j<col2;j++)
  55.         {
  56.             mat3[i][j]=0;
  57.             int arr[col1];
  58.  
  59.             #pragma omp parallel for
  60.             for(int k=0;k<col1;k++) arr[k] = mat1[i][k]*mat2[k][j];
  61.  
  62.             int sum = 0;
  63.             #pragma omp parallel for reduction(+: sum)
  64.             for (int l = 0; l < col1; l++) sum += arr[l];
  65.  
  66.             mat3[i][j] = sum;
  67.         }
  68.     }
  69.  
  70.     for(int i=0; i<row1; i++)
  71.     {
  72.         for(int j=0; j<col2; j++) cout<<mat3[i][j]<<" ";
  73.         cout<<endl;
  74.     }
  75.  
  76.     return 0;
  77. }
  78. /*
  79. Number of rows in first matrix: 5
  80. Number of columns in first matrix: 5
  81. Number of columns in second matrix: 6
  82.  
  83. Matrix 1:
  84. 1 7 4 0 9
  85. 4 8 8 2 4
  86. 5 5 1 7 1
  87. 1 5 2 7 6
  88. 1 4 2 3 2
  89.  
  90. Matrix 2:
  91. 2 1 6 8 5 7
  92. 6 1 8 9 2 7
  93. 9 5 4 3 1 2
  94. 3 3 4 1 1 3
  95. 8 7 4 2 7 7
  96.  
  97. Multiplication:
  98. 152 91 114 101 86 127
  99. 166 86 144 138 74 134
  100. 78 43 106 97 50 100
  101. 119 79 106 78 66 109
  102. 69 38 66 57 32 62
  103.  
  104. Process returned 0 (0x0)   execution time : 4.553 s
  105. Press any key to continue.
  106.  
  107. If Matrix A has dimensions 5 x 5 (5 rows and 5 columns),
  108. and Matrix B has dimensions 5 x 6 (5 rows and 6 columns),
  109. the resultant matrix after multiplication will have dimensions 5 x 6 (5 rows and 6 columns).
  110. */
  111.  
  112. /*
  113. This code performs matrix multiplication using OpenMP parallelization. Let's understand it step by step:
  114.  
  115. 1. **Input:**
  116.    - The user is prompted to enter the dimensions of two matrices: the number of rows and columns for
  117.    the first matrix and the number of columns for the second matrix.
  118.    - Matrices `mat1` and `mat2` are randomly generated based on the specified dimensions.
  119.  
  120. 2. **Matrix Multiplication:**
  121.    - Matrix multiplication is performed using nested loops.
  122.    - OpenMP directives are used to parallelize the computation.
  123.    - Outer loop iterates over the rows of the first matrix (`mat1`).
  124.    - Inner loop iterates over the columns of the second matrix (`mat2`).
  125.    - For each element of the resultant matrix (`mat3`), a temporary array `arr` is computed, storing the
  126.     products of corresponding elements of rows from `mat1` and columns from `mat2`.
  127.    - Another loop calculates the sum of the elements in the `arr` array using OpenMP reduction.
  128.  
  129. 3. **Output:**
  130.    - The resultant matrix `mat3` is printed, which represents the product of `mat1` and `mat2`.
  131.  
  132. 4. **Performance:**
  133.    - OpenMP parallelization is employed to distribute the computation across multiple threads, potentially
  134.    reducing the execution time.
  135.    - The execution time of the parallel computation is provided at the end of the output.
  136.  
  137. 5. **Sample Output:**
  138.    - For example, if the dimensions of `mat1` are 5x5 and the dimensions of `mat2` are 5x6, the output will
  139.    be a 5x6 matrix representing the result of the matrix multiplication.
  140.  
  141. Overall, this code demonstrates how matrix multiplication can be parallelized using OpenMP directives, potentially
  142.  improving performance by leveraging multiple threads for concurrent computation.
  143. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement