| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include "lifanov_k_trapezoid_method/mpi/include/ops_mpi.hpp" | ||
| 2 | |||
| 3 | #include <mpi.h> | ||
| 4 | |||
| 5 | #include <algorithm> | ||
| 6 | #include <cmath> | ||
| 7 | #include <vector> | ||
| 8 | |||
| 9 | #include "lifanov_k_trapezoid_method/common/include/common.hpp" | ||
| 10 | |||
| 11 | namespace lifanov_k_trapezoid_method { | ||
| 12 | |||
| 13 | namespace { | ||
| 14 | |||
| 15 | double Function(double x, double y) { | ||
| 16 | 13243 | return (x * x) + (y * y); | |
| 17 | } | ||
| 18 | |||
| 19 | double Weight(int ix, int iy, int nx, int ny) { | ||
| 20 | 13243 | double wx = (ix == 0 || ix == nx) ? 0.5 : 1.0; | |
| 21 |
2/2✓ Branch 0 taken 12897 times.
✓ Branch 1 taken 346 times.
|
13243 | double wy = (iy == 0 || iy == ny) ? 0.5 : 1.0; |
| 22 | 13243 | return wx * wy; | |
| 23 | } | ||
| 24 | |||
| 25 | 6 | double ComputeLocalSum(int x_start, int x_end, int nx, int ny, double ax, double ay, double hx, double hy) { | |
| 26 | double local_sum = 0.0; | ||
| 27 | |||
| 28 |
2/2✓ Branch 0 taken 173 times.
✓ Branch 1 taken 6 times.
|
179 | for (int i = x_start; i <= x_end; ++i) { |
| 29 | 173 | const double x = ax + (i * hx); | |
| 30 |
2/2✓ Branch 0 taken 13243 times.
✓ Branch 1 taken 173 times.
|
13416 | for (int j = 0; j <= ny; ++j) { |
| 31 |
2/2✓ Branch 0 taken 12897 times.
✓ Branch 1 taken 346 times.
|
13243 | const double y = ay + (j * hy); |
| 32 | 13243 | local_sum += Weight(i, j, nx, ny) * Function(x, y); | |
| 33 | } | ||
| 34 | } | ||
| 35 | |||
| 36 | 6 | return local_sum; | |
| 37 | } | ||
| 38 | |||
| 39 | } // namespace | ||
| 40 | |||
| 41 |
1/2✓ Branch 1 taken 6 times.
✗ Branch 2 not taken.
|
6 | LifanovKTrapezoidMethodMPI::LifanovKTrapezoidMethodMPI(const InType &input) { |
| 42 | SetTypeOfTask(GetStaticTypeOfTask()); | ||
| 43 |
1/2✓ Branch 1 taken 6 times.
✗ Branch 2 not taken.
|
6 | GetInput() = input; |
| 44 | 6 | GetOutput() = 0.0; | |
| 45 | 6 | } | |
| 46 | |||
| 47 |
1/2✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
|
6 | bool LifanovKTrapezoidMethodMPI::ValidationImpl() { |
| 48 | const auto &in = GetInput(); | ||
| 49 | |||
| 50 |
1/2✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
|
6 | if (in.size() != 6) { |
| 51 | return false; | ||
| 52 | } | ||
| 53 | |||
| 54 | 6 | const double ax = in[0]; | |
| 55 | 6 | const double bx = in[1]; | |
| 56 | 6 | const double ay = in[2]; | |
| 57 | 6 | const double by = in[3]; | |
| 58 | 6 | const int nx = static_cast<int>(in[4]); | |
| 59 | 6 | const int ny = static_cast<int>(in[5]); | |
| 60 | |||
| 61 |
2/4✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 6 times.
✗ Branch 3 not taken.
|
6 | return bx > ax && by > ay && nx > 0 && ny > 0; |
| 62 | } | ||
| 63 | |||
| 64 | 6 | bool LifanovKTrapezoidMethodMPI::PreProcessingImpl() { | |
| 65 | 6 | GetOutput() = 0.0; | |
| 66 | 6 | return true; | |
| 67 | } | ||
| 68 | |||
| 69 | 6 | bool LifanovKTrapezoidMethodMPI::RunImpl() { | |
| 70 | 6 | int rank = 0; | |
| 71 | 6 | int size = 1; | |
| 72 | 6 | MPI_Comm_rank(MPI_COMM_WORLD, &rank); | |
| 73 | 6 | MPI_Comm_size(MPI_COMM_WORLD, &size); | |
| 74 | |||
| 75 | const auto &in = GetInput(); | ||
| 76 | |||
| 77 | 6 | const int nx = static_cast<int>(in[4]); | |
| 78 | 6 | const int ny = static_cast<int>(in[5]); | |
| 79 | |||
| 80 | 6 | const double hx = (in[1] - in[0]) / nx; | |
| 81 | 6 | const double hy = (in[3] - in[2]) / ny; | |
| 82 | |||
| 83 | 6 | const int total_nodes = nx + 1; | |
| 84 | 6 | const int base = total_nodes / size; | |
| 85 | 6 | const int rem = total_nodes % size; | |
| 86 | |||
| 87 |
2/2✓ Branch 0 taken 3 times.
✓ Branch 1 taken 3 times.
|
6 | const int x_start = (rank * base) + std::min(rank, rem); |
| 88 | 6 | int x_end = x_start + base - 1; | |
| 89 |
2/2✓ Branch 0 taken 3 times.
✓ Branch 1 taken 3 times.
|
6 | if (rank < rem) { |
| 90 | x_end += 1; | ||
| 91 | } | ||
| 92 | |||
| 93 | 6 | double local_sum = 0.0; | |
| 94 |
1/2✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
|
6 | if (x_start <= x_end) { |
| 95 | 6 | local_sum = ComputeLocalSum(x_start, x_end, static_cast<int>(in[4]), static_cast<int>(in[5]), in[0], in[2], hx, hy); | |
| 96 | } | ||
| 97 | |||
| 98 | 6 | double global_sum = 0.0; | |
| 99 | 6 | MPI_Reduce(&local_sum, &global_sum, 1, MPI_DOUBLE, MPI_SUM, 0, MPI_COMM_WORLD); | |
| 100 | |||
| 101 |
2/2✓ Branch 0 taken 3 times.
✓ Branch 1 taken 3 times.
|
6 | if (rank == 0) { |
| 102 | 3 | GetOutput() = global_sum * hx * hy; | |
| 103 | } | ||
| 104 | |||
| 105 | 6 | MPI_Bcast(&GetOutput(), 1, MPI_DOUBLE, 0, MPI_COMM_WORLD); | |
| 106 | 6 | return true; | |
| 107 | } | ||
| 108 | |||
| 109 | 6 | bool LifanovKTrapezoidMethodMPI::PostProcessingImpl() { | |
| 110 | 6 | return true; | |
| 111 | } | ||
| 112 | |||
| 113 | } // namespace lifanov_k_trapezoid_method | ||
| 114 |