| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include "spichek_d_simpson_integral/mpi/include/ops_mpi.hpp" | ||
| 2 | |||
| 3 | #include <mpi.h> | ||
| 4 | |||
| 5 | #include <cmath> | ||
| 6 | |||
| 7 | // Explicitly include common.hpp to satisfy clang-tidy misc-include-cleaner for InType/OutType | ||
| 8 | #include "spichek_d_simpson_integral/common/include/common.hpp" | ||
| 9 | |||
| 10 | namespace spichek_d_simpson_integral { | ||
| 11 | |||
| 12 | namespace { | ||
| 13 | // Helper to reduce cognitive complexity and avoid nested ternary operators | ||
| 14 | int GetSimpsonWeight(int index, int n) { | ||
| 15 | 2316 | if (index == 0 || index == n) { | |
| 16 | return 1; | ||
| 17 | } | ||
| 18 |
4/4✓ Branch 0 taken 35 times.
✓ Branch 1 taken 32 times.
✓ Branch 2 taken 1085 times.
✓ Branch 3 taken 1012 times.
|
2164 | return (index % 2 == 0) ? 2 : 4; |
| 19 | } | ||
| 20 | } // namespace | ||
| 21 | |||
| 22 | 6 | SpichekDSimpsonIntegralMPI::SpichekDSimpsonIntegralMPI(const InType &in) { | |
| 23 | SetTypeOfTask(GetStaticTypeOfTask()); | ||
| 24 | 6 | GetInput() = in; | |
| 25 | GetOutput() = 0; | ||
| 26 | 6 | } | |
| 27 | |||
| 28 | 6 | bool SpichekDSimpsonIntegralMPI::ValidationImpl() { | |
| 29 |
2/4✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 6 times.
|
6 | return (GetInput() > 0) && (GetInput() % 2 == 0); |
| 30 | } | ||
| 31 | |||
| 32 | 6 | bool SpichekDSimpsonIntegralMPI::PreProcessingImpl() { | |
| 33 | 6 | GetOutput() = 0; | |
| 34 | 6 | return true; | |
| 35 | } | ||
| 36 | |||
| 37 | 6 | bool SpichekDSimpsonIntegralMPI::RunImpl() { | |
| 38 | 6 | int rank = 0; | |
| 39 | 6 | int size = 0; | |
| 40 | 6 | MPI_Comm_rank(MPI_COMM_WORLD, &rank); | |
| 41 | 6 | MPI_Comm_size(MPI_COMM_WORLD, &size); | |
| 42 | |||
| 43 | 6 | int n = 0; | |
| 44 |
2/2✓ Branch 0 taken 3 times.
✓ Branch 1 taken 3 times.
|
6 | if (rank == 0) { |
| 45 | 3 | n = GetInput(); | |
| 46 | } | ||
| 47 | |||
| 48 | 6 | MPI_Bcast(&n, 1, MPI_INT, 0, MPI_COMM_WORLD); | |
| 49 | |||
| 50 |
2/4✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 6 times.
|
6 | if (n <= 0 || n % 2 != 0) { |
| 51 | ✗ | GetOutput() = 0; | |
| 52 | ✗ | return true; | |
| 53 | } | ||
| 54 | |||
| 55 | 6 | const double h = 1.0 / n; | |
| 56 | 6 | double local_sum = 0.0; | |
| 57 | |||
| 58 | // Distribute rows (i) among processes | ||
| 59 |
2/2✓ Branch 0 taken 73 times.
✓ Branch 1 taken 6 times.
|
79 | for (int i = rank; i <= n; i += size) { |
| 60 |
2/2✓ Branch 0 taken 67 times.
✓ Branch 1 taken 6 times.
|
73 | const double x = i * h; |
| 61 | const int wx = GetSimpsonWeight(i, n); | ||
| 62 | |||
| 63 |
2/2✓ Branch 0 taken 2243 times.
✓ Branch 1 taken 73 times.
|
2316 | for (int j = 0; j <= n; ++j) { |
| 64 |
2/2✓ Branch 0 taken 2097 times.
✓ Branch 1 taken 146 times.
|
2243 | const double y = j * h; |
| 65 | const int wy = GetSimpsonWeight(j, n); | ||
| 66 | |||
| 67 | 2243 | local_sum += wx * wy * (x * x + y * y); | |
| 68 | } | ||
| 69 | } | ||
| 70 | |||
| 71 | 6 | double global_sum = 0.0; | |
| 72 | 6 | MPI_Allreduce(&local_sum, &global_sum, 1, MPI_DOUBLE, MPI_SUM, MPI_COMM_WORLD); | |
| 73 | |||
| 74 | 6 | const double result = global_sum * h * h / 9.0; | |
| 75 | |||
| 76 | 6 | GetOutput() = static_cast<OutType>(std::round(result)); | |
| 77 | 6 | return true; | |
| 78 | } | ||
| 79 | |||
| 80 | 6 | bool SpichekDSimpsonIntegralMPI::PostProcessingImpl() { | |
| 81 | 6 | return true; | |
| 82 | } | ||
| 83 | |||
| 84 | } // namespace spichek_d_simpson_integral | ||
| 85 |