| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include "viderman_a_elem_vec_sum/mpi/include/ops_mpi.hpp" | ||
| 2 | |||
| 3 | #include <mpi.h> | ||
| 4 | |||
| 5 | #include <cstddef> | ||
| 6 | #include <vector> | ||
| 7 | |||
| 8 | #include "viderman_a_elem_vec_sum/common/include/common.hpp" | ||
| 9 | |||
| 10 | namespace viderman_a_elem_vec_sum { | ||
| 11 | |||
| 12 |
1/2✓ Branch 1 taken 32 times.
✗ Branch 2 not taken.
|
32 | VidermanAElemVecSumMPI::VidermanAElemVecSumMPI(const InType &in) { |
| 13 | SetTypeOfTask(GetStaticTypeOfTask()); | ||
| 14 |
1/2✓ Branch 1 taken 32 times.
✗ Branch 2 not taken.
|
32 | GetInput() = in; |
| 15 | 32 | GetOutput() = 0.0; | |
| 16 | 32 | } | |
| 17 | |||
| 18 | 32 | bool VidermanAElemVecSumMPI::ValidationImpl() { | |
| 19 | 32 | return (GetOutput() == 0.0); | |
| 20 | } | ||
| 21 | |||
| 22 | 32 | bool VidermanAElemVecSumMPI::PreProcessingImpl() { | |
| 23 | 32 | return true; | |
| 24 | } | ||
| 25 | |||
| 26 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 30 times.
|
32 | bool VidermanAElemVecSumMPI::RunImpl() { |
| 27 | const auto &input_vector = GetInput(); | ||
| 28 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 30 times.
|
32 | if (input_vector.empty()) { |
| 29 | 2 | GetOutput() = 0.0; | |
| 30 | 2 | return true; | |
| 31 | } | ||
| 32 | |||
| 33 | 30 | int int_rank = 0; | |
| 34 | 30 | int total_processes = 0; | |
| 35 | 30 | MPI_Comm_rank(MPI_COMM_WORLD, &int_rank); | |
| 36 | 30 | MPI_Comm_size(MPI_COMM_WORLD, &total_processes); | |
| 37 | |||
| 38 |
2/2✓ Branch 0 taken 7 times.
✓ Branch 1 taken 23 times.
|
30 | const auto my_rank = static_cast<size_t>(int_rank); |
| 39 | const size_t element_count = input_vector.size(); | ||
| 40 | 30 | const auto total_procs_size = static_cast<size_t>(total_processes); | |
| 41 | |||
| 42 | 30 | const size_t base_chunk = element_count / total_procs_size; | |
| 43 | 30 | const size_t remaining_elements = element_count % total_procs_size; | |
| 44 | |||
| 45 | size_t my_chunk_size = base_chunk; | ||
| 46 |
2/2✓ Branch 0 taken 7 times.
✓ Branch 1 taken 23 times.
|
30 | if (my_rank < remaining_elements) { |
| 47 | 7 | my_chunk_size = base_chunk + 1; | |
| 48 | } | ||
| 49 | |||
| 50 | 30 | std::vector<double> local_data(my_chunk_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> send_counts(total_procs_size); |
| 52 |
1/4✓ Branch 1 taken 30 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
|
30 | std::vector<int> displacements(total_procs_size); |
| 53 | |||
| 54 | size_t displacement = 0; | ||
| 55 |
2/2✓ Branch 0 taken 60 times.
✓ Branch 1 taken 30 times.
|
90 | for (size_t i = 0; i < total_procs_size; ++i) { |
| 56 | size_t chunk = base_chunk; | ||
| 57 |
2/2✓ Branch 0 taken 14 times.
✓ Branch 1 taken 46 times.
|
60 | if (i < remaining_elements) { |
| 58 | 14 | chunk = base_chunk + 1; | |
| 59 | } | ||
| 60 | 60 | send_counts[i] = static_cast<int>(chunk); | |
| 61 | 60 | displacements[i] = static_cast<int>(displacement); | |
| 62 | 60 | displacement += chunk; | |
| 63 | } | ||
| 64 | |||
| 65 |
1/2✓ Branch 1 taken 30 times.
✗ Branch 2 not taken.
|
30 | MPI_Scatterv(input_vector.data(), send_counts.data(), displacements.data(), MPI_DOUBLE, local_data.data(), |
| 66 | static_cast<int>(my_chunk_size), MPI_DOUBLE, 0, MPI_COMM_WORLD); | ||
| 67 | |||
| 68 | 30 | double process_sum = 0.0; | |
| 69 |
2/2✓ Branch 0 taken 1110257 times.
✓ Branch 1 taken 30 times.
|
1110287 | for (double value : local_data) { |
| 70 | 1110257 | process_sum += value; | |
| 71 | } | ||
| 72 | |||
| 73 | 30 | double final_result = 0.0; | |
| 74 |
1/2✓ Branch 1 taken 30 times.
✗ Branch 2 not taken.
|
30 | MPI_Allreduce(&process_sum, &final_result, 1, MPI_DOUBLE, MPI_SUM, MPI_COMM_WORLD); |
| 75 | |||
| 76 |
1/2✓ Branch 0 taken 30 times.
✗ Branch 1 not taken.
|
30 | GetOutput() = final_result; |
| 77 | return true; | ||
| 78 | } | ||
| 79 | 32 | bool VidermanAElemVecSumMPI::PostProcessingImpl() { | |
| 80 | 32 | return true; | |
| 81 | } | ||
| 82 | |||
| 83 | } // namespace viderman_a_elem_vec_sum | ||
| 84 |