| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include "alekseev_a_min_dist_neigh_elem_vec/mpi/include/ops_mpi.hpp" | ||
| 2 | |||
| 3 | #include <mpi.h> | ||
| 4 | |||
| 5 | #include <cstdlib> | ||
| 6 | #include <limits> | ||
| 7 | #include <tuple> | ||
| 8 | #include <vector> | ||
| 9 | |||
| 10 | #include "alekseev_a_min_dist_neigh_elem_vec/common/include/common.hpp" | ||
| 11 | |||
| 12 | namespace alekseev_a_min_dist_neigh_elem_vec { | ||
| 13 | |||
| 14 |
1/2✓ Branch 1 taken 50 times.
✗ Branch 2 not taken.
|
50 | AlekseevAMinDistNeighElemVecMPI::AlekseevAMinDistNeighElemVecMPI(const InType &in) { |
| 15 | SetTypeOfTask(GetStaticTypeOfTask()); | ||
| 16 |
1/2✓ Branch 1 taken 50 times.
✗ Branch 2 not taken.
|
50 | GetInput() = in; |
| 17 | GetOutput() = std::make_tuple(-1, -1); | ||
| 18 | 50 | } | |
| 19 | |||
| 20 | 50 | bool AlekseevAMinDistNeighElemVecMPI::ValidationImpl() { | |
| 21 | 50 | return true; | |
| 22 | } | ||
| 23 | |||
| 24 | 50 | bool AlekseevAMinDistNeighElemVecMPI::PreProcessingImpl() { | |
| 25 | 50 | return true; | |
| 26 | } | ||
| 27 | |||
| 28 | namespace { | ||
| 29 | 46 | std::vector<int> CalculateSendCountsAndDisplacements(int total_size, int comm_size, std::vector<int> &displacements) { | |
| 30 | 46 | int base_chunk = total_size / comm_size; | |
| 31 | 46 | int remainder = total_size % comm_size; | |
| 32 | |||
| 33 | 46 | std::vector<int> send_counts(comm_size); | |
| 34 |
1/2✓ Branch 1 taken 46 times.
✗ Branch 2 not taken.
|
46 | displacements.resize(comm_size); |
| 35 | int displacement = 0; | ||
| 36 |
2/2✓ Branch 0 taken 92 times.
✓ Branch 1 taken 46 times.
|
138 | for (int i = 0; i < comm_size; i++) { |
| 37 |
2/2✓ Branch 0 taken 88 times.
✓ Branch 1 taken 4 times.
|
180 | send_counts[i] = base_chunk + (i < remainder ? 1 : 0); |
| 38 | 92 | displacements[i] = displacement; | |
| 39 | 92 | displacement += send_counts[i]; | |
| 40 | } | ||
| 41 | 46 | return send_counts; | |
| 42 | } | ||
| 43 | |||
| 44 | 46 | std::vector<int> CalculatePrevElements(int rank, int comm_size, const std::vector<int> &vec, | |
| 45 | const std::vector<int> &displacements) { | ||
| 46 | 46 | std::vector<int> prev_elements(comm_size, 0); | |
| 47 |
2/2✓ Branch 0 taken 23 times.
✓ Branch 1 taken 23 times.
|
46 | if (rank == 0) { |
| 48 |
2/2✓ Branch 0 taken 23 times.
✓ Branch 1 taken 23 times.
|
46 | for (int i = 1; i < comm_size; i++) { |
| 49 | 23 | int prev_block_end = displacements[i] - 1; | |
| 50 | 23 | prev_elements[i] = vec[prev_block_end]; | |
| 51 | } | ||
| 52 | } | ||
| 53 | 46 | return prev_elements; | |
| 54 | } | ||
| 55 | |||
| 56 | 46 | std::tuple<int, int> FindLocalMinDistance(const std::vector<int> &local_data, int my_chunk_size, int rank, | |
| 57 | int my_prev_element, const std::vector<int> &displacements) { | ||
| 58 | int local_min_dist = std::numeric_limits<int>::max(); | ||
| 59 | int local_min_index = -1; | ||
| 60 | |||
| 61 |
2/2✓ Branch 0 taken 23 times.
✓ Branch 1 taken 23 times.
|
46 | if (rank > 0 && my_chunk_size > 0) { |
| 62 | 23 | int dist = std::abs(my_prev_element - local_data[0]); | |
| 63 |
1/2✓ Branch 0 taken 23 times.
✗ Branch 1 not taken.
|
23 | if (dist < local_min_dist) { |
| 64 | local_min_dist = dist; | ||
| 65 | 23 | local_min_index = displacements[rank]; | |
| 66 | } | ||
| 67 | } | ||
| 68 | |||
| 69 |
2/2✓ Branch 0 taken 74 times.
✓ Branch 1 taken 46 times.
|
120 | for (int i = 0; i < my_chunk_size - 1; i++) { |
| 70 |
2/2✓ Branch 0 taken 40 times.
✓ Branch 1 taken 34 times.
|
74 | int dist = std::abs(local_data[i] - local_data[i + 1]); |
| 71 |
2/2✓ Branch 0 taken 40 times.
✓ Branch 1 taken 34 times.
|
74 | if (dist < local_min_dist) { |
| 72 | local_min_dist = dist; | ||
| 73 | 40 | local_min_index = displacements[rank] + i + 1; | |
| 74 | } | ||
| 75 | } | ||
| 76 | |||
| 77 | 46 | return std::make_tuple(local_min_dist, local_min_index); | |
| 78 | } | ||
| 79 | } // namespace | ||
| 80 | |||
| 81 | 50 | bool AlekseevAMinDistNeighElemVecMPI::RunImpl() { | |
| 82 | 50 | int rank = 0; | |
| 83 | 50 | int comm_size = 0; | |
| 84 | 50 | MPI_Comm_rank(MPI_COMM_WORLD, &rank); | |
| 85 | 50 | MPI_Comm_size(MPI_COMM_WORLD, &comm_size); | |
| 86 | |||
| 87 | const auto &vec = GetInput(); | ||
| 88 | 50 | int total_size = static_cast<int>(vec.size()); | |
| 89 | |||
| 90 |
2/2✓ Branch 0 taken 4 times.
✓ Branch 1 taken 46 times.
|
50 | if (total_size < 2) { |
| 91 | GetOutput() = std::make_tuple(-1, -1); | ||
| 92 | 4 | return true; | |
| 93 | } | ||
| 94 | |||
| 95 | 46 | std::vector<int> displacements; | |
| 96 |
1/2✓ Branch 1 taken 46 times.
✗ Branch 2 not taken.
|
46 | std::vector<int> send_counts = CalculateSendCountsAndDisplacements(total_size, comm_size, displacements); |
| 97 | |||
| 98 |
1/2✓ Branch 1 taken 46 times.
✗ Branch 2 not taken.
|
46 | int my_chunk_size = send_counts[rank]; |
| 99 |
2/6✓ Branch 1 taken 46 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 46 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
|
46 | std::vector<int> local_data(my_chunk_size); |
| 100 | |||
| 101 |
1/2✓ Branch 1 taken 46 times.
✗ Branch 2 not taken.
|
46 | MPI_Scatterv(vec.data(), send_counts.data(), displacements.data(), MPI_INT, local_data.data(), my_chunk_size, MPI_INT, |
| 102 | 0, MPI_COMM_WORLD); | ||
| 103 | |||
| 104 |
1/2✓ Branch 1 taken 46 times.
✗ Branch 2 not taken.
|
46 | std::vector<int> prev_elements = CalculatePrevElements(rank, comm_size, vec, displacements); |
| 105 | |||
| 106 |
1/2✓ Branch 1 taken 46 times.
✗ Branch 2 not taken.
|
46 | int my_prev_element = 0; |
| 107 |
1/2✓ Branch 1 taken 46 times.
✗ Branch 2 not taken.
|
46 | MPI_Scatter(prev_elements.data(), 1, MPI_INT, &my_prev_element, 1, MPI_INT, 0, MPI_COMM_WORLD); |
| 108 | |||
| 109 | auto [local_min_dist, local_min_index] = | ||
| 110 | 46 | FindLocalMinDistance(local_data, my_chunk_size, rank, my_prev_element, displacements); | |
| 111 | |||
| 112 | 46 | int global_min_dist = std::numeric_limits<int>::max(); | |
| 113 |
1/2✓ Branch 1 taken 46 times.
✗ Branch 2 not taken.
|
46 | MPI_Allreduce(&local_min_dist, &global_min_dist, 1, MPI_INT, MPI_MIN, MPI_COMM_WORLD); |
| 114 | |||
| 115 | 46 | int candidate_index = | |
| 116 |
3/4✓ Branch 0 taken 35 times.
✓ Branch 1 taken 11 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 35 times.
|
46 | (local_min_dist == global_min_dist && local_min_index != -1) ? local_min_index : std::numeric_limits<int>::max(); |
| 117 | |||
| 118 | 46 | int global_min_index = std::numeric_limits<int>::max(); | |
| 119 |
1/2✓ Branch 1 taken 46 times.
✗ Branch 2 not taken.
|
46 | MPI_Allreduce(&candidate_index, &global_min_index, 1, MPI_INT, MPI_MIN, MPI_COMM_WORLD); |
| 120 | |||
| 121 |
1/2✓ Branch 0 taken 46 times.
✗ Branch 1 not taken.
|
46 | GetOutput() = std::make_tuple(global_min_index - 1, global_min_index); |
| 122 | return true; | ||
| 123 | } | ||
| 124 | |||
| 125 | 50 | bool AlekseevAMinDistNeighElemVecMPI::PostProcessingImpl() { | |
| 126 | 50 | return true; | |
| 127 | } | ||
| 128 | |||
| 129 | } // namespace alekseev_a_min_dist_neigh_elem_vec | ||
| 130 |