| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include "gusev_d_star/mpi/include/ops_mpi.hpp" | ||
| 2 | |||
| 3 | #include <mpi.h> | ||
| 4 | |||
| 5 | #include <array> | ||
| 6 | |||
| 7 | #include "gusev_d_star/common/include/common.hpp" | ||
| 8 | |||
| 9 | namespace gusev_d_star { | ||
| 10 | |||
| 11 | constexpr int kTagTask = 0; | ||
| 12 | constexpr int kTagResult = 1; | ||
| 13 | constexpr int kHubRank = 0; | ||
| 14 | |||
| 15 | 42 | GusevDStarMPI::GusevDStarMPI(const InType &in) { | |
| 16 | SetTypeOfTask(GetStaticTypeOfTask()); | ||
| 17 | 42 | GetInput() = in; | |
| 18 | GetOutput() = 0; | ||
| 19 | 42 | } | |
| 20 | |||
| 21 | 42 | bool GusevDStarMPI::ValidationImpl() { | |
| 22 | 42 | return (GetInput() > 0); | |
| 23 | } | ||
| 24 | |||
| 25 | 42 | bool GusevDStarMPI::PreProcessingImpl() { | |
| 26 | 42 | GetOutput() = 2 * GetInput(); | |
| 27 | 42 | return true; | |
| 28 | } | ||
| 29 | |||
| 30 | 42 | bool GusevDStarMPI::RunImpl() { | |
| 31 | 42 | auto input = GetInput(); | |
| 32 | 42 | int rank = 0; | |
| 33 | 42 | int size = 0; | |
| 34 | |||
| 35 | 42 | MPI_Comm_rank(MPI_COMM_WORLD, &rank); | |
| 36 | 42 | MPI_Comm_size(MPI_COMM_WORLD, &size); | |
| 37 | |||
| 38 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 42 times.
|
42 | if (size == 1) { |
| 39 | ✗ | volatile int local_res = 0; | |
| 40 | ✗ | for (int i = 0; i < input; i++) { | |
| 41 | ✗ | for (int j = 0; j < input; j++) { | |
| 42 | ✗ | for (int k = 0; k < input; k++) { | |
| 43 | ✗ | local_res += (i + j + k); | |
| 44 | ✗ | local_res -= (i + j + k); | |
| 45 | } | ||
| 46 | } | ||
| 47 | } | ||
| 48 | ✗ | GetOutput() += local_res; | |
| 49 | ✗ | return true; | |
| 50 | } | ||
| 51 | |||
| 52 |
2/2✓ Branch 0 taken 21 times.
✓ Branch 1 taken 21 times.
|
42 | if (rank == kHubRank) { |
| 53 | 21 | RunAsMaster(size, input); | |
| 54 | } else { | ||
| 55 | 21 | RunAsWorker(input); | |
| 56 | } | ||
| 57 | |||
| 58 | return true; | ||
| 59 | } | ||
| 60 | |||
| 61 | 42 | bool GusevDStarMPI::PostProcessingImpl() { | |
| 62 | 42 | GetOutput() -= GetInput(); | |
| 63 | 42 | return true; | |
| 64 | } | ||
| 65 | |||
| 66 | 21 | void GusevDStarMPI::RunAsMaster(int size, int input) { | |
| 67 | 21 | int workers_count = size - 1; | |
| 68 | 21 | int chunk = input / workers_count; | |
| 69 | 21 | int remainder = input % workers_count; | |
| 70 | int current_start_index = 0; | ||
| 71 | |||
| 72 |
2/2✓ Branch 0 taken 21 times.
✓ Branch 1 taken 21 times.
|
42 | for (int dst_rank = 1; dst_rank < size; dst_rank++) { |
| 73 |
1/2✓ Branch 0 taken 21 times.
✗ Branch 1 not taken.
|
21 | int count = chunk + (dst_rank <= remainder ? 1 : 0); |
| 74 | |||
| 75 | 21 | std::array<int, 2> task_data = {current_start_index, count}; | |
| 76 | |||
| 77 | 21 | MPI_Send(task_data.data(), 2, MPI_INT, dst_rank, kTagTask, MPI_COMM_WORLD); | |
| 78 | 21 | current_start_index += count; | |
| 79 | } | ||
| 80 | |||
| 81 |
2/2✓ Branch 0 taken 21 times.
✓ Branch 1 taken 21 times.
|
42 | for (int src_rank = 1; src_rank < size; src_rank++) { |
| 82 | 21 | int worker_result = 0; | |
| 83 | MPI_Status status; | ||
| 84 | |||
| 85 | 21 | MPI_Recv(&worker_result, 1, MPI_INT, src_rank, kTagResult, MPI_COMM_WORLD, &status); | |
| 86 | 21 | GetOutput() += worker_result; | |
| 87 | } | ||
| 88 | 21 | } | |
| 89 | |||
| 90 | 21 | void GusevDStarMPI::RunAsWorker(int input) { | |
| 91 | 21 | std::array<int, 2> task_data = {}; | |
| 92 | MPI_Status status; | ||
| 93 | |||
| 94 | 21 | MPI_Recv(task_data.data(), 2, MPI_INT, kHubRank, kTagTask, MPI_COMM_WORLD, &status); | |
| 95 | |||
| 96 | 21 | int start_i = task_data[0]; | |
| 97 | 21 | int count_i = task_data[1]; | |
| 98 | 21 | int end_i = start_i + count_i; | |
| 99 | |||
| 100 | 21 | volatile int local_res = 0; | |
| 101 | |||
| 102 |
2/2✓ Branch 0 taken 2080 times.
✓ Branch 1 taken 21 times.
|
2101 | for (int i = start_i; i < end_i; i++) { |
| 103 |
2/2✓ Branch 0 taken 408292 times.
✓ Branch 1 taken 2080 times.
|
410372 | for (int j = 0; j < input; j++) { |
| 104 |
2/2✓ Branch 0 taken 97409602 times.
✓ Branch 1 taken 408292 times.
|
97817894 | for (int k = 0; k < input; k++) { |
| 105 | 97409602 | local_res += (i + j + k); | |
| 106 | 97409602 | local_res -= (i + j + k); | |
| 107 | } | ||
| 108 | } | ||
| 109 | } | ||
| 110 | |||
| 111 | 21 | int res_to_send = local_res; | |
| 112 | |||
| 113 | 21 | MPI_Send(static_cast<const void *>(&res_to_send), 1, MPI_INT, kHubRank, kTagResult, MPI_COMM_WORLD); | |
| 114 | 21 | } | |
| 115 | |||
| 116 | } // namespace gusev_d_star | ||
| 117 |