Advertisement
O_Egor

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

Feb 8th, 2021 (edited)
640
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.18 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <omp.h>
  4. using namespace std;
  5.  
  6. class M {
  7.     public:
  8.         vector<vector<int> >v;
  9.         int n;
  10.  
  11.         M()
  12.         {
  13.             n = 0;
  14.         }
  15.         M(int n) :n(n)
  16.         {
  17.             v.resize(n, vector<int>(n, 1));
  18.         }
  19.  
  20.         M operator*(M& b)
  21.         {
  22.             M c(n);
  23.             for (int i = 0; i < n; ++i)
  24.                 for (int j = 0; j < n; ++j)
  25.                     c.v[i][j] = 0;
  26.             //  c.display();
  27.             cout << '\n';
  28.             double t1 = omp_get_wtime();
  29.             omp_set_num_threads(4);
  30.     #pragma omp parallel for
  31.             for (int i = 0; i < n; i++)
  32.                 for (int j = 0; j < n; j++)
  33.                     for (int k = 0; k < n; k++)
  34.                         c.v[i][j] += v[i][k] * b.v[k][j];
  35.                    
  36.             double t2 = omp_get_wtime();
  37.             cout << t2 - t1;
  38.             cout << '\n';
  39.             return c;
  40.         }
  41.  
  42.  
  43.         void display()
  44.         {
  45.             for (int i = 0; i < n; i++)
  46.             {
  47.                 for (int j = 0; j < n; j++)
  48.                     cout << v[i][j] << ' ';
  49.                 cout << '\n';
  50.             }
  51.         }
  52.         void in()
  53.         {
  54.             for (auto& i : v)
  55.                 for (auto& j : i)
  56.                     cin >> j;
  57.         }
  58. };
  59.  
  60. int main()
  61. {
  62.     int n;
  63.     cin >> n;
  64.     M a(n);
  65.     M b(n);
  66.     a.in();
  67.     b.in();
  68.     M c = (a * b);
  69.     //a.display();
  70.     cout << '\n';
  71.     //b.display();
  72.     cout << '\n';
  73.     c.display();
  74.     cout << '\n';
  75.     system("pause");
  76.     return 0;
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement