| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include "pikhotskiy_r_elem_vec_sum/mpi/include/ops_mpi.hpp" | ||
| 2 | |||
| 3 | #include <mpi.h> | ||
| 4 | |||
| 5 | #include <cmath> | ||
| 6 | #include <utility> | ||
| 7 | #include <vector> | ||
| 8 | |||
| 9 | #include "pikhotskiy_r_elem_vec_sum/common/include/common.hpp" | ||
| 10 | |||
| 11 | namespace pikhotskiy_r_elem_vec_sum { | ||
| 12 | |||
| 13 |
1/2✓ Branch 1 taken 32 times.
✗ Branch 2 not taken.
|
32 | PikhotskiyRElemVecSumMPI::PikhotskiyRElemVecSumMPI(const InType &in) { |
| 14 | SetTypeOfTask(GetStaticTypeOfTask()); | ||
| 15 | GetInput() = in; | ||
| 16 | 32 | GetOutput() = 0; | |
| 17 | 32 | } | |
| 18 | |||
| 19 | 32 | bool PikhotskiyRElemVecSumMPI::ValidationImpl() { | |
| 20 | const auto &input_data = GetInput(); | ||
| 21 | 32 | bool output_check = (GetOutput() == 0); | |
| 22 |
1/2✓ Branch 0 taken 32 times.
✗ Branch 1 not taken.
|
32 | bool size_check = std::cmp_equal(std::get<1>(input_data).size(), std::get<0>(input_data)); |
| 23 | 32 | return output_check && size_check; | |
| 24 | } | ||
| 25 | |||
| 26 | 32 | bool PikhotskiyRElemVecSumMPI::PreProcessingImpl() { | |
| 27 | 32 | GetOutput() = 0; | |
| 28 | 32 | return true; | |
| 29 | } | ||
| 30 | |||
| 31 | 32 | bool PikhotskiyRElemVecSumMPI::RunImpl() { | |
| 32 | 32 | int my_rank = 0; | |
| 33 | 32 | int world_size = 0; | |
| 34 | 32 | MPI_Comm_rank(MPI_COMM_WORLD, &my_rank); | |
| 35 | 32 | MPI_Comm_size(MPI_COMM_WORLD, &world_size); | |
| 36 | |||
| 37 | 32 | int total_elements = 0; | |
| 38 |
2/2✓ Branch 0 taken 16 times.
✓ Branch 1 taken 16 times.
|
32 | if (my_rank == 0) { |
| 39 | 16 | total_elements = std::get<0>(GetInput()); | |
| 40 | } | ||
| 41 | |||
| 42 | 32 | MPI_Bcast(&total_elements, 1, MPI_INT, 0, MPI_COMM_WORLD); | |
| 43 | |||
| 44 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 30 times.
|
32 | if (total_elements == 0) { |
| 45 | 2 | GetOutput() = 0LL; | |
| 46 | 2 | return true; | |
| 47 | } | ||
| 48 | |||
| 49 | // Calculate distribution of work | ||
| 50 | 30 | std::vector<int> counts_per_process(world_size); | |
| 51 |
1/4✓ Branch 1 taken 30 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
|
30 | std::vector<int> offsets(world_size); |
| 52 | |||
| 53 | 30 | int base_count = total_elements / world_size; | |
| 54 | 30 | int extra_elements = total_elements % world_size; | |
| 55 | |||
| 56 |
2/2✓ Branch 0 taken 60 times.
✓ Branch 1 taken 30 times.
|
90 | for (int proc = 0; proc < world_size; ++proc) { |
| 57 |
4/4✓ Branch 0 taken 44 times.
✓ Branch 1 taken 16 times.
✓ Branch 2 taken 30 times.
✓ Branch 3 taken 30 times.
|
104 | counts_per_process[proc] = base_count + (proc < extra_elements ? 1 : 0); |
| 58 |
2/2✓ Branch 0 taken 30 times.
✓ Branch 1 taken 30 times.
|
60 | offsets[proc] = (proc == 0) ? 0 : offsets[proc - 1] + counts_per_process[proc - 1]; |
| 59 | } | ||
| 60 | |||
| 61 |
1/2✓ Branch 1 taken 30 times.
✗ Branch 2 not taken.
|
30 | int my_count = counts_per_process[my_rank]; |
| 62 |
1/4✓ Branch 1 taken 30 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
|
30 | std::vector<int> my_data(my_count); |
| 63 | |||
| 64 | int *send_buffer = nullptr; | ||
| 65 |
2/2✓ Branch 0 taken 15 times.
✓ Branch 1 taken 15 times.
|
30 | if (my_rank == 0) { |
| 66 | send_buffer = const_cast<int *>(std::get<1>(GetInput()).data()); | ||
| 67 | } | ||
| 68 | |||
| 69 |
1/2✓ Branch 1 taken 30 times.
✗ Branch 2 not taken.
|
30 | MPI_Scatterv(send_buffer, counts_per_process.data(), offsets.data(), MPI_INT, my_data.data(), my_count, MPI_INT, 0, |
| 70 | MPI_COMM_WORLD); | ||
| 71 | |||
| 72 | // Compute local sum | ||
| 73 | 30 | OutType my_sum = 0LL; | |
| 74 |
2/2✓ Branch 0 taken 48 times.
✓ Branch 1 taken 30 times.
|
78 | for (int i = 0; i < my_count; ++i) { |
| 75 | 48 | my_sum += static_cast<OutType>(my_data[i]); | |
| 76 | } | ||
| 77 | |||
| 78 | // Combine all partial sums | ||
| 79 | 30 | OutType global_result = 0LL; | |
| 80 |
1/2✓ Branch 1 taken 30 times.
✗ Branch 2 not taken.
|
30 | MPI_Reduce(&my_sum, &global_result, 1, MPI_LONG_LONG, MPI_SUM, 0, MPI_COMM_WORLD); |
| 81 | |||
| 82 | // Share result with all processes | ||
| 83 |
1/2✓ Branch 1 taken 30 times.
✗ Branch 2 not taken.
|
30 | MPI_Bcast(&global_result, 1, MPI_LONG_LONG, 0, MPI_COMM_WORLD); |
| 84 | |||
| 85 | 30 | GetOutput() = global_result; | |
| 86 | |||
| 87 | // Synchronize before finishing | ||
| 88 |
1/2✓ Branch 1 taken 30 times.
✗ Branch 2 not taken.
|
30 | MPI_Barrier(MPI_COMM_WORLD); |
| 89 | |||
| 90 | return true; | ||
| 91 | } | ||
| 92 | |||
| 93 | 32 | bool PikhotskiyRElemVecSumMPI::PostProcessingImpl() { | |
| 94 | 32 | return true; | |
| 95 | } | ||
| 96 | |||
| 97 | } // namespace pikhotskiy_r_elem_vec_sum | ||
| 98 |