| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include "dergynov_s_trapezoid_integration/mpi/include/ops_mpi.hpp" | ||
| 2 | |||
| 3 | #include <mpi.h> | ||
| 4 | |||
| 5 | #include <algorithm> | ||
| 6 | |||
| 7 | #include "dergynov_s_trapezoid_integration/common/include/common.hpp" | ||
| 8 | |||
| 9 | namespace dergynov_s_trapezoid_integration { | ||
| 10 | |||
| 11 | ✗ | DergynovSTrapezoidIntegrationMPI::DergynovSTrapezoidIntegrationMPI(const InType &in) { | |
| 12 | SetTypeOfTask(GetStaticTypeOfTask()); | ||
| 13 | ✗ | GetInput() = in; | |
| 14 | GetOutput() = 0.0; | ||
| 15 | ✗ | } | |
| 16 | |||
| 17 | ✗ | bool DergynovSTrapezoidIntegrationMPI::ValidationImpl() { | |
| 18 | const auto &in = GetInput(); | ||
| 19 | ✗ | return (in.n > 0) && (in.a < in.b); | |
| 20 | } | ||
| 21 | |||
| 22 | ✗ | bool DergynovSTrapezoidIntegrationMPI::PreProcessingImpl() { | |
| 23 | ✗ | GetOutput() = 0.0; | |
| 24 | ✗ | return true; | |
| 25 | } | ||
| 26 | |||
| 27 | ✗ | bool DergynovSTrapezoidIntegrationMPI::RunImpl() { | |
| 28 | ✗ | int rank = 0; | |
| 29 | ✗ | int size = 0; | |
| 30 | ✗ | MPI_Comm_rank(MPI_COMM_WORLD, &rank); | |
| 31 | ✗ | MPI_Comm_size(MPI_COMM_WORLD, &size); | |
| 32 | |||
| 33 | ✗ | InType in = (rank == 0) ? GetInput() : InType{}; | |
| 34 | ✗ | MPI_Bcast(&in, sizeof(InType), MPI_BYTE, 0, MPI_COMM_WORLD); | |
| 35 | ✗ | GetInput() = in; | |
| 36 | |||
| 37 | ✗ | const double a = in.a; | |
| 38 | ✗ | const double b = in.b; | |
| 39 | ✗ | const int n = in.n; | |
| 40 | |||
| 41 | ✗ | if (n <= 0 || !(a < b)) { | |
| 42 | ✗ | if (rank == 0) { | |
| 43 | ✗ | GetOutput() = 0.0; | |
| 44 | } | ||
| 45 | ✗ | return true; | |
| 46 | } | ||
| 47 | |||
| 48 | ✗ | int base = n / size; | |
| 49 | ✗ | int rem = n % size; | |
| 50 | ✗ | int local_n = base + (rank < rem ? 1 : 0); | |
| 51 | ✗ | int start = (rank * base) + std::min(rank, rem); | |
| 52 | ✗ | int end = start + local_n; | |
| 53 | |||
| 54 | ✗ | const double h = (b - a) / static_cast<double>(n); | |
| 55 | |||
| 56 | ✗ | double local_sum = 0.0; | |
| 57 | ✗ | for (int i = start; i < end; ++i) { | |
| 58 | ✗ | double x1 = a + (h * i); | |
| 59 | ✗ | double x2 = a + (h * (i + 1)); | |
| 60 | ✗ | local_sum += 0.5 * (Function(x1, in.func_id) + Function(x2, in.func_id)) * h; | |
| 61 | } | ||
| 62 | |||
| 63 | ✗ | double global_sum = 0.0; | |
| 64 | ✗ | MPI_Reduce(&local_sum, &global_sum, 1, MPI_DOUBLE, MPI_SUM, 0, MPI_COMM_WORLD); | |
| 65 | ✗ | MPI_Bcast(&global_sum, 1, MPI_DOUBLE, 0, MPI_COMM_WORLD); | |
| 66 | |||
| 67 | ✗ | GetOutput() = global_sum; | |
| 68 | ✗ | return true; | |
| 69 | } | ||
| 70 | |||
| 71 | ✗ | bool DergynovSTrapezoidIntegrationMPI::PostProcessingImpl() { | |
| 72 | ✗ | return true; | |
| 73 | } | ||
| 74 | |||
| 75 | } // namespace dergynov_s_trapezoid_integration | ||
| 76 |