| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include "../include/ops_mpi.hpp" | ||
| 2 | |||
| 3 | #include <mpi.h> | ||
| 4 | |||
| 5 | #include <numeric> | ||
| 6 | #include <vector> | ||
| 7 | |||
| 8 | #include "../../common/include/common.hpp" | ||
| 9 | |||
| 10 | namespace shekhirev_v_custom_reduce_mpi { | ||
| 11 | |||
| 12 | 8 | CustomReduceMPI::CustomReduceMPI(const shekhirev_v_custom_reduce::InType &in) { | |
| 13 | SetTypeOfTask(GetStaticTypeOfTask()); | ||
| 14 | 8 | GetInput() = in; | |
| 15 | GetOutput() = 0; | ||
| 16 | 8 | } | |
| 17 | |||
| 18 | 8 | bool CustomReduceMPI::ValidationImpl() { | |
| 19 | 8 | return GetInput() > 0; | |
| 20 | } | ||
| 21 | |||
| 22 | 8 | bool CustomReduceMPI::PreProcessingImpl() { | |
| 23 | 8 | shekhirev_v_custom_reduce::InType input_val = 0; | |
| 24 | 8 | int rank = 0; | |
| 25 | 8 | MPI_Comm_rank(MPI_COMM_WORLD, &rank); | |
| 26 | |||
| 27 |
2/2✓ Branch 0 taken 4 times.
✓ Branch 1 taken 4 times.
|
8 | if (rank == 0) { |
| 28 | 4 | input_val = GetInput(); | |
| 29 | } | ||
| 30 | |||
| 31 | 8 | MPI_Bcast(&input_val, 1, MPI_INT, 0, MPI_COMM_WORLD); | |
| 32 | |||
| 33 | 8 | GetInput() = input_val; | |
| 34 | 8 | GetOutput() = GetInput() + 1; | |
| 35 | 8 | return true; | |
| 36 | } | ||
| 37 | |||
| 38 | 8 | bool CustomReduceMPI::RunImpl() { | |
| 39 | 8 | int rank = 0; | |
| 40 | 8 | int size = 0; | |
| 41 | 8 | MPI_Comm_rank(MPI_COMM_WORLD, &rank); | |
| 42 | 8 | MPI_Comm_size(MPI_COMM_WORLD, &size); | |
| 43 | |||
| 44 | 8 | const int count_per_process = GetInput() / size; | |
| 45 | 8 | const int remainder = GetInput() % size; | |
| 46 |
2/2✓ Branch 0 taken 5 times.
✓ Branch 1 taken 3 times.
|
8 | const int local_count = count_per_process + (rank < remainder ? 1 : 0); |
| 47 | |||
| 48 | 8 | const std::vector<int> local_vec(local_count, 1); | |
| 49 | 8 | const int local_sum = std::accumulate(local_vec.begin(), local_vec.end(), 0); | |
| 50 | |||
| 51 |
2/2✓ Branch 0 taken 4 times.
✓ Branch 1 taken 4 times.
|
8 | if (rank == 0) { |
| 52 | int global_sum = local_sum; | ||
| 53 | |||
| 54 |
2/2✓ Branch 0 taken 4 times.
✓ Branch 1 taken 4 times.
|
8 | for (int i = 1; i < size; ++i) { |
| 55 | 4 | int recv_val = 0; | |
| 56 | MPI_Status status; | ||
| 57 |
1/2✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
|
4 | MPI_Recv(&recv_val, 1, MPI_INT, i, 0, MPI_COMM_WORLD, &status); |
| 58 | 4 | global_sum += recv_val; | |
| 59 | } | ||
| 60 | |||
| 61 | 4 | GetOutput() = global_sum; | |
| 62 | } else { | ||
| 63 |
1/2✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
|
4 | MPI_Send(&local_sum, 1, MPI_INT, 0, 0, MPI_COMM_WORLD); |
| 64 | } | ||
| 65 | |||
| 66 | 8 | return true; | |
| 67 | } | ||
| 68 | |||
| 69 | 8 | bool CustomReduceMPI::PostProcessingImpl() { | |
| 70 | 8 | return true; | |
| 71 | } | ||
| 72 | |||
| 73 | } // namespace shekhirev_v_custom_reduce_mpi | ||
| 74 |