| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include "tochilin_e_integration_trapezoid/mpi/include/ops_mpi.hpp" | ||
| 2 | |||
| 3 | #include <mpi.h> | ||
| 4 | |||
| 5 | #include <array> | ||
| 6 | #include <cmath> | ||
| 7 | |||
| 8 | #include "tochilin_e_integration_trapezoid/common/include/common.hpp" | ||
| 9 | |||
| 10 | namespace tochilin_e_integration_trapezoid { | ||
| 11 | |||
| 12 |
1/2✓ Branch 1 taken 6 times.
✗ Branch 2 not taken.
|
6 | TochilinEIntegrationTrapezoidMPI::TochilinEIntegrationTrapezoidMPI(const InType &in) { |
| 13 | SetTypeOfTask(GetStaticTypeOfTask()); | ||
| 14 | GetInput() = in; | ||
| 15 | 6 | GetOutput() = 0.0; | |
| 16 | 6 | } | |
| 17 | |||
| 18 | 6 | bool TochilinEIntegrationTrapezoidMPI::ValidationImpl() { | |
| 19 | 6 | int rank = 0; | |
| 20 | 6 | MPI_Comm_rank(MPI_COMM_WORLD, &rank); | |
| 21 |
2/2✓ Branch 0 taken 3 times.
✓ Branch 1 taken 3 times.
|
6 | if (rank == 0) { |
| 22 | const auto &input = GetInput(); | ||
| 23 |
3/6✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 3 times.
✗ Branch 5 not taken.
|
3 | return input.num_intervals > 0 && input.lower_bound < input.upper_bound && input.function != nullptr; |
| 24 | } | ||
| 25 | return true; | ||
| 26 | } | ||
| 27 | |||
| 28 | 6 | bool TochilinEIntegrationTrapezoidMPI::PreProcessingImpl() { | |
| 29 | 6 | int rank = 0; | |
| 30 | 6 | MPI_Comm_rank(MPI_COMM_WORLD, &rank); | |
| 31 |
2/2✓ Branch 0 taken 3 times.
✓ Branch 1 taken 3 times.
|
6 | if (rank == 0) { |
| 32 | const auto &input = GetInput(); | ||
| 33 | 3 | lower_bound_ = input.lower_bound; | |
| 34 | 3 | upper_bound_ = input.upper_bound; | |
| 35 | 3 | num_intervals_ = input.num_intervals; | |
| 36 | 3 | function_ = input.function; | |
| 37 | } | ||
| 38 | 6 | result_ = 0.0; | |
| 39 | 6 | return true; | |
| 40 | } | ||
| 41 | |||
| 42 | 6 | bool TochilinEIntegrationTrapezoidMPI::RunImpl() { | |
| 43 | 6 | int rank = 0; | |
| 44 | 6 | int size = 0; | |
| 45 | 6 | MPI_Comm_rank(MPI_COMM_WORLD, &rank); | |
| 46 | 6 | MPI_Comm_size(MPI_COMM_WORLD, &size); | |
| 47 | |||
| 48 | 6 | std::array<double, 3> params = {0.0, 0.0, 0.0}; | |
| 49 |
2/2✓ Branch 0 taken 3 times.
✓ Branch 1 taken 3 times.
|
6 | if (rank == 0) { |
| 50 | 3 | params[0] = lower_bound_; | |
| 51 | 3 | params[1] = upper_bound_; | |
| 52 | 3 | params[2] = static_cast<double>(num_intervals_); | |
| 53 | } | ||
| 54 | |||
| 55 | 6 | MPI_Bcast(params.data(), 3, MPI_DOUBLE, 0, MPI_COMM_WORLD); | |
| 56 | |||
| 57 | 6 | double local_lower = params[0]; | |
| 58 | 6 | double local_upper = params[1]; | |
| 59 | 6 | int total_intervals = static_cast<int>(params[2]); | |
| 60 | |||
| 61 | 6 | function_ = GetInput().function; | |
| 62 | |||
| 63 | 6 | double step = (local_upper - local_lower) / total_intervals; | |
| 64 | |||
| 65 | 6 | int base_intervals = total_intervals / size; | |
| 66 | 6 | int remainder = total_intervals % size; | |
| 67 | |||
| 68 | int local_start = 0; | ||
| 69 | int local_count = 0; | ||
| 70 | |||
| 71 |
2/2✓ Branch 0 taken 3 times.
✓ Branch 1 taken 3 times.
|
6 | if (rank == 0) { |
| 72 | local_start = 0; | ||
| 73 |
1/2✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
|
3 | local_count = base_intervals + (rank < remainder ? 1 : 0); |
| 74 | |||
| 75 | int current_pos = local_count; | ||
| 76 |
2/2✓ Branch 0 taken 3 times.
✓ Branch 1 taken 3 times.
|
6 | for (int proc = 1; proc < size; ++proc) { |
| 77 |
1/2✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
|
3 | int count = base_intervals + (proc < remainder ? 1 : 0); |
| 78 | 3 | std::array<double, 3> send_data = {static_cast<double>(current_pos), static_cast<double>(count), step}; | |
| 79 | 3 | MPI_Send(send_data.data(), 3, MPI_DOUBLE, proc, 0, MPI_COMM_WORLD); | |
| 80 | 3 | current_pos += count; | |
| 81 | } | ||
| 82 | } else { | ||
| 83 | 3 | std::array<double, 3> recv_data = {0.0, 0.0, 0.0}; | |
| 84 | 3 | MPI_Recv(recv_data.data(), 3, MPI_DOUBLE, 0, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE); | |
| 85 | 3 | local_start = static_cast<int>(recv_data[0]); | |
| 86 | 3 | local_count = static_cast<int>(recv_data[1]); | |
| 87 | 3 | step = recv_data[2]; | |
| 88 | } | ||
| 89 | |||
| 90 | 6 | double local_sum = 0.0; | |
| 91 | |||
| 92 |
2/2✓ Branch 0 taken 3000 times.
✓ Branch 1 taken 6 times.
|
3006 | for (int i = local_start; i < local_start + local_count; ++i) { |
| 93 | 3000 | double x_left = local_lower + (i * step); | |
| 94 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 3000 times.
|
3000 | double x_right = local_lower + ((i + 1) * step); |
| 95 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 3000 times.
|
6000 | local_sum += ((function_(x_left) + function_(x_right)) / 2.0) * step; |
| 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 | result_ = global_sum; | |
| 103 | } | ||
| 104 | |||
| 105 | 6 | return true; | |
| 106 | } | ||
| 107 | |||
| 108 | 6 | bool TochilinEIntegrationTrapezoidMPI::PostProcessingImpl() { | |
| 109 | 6 | MPI_Bcast(&result_, 1, MPI_DOUBLE, 0, MPI_COMM_WORLD); | |
| 110 | 6 | GetOutput() = result_; | |
| 111 | 6 | return true; | |
| 112 | } | ||
| 113 | |||
| 114 | } // namespace tochilin_e_integration_trapezoid | ||
| 115 |