Advertisement
O_Egor

Параллельное программирование лаба 1

Mar 15th, 2021 (edited)
1,365
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.12 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. using namespace std;
  4. class Matrix{
  5. public:
  6.     Matrix(const int& n):n(n){
  7.         m.resize(n, vector<int> (n));
  8.     }
  9.     void output () const{
  10.         for(auto& i : m){
  11.             for(auto& j : i)
  12.                 cout<<j<<' ';
  13.             cout<<endl;
  14.         }
  15.     }
  16.     Matrix operator *(const Matrix& m2){
  17.         Matrix tmp(n);
  18.         #pragma omp parallel for
  19.         for(int i = 0; i <n; i++){
  20.             for(int j = 0; j < n; j++){
  21.                 for(int k = 0; k < n; k++){
  22.                     tmp.m[i][j]+=m[i][k]*m2.m[k][j];
  23.                 }
  24.             }
  25.         }
  26.         return tmp;
  27.     }
  28.     void in(){
  29.         for(auto& i: m)
  30.             for(auto& j: i)
  31.                 cin>>j;
  32.     }
  33. private:
  34.     int n;
  35.     vector<vector<int> > m;
  36. };
  37. int main(){
  38.     int n;
  39.     cin>>n;
  40.     Matrix m1(n);
  41.     m1.in();
  42.     Matrix m2(n);
  43.    m2.in();
  44.     Matrix m3(n);
  45.     m3=m1*m2;
  46.     cout<<endl;
  47.     m1.output();
  48.     cout<<endl;
  49.     m2.output();
  50.     cout<<endl;
  51.     m3.output();
  52.     return 0;
  53. }
  54.  
  55. /*
  56. 3
  57. 1 2 1
  58. 3 0 4
  59. 1 3 2
  60.  
  61. 4 2 0
  62. 1 -1 3
  63. 2 3 1
  64. */
  65.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement