Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define _USE_MATH_DEFINES
- #include <iostream>
- #include <iomanip>
- #include <string>
- #include <cmath>
- #include <omp.h>
- using namespace std;
- typedef double (*Function)(double, double, double);
- // Задание 3
- double f3(double x, double y, double z)
- {
- return 1 / (2 + (sin((x * x) + (y * y)) * cos(z)));
- }
- // Задание 5
- double f5(double x, double y, double z)
- {
- return 1 / (2 + sin((x * x) + (y * y) + (z * z)));
- }
- // Задание 7
- double f7(double x, double y, double z)
- {
- return pow(M_E, z - x * x) / (2 + cos(x + (y * y) + z));
- }
- // Последовательная реализация
- double A(Function f, int N)
- {
- double h = 1.0 / N, res = 0;
- double start = omp_get_wtime();
- for (int i = 0; i < N; ++i)
- for (int j = 0; j < N; ++j)
- for (int k = 0; k < N; ++k)
- res += f(h * (i + 0.5), h * (j + 0.5), h * (k + 0.5));
- res *= h * h * h;
- double end = omp_get_wtime();
- // ПРОВЕРКА:
- // cout << "A: " << res << "\n";
- return end - start;
- }
- // а) По умолчанию
- double A1(Function f, int N)
- {
- double h = 1.0 / N, res = 0;
- double start = omp_get_wtime();
- #pragma omp parallel for shared(h) reduction(+:res)
- for (int i = 0; i < N; ++i)
- for (int j = 0; j < N; ++j)
- for (int k = 0; k < N; ++k)
- res += f(h * (i + 0.5), h * (j + 0.5), h * (k + 0.5));
- res *= h * h * h;
- double end = omp_get_wtime();
- // ПРОВЕРКА
- // cout << "A1: " << res << "\n";
- return end - start;
- }
- // б) schedule(static)
- double A2(Function f, int N)
- {
- double h = 1.0 / N, res = 0;
- double start = omp_get_wtime();
- #pragma omp parallel for shared(h) reduction(+:res) schedule(static)
- for (int i = 0; i < N; ++i)
- for (int j = 0; j < N; ++j)
- for (int k = 0; k < N; ++k)
- res += f(h * (i + 0.5), h * (j + 0.5), h * (k + 0.5));
- res *= h * h * h;
- double end = omp_get_wtime();
- // ПРОВЕРКА
- // cout << "A2: " << res << "\n";
- return end - start;
- }
- // в) schedule(dynamic)
- double A3(Function f, int N)
- {
- double h = 1.0 / N, res = 0;
- double start = omp_get_wtime();
- #pragma omp parallel for shared(h) reduction(+:res) schedule(dynamic)
- for (int i = 0; i < N; ++i)
- for (int j = 0; j < N; ++j)
- for (int k = 0; k < N; ++k)
- res += f(h * (i + 0.5), h * (j + 0.5), h * (k + 0.5));
- res *= h * h * h;
- double end = omp_get_wtime();
- // ПРОВЕРКА
- // cout << "A3: " << res << "\n";
- return end - start;
- }
- // Фиксируем N
- void task_one_n(Function f, int N)
- {
- double concurrent = A(f, N);
- double acc1 = concurrent / A1(f, N);
- double acc2 = concurrent / A2(f, N);
- double acc3 = concurrent / A3(f, N);
- cout << left << "| " << setw(5) << N << " | " << setw(12) << acc1 << " | " << setw(10) << acc2 << " | " << setw(10) << acc3 << " | " << concurrent << " s\n";
- }
- // Фиксируем функцию f
- void task_one_fun(Function f)
- {
- cout << "+-------+--------------+------------+------------+\n";
- cout << "| N | По умолчанию | Static | Dynamic |\n";
- cout << "+-------+--------------+------------+------------+\n";
- task_one_n(f, 1000);
- task_one_n(f, 1500);
- cout << "+-------+--------------+------------+------------+\n\n";
- }
- int main()
- {
- setlocale(LC_ALL, "Russian");
- cout << fixed << setprecision(4);
- cout << "Задание 3:\n";
- task_one_fun(f3);
- cout << "Задание 5:\n";
- task_one_fun(f5);
- cout << "Задание 7:\n";
- task_one_fun(f7);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement