Advertisement
Tvor0zhok

ParProg5.1

Mar 5th, 2023
606
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.48 KB | None | 0 0
  1. #define _USE_MATH_DEFINES
  2. #include <iostream>
  3. #include <iomanip>
  4. #include <string>
  5. #include <cmath>
  6. #include <omp.h>
  7. using namespace std;
  8.  
  9. typedef double (*Function)(double, double, double);
  10.  
  11. // Задание 3
  12. double f3(double x, double y, double z)
  13. {
  14.     return 1 / (2 + (sin((x * x) + (y * y)) * cos(z)));
  15. }
  16.  
  17. // Задание 5
  18. double f5(double x, double y, double z)
  19. {
  20.     return 1 / (2 + sin((x * x) + (y * y) + (z * z)));
  21. }
  22.  
  23. // Задание 7
  24. double f7(double x, double y, double z)
  25. {
  26.     return pow(M_E, z - x * x) / (2 + cos(x + (y * y) + z));
  27. }
  28.  
  29. // Последовательная реализация
  30. double A(Function f, int N)
  31. {
  32.     double h = 1.0 / N, res = 0;
  33.  
  34.     double start = omp_get_wtime();
  35.  
  36.     for (int i = 0; i < N; ++i)
  37.         for (int j = 0; j < N; ++j)
  38.             for (int k = 0; k < N; ++k)
  39.                 res += f(h * (i + 0.5), h * (j + 0.5), h * (k + 0.5));
  40.  
  41.     res *= h * h * h;
  42.    
  43.     double end = omp_get_wtime();
  44.  
  45.     // ПРОВЕРКА:
  46.     // cout << "A: " << res << "\n";
  47.  
  48.     return end - start;
  49. }
  50.  
  51. // а) По умолчанию
  52. double A1(Function f, int N)
  53. {
  54.     double h = 1.0 / N, res = 0;
  55.  
  56.     double start = omp_get_wtime();
  57.  
  58. #pragma omp parallel for shared(h) reduction(+:res)
  59.     for (int i = 0; i < N; ++i)
  60.         for (int j = 0; j < N; ++j)
  61.             for (int k = 0; k < N; ++k)
  62.                 res += f(h * (i + 0.5), h * (j + 0.5), h * (k + 0.5));
  63.  
  64.     res *= h * h * h;
  65.    
  66.     double end = omp_get_wtime();
  67.  
  68.     // ПРОВЕРКА
  69.     // cout << "A1: " << res << "\n";
  70.  
  71.     return end - start;
  72. }
  73.  
  74. // б) schedule(static)
  75. double A2(Function f, int N)
  76. {
  77.     double h = 1.0 / N, res = 0;
  78.  
  79.     double start = omp_get_wtime();
  80.  
  81. #pragma omp parallel for shared(h) reduction(+:res) schedule(static)
  82.     for (int i = 0; i < N; ++i)
  83.         for (int j = 0; j < N; ++j)
  84.             for (int k = 0; k < N; ++k)
  85.                 res += f(h * (i + 0.5), h * (j + 0.5), h * (k + 0.5));
  86.  
  87.     res *= h * h * h;
  88.  
  89.     double end = omp_get_wtime();
  90.  
  91.     // ПРОВЕРКА
  92.     // cout << "A2: " << res << "\n";
  93.  
  94.     return end - start;
  95. }
  96.  
  97. // в) schedule(dynamic)
  98. double A3(Function f, int N)
  99. {
  100.     double h = 1.0 / N, res = 0;
  101.  
  102.     double start = omp_get_wtime();
  103.  
  104. #pragma omp parallel for shared(h) reduction(+:res) schedule(dynamic)
  105.     for (int i = 0; i < N; ++i)
  106.         for (int j = 0; j < N; ++j)
  107.             for (int k = 0; k < N; ++k)
  108.                 res += f(h * (i + 0.5), h * (j + 0.5), h * (k + 0.5));
  109.  
  110.     res *= h * h * h;
  111.    
  112.     double end = omp_get_wtime();
  113.  
  114.     // ПРОВЕРКА
  115.     // cout << "A3: " << res << "\n";
  116.  
  117.     return end - start;
  118. }
  119.  
  120. // Фиксируем N
  121. void task_one_n(Function f, int N)
  122. {
  123.     double concurrent = A(f, N);
  124.  
  125.     double acc1 = concurrent / A1(f, N);
  126.     double acc2 = concurrent / A2(f, N);
  127.     double acc3 = concurrent / A3(f, N);
  128.  
  129.     cout << left << "| " << setw(5) << N << " | " << setw(12) << acc1 << " | " << setw(10) << acc2 << " | " << setw(10) << acc3 << " | " << concurrent << " s\n";
  130. }
  131.  
  132. // Фиксируем функцию f
  133. void task_one_fun(Function f)
  134. {
  135.     cout << "+-------+--------------+------------+------------+\n";
  136.     cout << "|   N   | По умолчанию |   Static   |  Dynamic   |\n";
  137.     cout << "+-------+--------------+------------+------------+\n";
  138.  
  139.     task_one_n(f, 1000);
  140.     task_one_n(f, 1500);
  141.  
  142.     cout << "+-------+--------------+------------+------------+\n\n";
  143. }
  144.  
  145. int main()
  146. {
  147.     setlocale(LC_ALL, "Russian");
  148.     cout << fixed << setprecision(4);
  149.  
  150.     cout << "Задание 3:\n";
  151.     task_one_fun(f3);
  152.  
  153.     cout << "Задание 5:\n";
  154.     task_one_fun(f5);
  155.  
  156.     cout << "Задание 7:\n";
  157.     task_one_fun(f7);
  158.  
  159.     return 0;
  160. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement