Facebook
From Tomcik, 5 Years ago, written in C++.
Embed
Download Paste or View Raw
Hits: 201
  1. #include <iostream>
  2. #include <omp.h>
  3. #include <cmath>
  4. #include <cstdlib>
  5. #include <fstream>
  6.  
  7. using namespace std;
  8.  
  9. int main()
  10. {
  11.   double omp_time1, omp_time2;
  12.   int n;
  13.   cout << "Podaj rozmiar macierzy"<< endl;
  14.   cin >> n;
  15.   double **A = new double*[n];
  16.   double **B = new double*[n];
  17.   double **C = new double*[n];
  18.  
  19.   for(int i=0;i<n;++i)
  20.   {
  21.     A[i]= new double[n];
  22.     B[i]= new double[n];
  23.     C[i]= new double[n];
  24.   }
  25.  
  26.   ofstream dopliku("macierze.txt");
  27.  
  28.   for(int i=0;i<n;++i)
  29.   {
  30.     for(int j=0;j<n;++j)
  31.     {
  32.       A[i][j] = rand() %100 + 1;
  33.       B[i][j] = rand() %100 + 1;
  34.     }
  35.   }
  36.  
  37.   #pragma omp parallel
  38.   {
  39.     #pragma omp sections
  40.     {
  41.       #pragma omp section
  42.       {
  43.         omp_time1 = omp_get_wtime();
  44.         for(int i=0;i<n;++i)
  45.         {
  46.           for(int j=0;j<n;++j)
  47.           {
  48.             dopliku << A[i][j];
  49.             dopliku << B[i][j];
  50.           }
  51.         }
  52.         dopliku.close();
  53.         cout << "Koniec zapisu" << endl;
  54.         omp_time1 =omp_get_wtime()-omp_time1;
  55.       }
  56.       #pragma omp section
  57.       {
  58.         omp_time2 = omp_get_wtime();
  59.         for(int i=0;i<n;++i)
  60.         {
  61.           for(int j=0;j<n;++j)
  62.           {
  63.             for(int k=0;k<n;++k)
  64.             {
  65.               C[i][j] += A[i][k] * B[k][j];
  66.             }
  67.           }
  68.         }
  69.         cout << "Koniec obliczen" << endl;
  70.         omp_time2 =omp_get_wtime()-omp_time2;
  71.       }
  72.     }
  73.   }
  74.   cout << "Czas sekcji zapisu " << omp_time1 << endl;
  75.   cout << "Czas sekcji obliczen " << omp_time2 << endl;
  76. }
  77.