| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include "ovsyannikov_n_simpson_method/all/include/ops_all.hpp" | ||
| 2 | |||
| 3 | #include <mpi.h> | ||
| 4 | #include <omp.h> | ||
| 5 | #include <tbb/tbb.h> | ||
| 6 | |||
| 7 | #include <algorithm> | ||
| 8 | #include <atomic> | ||
| 9 | #include <cmath> | ||
| 10 | #include <functional> | ||
| 11 | #include <thread> | ||
| 12 | |||
| 13 | #include "ovsyannikov_n_simpson_method/common/include/common.hpp" | ||
| 14 | |||
| 15 | namespace ovsyannikov_n_simpson_method { | ||
| 16 | |||
| 17 | ✗ | double OvsyannikovNSimpsonMethodALL::Function(double x, double y) { | |
| 18 | 8556 | return x + y; | |
| 19 | } | ||
| 20 | |||
| 21 | ✗ | double OvsyannikovNSimpsonMethodALL::GetCoeff(int i, int n) { | |
| 22 | ✗ | if (i == 0 || i == n) { | |
| 23 | return 1.0; | ||
| 24 | } | ||
| 25 |
2/4✓ Branch 0 taken 3969 times.
✓ Branch 1 taken 4175 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
8144 | return (i % 2 == 1) ? 4.0 : 2.0; |
| 26 | } | ||
| 27 | |||
| 28 | 12 | OvsyannikovNSimpsonMethodALL::OvsyannikovNSimpsonMethodALL(const InType &in) { | |
| 29 | SetTypeOfTask(GetStaticTypeOfTask()); | ||
| 30 | 12 | GetInput() = in; | |
| 31 | 12 | } | |
| 32 | |||
| 33 | 12 | bool OvsyannikovNSimpsonMethodALL::ValidationImpl() { | |
| 34 |
4/8✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 12 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 12 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 12 times.
|
12 | return GetInput().nx > 0 && GetInput().nx % 2 == 0 && GetInput().ny > 0 && GetInput().ny % 2 == 0; |
| 35 | } | ||
| 36 | |||
| 37 | 12 | bool OvsyannikovNSimpsonMethodALL::PreProcessingImpl() { | |
| 38 | 12 | params_ = GetInput(); | |
| 39 | 12 | res_ = 0.0; | |
| 40 | 12 | return true; | |
| 41 | } | ||
| 42 | |||
| 43 | 12 | bool OvsyannikovNSimpsonMethodALL::RunImpl() { | |
| 44 | 12 | int rank = 0; | |
| 45 | 12 | int size = 1; | |
| 46 | 12 | MPI_Comm_rank(MPI_COMM_WORLD, &rank); | |
| 47 | 12 | MPI_Comm_size(MPI_COMM_WORLD, &size); | |
| 48 | |||
| 49 | 12 | const int nx_l = params_.nx; | |
| 50 | 12 | const int ny_l = params_.ny; | |
| 51 | 12 | const double ax_l = params_.ax; | |
| 52 | 12 | const double ay_l = params_.ay; | |
| 53 | 12 | const double hx = (params_.bx - params_.ax) / nx_l; | |
| 54 | 12 | const double hy = (params_.by - params_.ay) / ny_l; | |
| 55 | |||
| 56 | 12 | int total_rows = nx_l + 1; | |
| 57 | 12 | int rows_per_rank = total_rows / size; | |
| 58 | 12 | int remainder = total_rows % size; | |
| 59 | |||
| 60 |
2/2✓ Branch 0 taken 6 times.
✓ Branch 1 taken 6 times.
|
12 | int my_start = (rank * rows_per_rank) + std::min(rank, remainder); |
| 61 |
2/2✓ Branch 0 taken 6 times.
✓ Branch 1 taken 6 times.
|
12 | int my_end = my_start + rows_per_rank + (rank < remainder ? 1 : 0); |
| 62 | |||
| 63 | 12 | double local_sum = 0.0; | |
| 64 | |||
| 65 | 12 | #pragma omp parallel for default(none) shared(my_start, my_end, nx_l, ny_l, ax_l, ay_l, hx, hy) reduction(+ : local_sum) | |
| 66 | for (int i = my_start; i < my_end; ++i) { | ||
| 67 | const double x = ax_l + (static_cast<double>(i) * hx); | ||
| 68 | const double coeff_x = GetCoeff(i, nx_l); | ||
| 69 | |||
| 70 | double row_sum = tbb::parallel_reduce(tbb::blocked_range<int>(0, ny_l + 1), 0.0, | ||
| 71 | 8556 | [&](const tbb::blocked_range<int> &r, double sum) { | |
| 72 |
2/2✓ Branch 0 taken 8556 times.
✓ Branch 1 taken 8556 times.
|
17112 | for (int j = r.begin(); j < r.end(); ++j) { |
| 73 | 8556 | const double y = ay_l + (static_cast<double>(j) * hy); | |
| 74 |
2/2✓ Branch 0 taken 8144 times.
✓ Branch 1 taken 412 times.
|
8556 | const double coeff_y = GetCoeff(j, ny_l); |
| 75 | 8556 | sum += coeff_y * Function(x, y); | |
| 76 | } | ||
| 77 | 8556 | return sum; | |
| 78 | }, std::plus<>()); | ||
| 79 | |||
| 80 | local_sum += coeff_x * row_sum; | ||
| 81 | } | ||
| 82 | |||
| 83 | 12 | double total_sum = 0.0; | |
| 84 | 12 | MPI_Reduce(&local_sum, &total_sum, 1, MPI_DOUBLE, MPI_SUM, 0, MPI_COMM_WORLD); | |
| 85 | |||
| 86 |
2/2✓ Branch 0 taken 6 times.
✓ Branch 1 taken 6 times.
|
12 | if (rank == 0) { |
| 87 | 6 | res_ = (hx * hy / 9.0) * total_sum; | |
| 88 | } | ||
| 89 | |||
| 90 | 12 | MPI_Bcast(&res_, 1, MPI_DOUBLE, 0, MPI_COMM_WORLD); | |
| 91 | |||
| 92 | 12 | std::atomic<int> counter(0); | |
| 93 | 12 | std::thread t([&]() { counter++; }); | |
| 94 |
1/2✓ Branch 1 taken 12 times.
✗ Branch 2 not taken.
|
12 | t.join(); |
| 95 | |||
| 96 |
1/2✓ Branch 1 taken 12 times.
✗ Branch 2 not taken.
|
12 | MPI_Barrier(MPI_COMM_WORLD); |
| 97 | 12 | return true; | |
| 98 | } | ||
| 99 | |||
| 100 | 12 | bool OvsyannikovNSimpsonMethodALL::PostProcessingImpl() { | |
| 101 | 12 | GetOutput() = res_; | |
| 102 | 12 | return true; | |
| 103 | } | ||
| 104 | |||
| 105 | } // namespace ovsyannikov_n_simpson_method | ||
| 106 |