| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include "lifanov_k_allreduce/mpi/include/ops_mpi.hpp" | ||
| 2 | |||
| 3 | #include <mpi.h> | ||
| 4 | |||
| 5 | #include <vector> | ||
| 6 | |||
| 7 | #include "lifanov_k_allreduce/common/include/common.hpp" | ||
| 8 | |||
| 9 | namespace lifanov_k_allreduce { | ||
| 10 | |||
| 11 |
1/2✓ Branch 1 taken 6 times.
✗ Branch 2 not taken.
|
6 | LifanovKAllReduceMPI::LifanovKAllReduceMPI(const InType &in) { |
| 12 | SetTypeOfTask(GetStaticTypeOfTask()); | ||
| 13 |
1/2✓ Branch 1 taken 6 times.
✗ Branch 2 not taken.
|
6 | GetInput() = in; |
| 14 | GetOutput().clear(); | ||
| 15 | 6 | } | |
| 16 | |||
| 17 | 6 | bool LifanovKAllReduceMPI::ValidationImpl() { | |
| 18 | 6 | return (!GetInput().empty()); | |
| 19 | } | ||
| 20 | |||
| 21 | 6 | bool LifanovKAllReduceMPI::PreProcessingImpl() { | |
| 22 | 6 | GetOutput().resize(1); | |
| 23 | 6 | return true; | |
| 24 | } | ||
| 25 | |||
| 26 | 6 | bool LifanovKAllReduceMPI::RunImpl() { | |
| 27 | 6 | int current_rank = 0; | |
| 28 | 6 | int num_processes = 0; | |
| 29 | 6 | MPI_Comm_rank(MPI_COMM_WORLD, ¤t_rank); | |
| 30 | 6 | MPI_Comm_size(MPI_COMM_WORLD, &num_processes); | |
| 31 | |||
| 32 | 6 | int input_size = 0; | |
| 33 |
2/2✓ Branch 0 taken 3 times.
✓ Branch 1 taken 3 times.
|
6 | if (current_rank == 0) { |
| 34 | 3 | input_size = static_cast<int>(GetInput().size()); | |
| 35 | } | ||
| 36 | |||
| 37 | 6 | MPI_Bcast(&input_size, 1, MPI_INT, 0, MPI_COMM_WORLD); | |
| 38 | |||
| 39 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
|
6 | if (input_size == 0) { |
| 40 | ✗ | GetOutput()[0] = 0; | |
| 41 | ✗ | return true; | |
| 42 | } | ||
| 43 | |||
| 44 | 6 | int base_chunk_size = input_size / num_processes; | |
| 45 | 6 | int extra_elements = input_size % num_processes; | |
| 46 | |||
| 47 |
2/2✓ Branch 0 taken 4 times.
✓ Branch 1 taken 2 times.
|
6 | int local_chunk_size = base_chunk_size + (current_rank < extra_elements ? 1 : 0); |
| 48 | 6 | local_input_.resize(local_chunk_size); | |
| 49 | |||
| 50 | 6 | std::vector<int> send_counts(num_processes, 0); | |
| 51 |
1/4✓ Branch 1 taken 6 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
|
6 | std::vector<int> displacements(num_processes, 0); |
| 52 | |||
| 53 |
2/2✓ Branch 0 taken 3 times.
✓ Branch 1 taken 3 times.
|
6 | if (current_rank == 0) { |
| 54 | int offset = 0; | ||
| 55 |
2/2✓ Branch 0 taken 6 times.
✓ Branch 1 taken 3 times.
|
9 | for (int i = 0; i < num_processes; ++i) { |
| 56 |
2/2✓ Branch 0 taken 4 times.
✓ Branch 1 taken 2 times.
|
10 | send_counts[i] = base_chunk_size + (i < extra_elements ? 1 : 0); |
| 57 | 6 | displacements[i] = offset; | |
| 58 | 6 | offset += send_counts[i]; | |
| 59 | } | ||
| 60 | } | ||
| 61 | |||
| 62 |
3/4✓ Branch 0 taken 3 times.
✓ Branch 1 taken 3 times.
✓ Branch 3 taken 6 times.
✗ Branch 4 not taken.
|
9 | MPI_Scatterv(current_rank == 0 ? GetInput().data() : nullptr, send_counts.data(), displacements.data(), MPI_INT, |
| 63 | local_input_.data(), local_chunk_size, MPI_INT, 0, MPI_COMM_WORLD); | ||
| 64 | |||
| 65 | 6 | int local_sum = 0; | |
| 66 |
2/2✓ Branch 0 taken 184 times.
✓ Branch 1 taken 6 times.
|
190 | for (int i = 0; i < local_chunk_size; ++i) { |
| 67 | 184 | local_sum += local_input_[i]; | |
| 68 | } | ||
| 69 | |||
| 70 | 6 | int total_sum = 0; | |
| 71 | |||
| 72 |
2/2✓ Branch 0 taken 3 times.
✓ Branch 1 taken 3 times.
|
6 | if (current_rank == 0) { |
| 73 | 3 | total_sum = local_sum; | |
| 74 |
2/2✓ Branch 0 taken 3 times.
✓ Branch 1 taken 3 times.
|
6 | for (int i = 1; i < num_processes; ++i) { |
| 75 | 3 | int received_sum = 0; | |
| 76 |
1/2✓ Branch 1 taken 3 times.
✗ Branch 2 not taken.
|
3 | MPI_Recv(&received_sum, 1, MPI_INT, i, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE); |
| 77 | 3 | total_sum += received_sum; | |
| 78 | } | ||
| 79 | } else { | ||
| 80 |
1/2✓ Branch 1 taken 3 times.
✗ Branch 2 not taken.
|
3 | MPI_Send(&local_sum, 1, MPI_INT, 0, 0, MPI_COMM_WORLD); |
| 81 | } | ||
| 82 | |||
| 83 |
1/2✓ Branch 1 taken 6 times.
✗ Branch 2 not taken.
|
6 | MPI_Bcast(&total_sum, 1, MPI_INT, 0, MPI_COMM_WORLD); |
| 84 |
1/2✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
|
6 | GetOutput()[0] = total_sum; |
| 85 | |||
| 86 | return true; | ||
| 87 | } | ||
| 88 | |||
| 89 | 6 | bool LifanovKAllReduceMPI::PostProcessingImpl() { | |
| 90 | 6 | return true; | |
| 91 | } | ||
| 92 | |||
| 93 | } // namespace lifanov_k_allreduce | ||
| 94 |