| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include "romanov_m_closest_elem_vec/mpi/include/ops_mpi.hpp" | ||
| 2 | |||
| 3 | #include <mpi.h> | ||
| 4 | |||
| 5 | #include <cmath> | ||
| 6 | #include <cstddef> | ||
| 7 | #include <limits> | ||
| 8 | #include <tuple> | ||
| 9 | #include <vector> | ||
| 10 | |||
| 11 | #include "romanov_m_closest_elem_vec/common/include/common.hpp" | ||
| 12 | |||
| 13 | namespace romanov_m_closest_elem_vec { | ||
| 14 | |||
| 15 | 30 | void PerformBoundaryCheck(int rank, int comm_size, int local_sz, int global_offset, const std::vector<int> &local_data, | |
| 16 | Result &local_res) { | ||
| 17 |
1/2✓ Branch 0 taken 30 times.
✗ Branch 1 not taken.
|
30 | if (comm_size > 1) { |
| 18 | 30 | int send_val = 0; | |
| 19 | 30 | int prev_last_val = 0; | |
| 20 |
2/2✓ Branch 0 taken 15 times.
✓ Branch 1 taken 15 times.
|
30 | const int dest = (rank == comm_size - 1) ? MPI_PROC_NULL : rank + 1; |
| 21 |
2/2✓ Branch 0 taken 15 times.
✓ Branch 1 taken 15 times.
|
30 | const int source = (rank == 0) ? MPI_PROC_NULL : rank - 1; |
| 22 | |||
| 23 |
3/4✓ Branch 0 taken 30 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 15 times.
✓ Branch 3 taken 15 times.
|
30 | if (local_sz > 0 && rank != comm_size - 1) { |
| 24 | 15 | send_val = local_data.back(); | |
| 25 | } | ||
| 26 | |||
| 27 | 30 | MPI_Sendrecv(&send_val, 1, MPI_INT, dest, 0, &prev_last_val, 1, MPI_INT, source, 0, MPI_COMM_WORLD, | |
| 28 | MPI_STATUS_IGNORE); | ||
| 29 | |||
| 30 |
2/2✓ Branch 0 taken 15 times.
✓ Branch 1 taken 15 times.
|
30 | if (rank > 0 && local_sz > 0) { |
| 31 |
2/2✓ Branch 0 taken 6 times.
✓ Branch 1 taken 9 times.
|
15 | const int boundary_idx = global_offset - 1; |
| 32 | 15 | const int boundary_diff = std::abs(local_data[0] - prev_last_val); | |
| 33 | UpdateResult(local_res, boundary_diff, boundary_idx); | ||
| 34 | } | ||
| 35 | } | ||
| 36 | 30 | } | |
| 37 | |||
| 38 | 30 | void CalculateDistribution(int total_size, int comm_size, std::vector<int> &send_counts, std::vector<int> &displs) { | |
| 39 | 30 | int base_count = total_size / comm_size; | |
| 40 | 30 | int remainder = total_size % comm_size; | |
| 41 | int current_displ = 0; | ||
| 42 | |||
| 43 | 30 | send_counts.resize(comm_size); | |
| 44 | 30 | displs.resize(comm_size); | |
| 45 | |||
| 46 |
2/2✓ Branch 0 taken 60 times.
✓ Branch 1 taken 30 times.
|
90 | for (int i = 0; i < comm_size; ++i) { |
| 47 |
2/2✓ Branch 0 taken 52 times.
✓ Branch 1 taken 8 times.
|
112 | send_counts[i] = base_count + (i < remainder ? 1 : 0); |
| 48 | 60 | displs[i] = current_displ; | |
| 49 | 60 | current_displ += send_counts[i]; | |
| 50 | } | ||
| 51 | 30 | } | |
| 52 | |||
| 53 | ✗ | void UpdateResult(Result ¤t_res, int new_diff, int new_idx) { | |
| 54 |
4/6✓ Branch 0 taken 33 times.
✓ Branch 1 taken 403 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 6 times.
✓ Branch 5 taken 9 times.
|
451 | if (new_diff < current_res.diff) { |
| 55 | 39 | current_res.diff = new_diff; | |
| 56 | 39 | current_res.idx = new_idx; | |
| 57 |
4/6✓ Branch 0 taken 343 times.
✓ Branch 1 taken 60 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 5 times.
✓ Branch 5 taken 4 times.
|
412 | } else if (new_diff == current_res.diff) { |
| 58 |
4/12✓ Branch 0 taken 343 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 343 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✓ Branch 8 taken 5 times.
✗ Branch 9 not taken.
✓ Branch 10 taken 5 times.
✗ Branch 11 not taken.
|
348 | if (current_res.idx == -1 || new_idx < current_res.idx) { |
| 59 | 5 | current_res.idx = new_idx; | |
| 60 | } | ||
| 61 | } | ||
| 62 | ✗ | } | |
| 63 | |||
| 64 | 30 | void LocalFindMinDiff(const std::vector<int> &local_data, int local_sz, int global_offset, Result &local_res) { | |
| 65 |
2/2✓ Branch 0 taken 28 times.
✓ Branch 1 taken 2 times.
|
30 | if (local_sz >= 2) { |
| 66 |
2/2✓ Branch 0 taken 436 times.
✓ Branch 1 taken 28 times.
|
464 | for (int i = 0; i < local_sz - 1; ++i) { |
| 67 | 436 | const int current_idx = global_offset + i; | |
| 68 |
2/2✓ Branch 0 taken 33 times.
✓ Branch 1 taken 403 times.
|
436 | const int diff = std::abs(local_data[i + 1] - local_data[i]); |
| 69 | |||
| 70 | UpdateResult(local_res, diff, current_idx); | ||
| 71 | } | ||
| 72 | } | ||
| 73 | 30 | } | |
| 74 | |||
| 75 |
1/2✓ Branch 1 taken 30 times.
✗ Branch 2 not taken.
|
30 | RomanovMClosestElemVecMPI::RomanovMClosestElemVecMPI(const InType &in) { |
| 76 | SetTypeOfTask(GetStaticTypeOfTask()); | ||
| 77 |
1/2✓ Branch 1 taken 30 times.
✗ Branch 2 not taken.
|
30 | GetInput() = in; |
| 78 | GetOutput() = std::make_tuple(-1, -1); | ||
| 79 | 30 | } | |
| 80 | |||
| 81 | 30 | bool RomanovMClosestElemVecMPI::ValidationImpl() { | |
| 82 | 30 | int rank = 0; | |
| 83 | 30 | MPI_Comm_rank(MPI_COMM_WORLD, &rank); | |
| 84 | |||
| 85 | 30 | int size = 0; | |
| 86 |
2/2✓ Branch 0 taken 15 times.
✓ Branch 1 taken 15 times.
|
30 | if (rank == 0) { |
| 87 | 15 | size = static_cast<int>(GetInput().size()); | |
| 88 | } | ||
| 89 | |||
| 90 | 30 | MPI_Bcast(&size, 1, MPI_INT, 0, MPI_COMM_WORLD); | |
| 91 | 30 | return size >= 2; | |
| 92 | } | ||
| 93 | |||
| 94 | 30 | bool RomanovMClosestElemVecMPI::PreProcessingImpl() { | |
| 95 | 30 | return true; | |
| 96 | } | ||
| 97 | |||
| 98 | 30 | bool RomanovMClosestElemVecMPI::RunImpl() { | |
| 99 | 30 | int rank = 0; | |
| 100 | 30 | int comm_size = 0; | |
| 101 | 30 | MPI_Comm_rank(MPI_COMM_WORLD, &rank); | |
| 102 | 30 | MPI_Comm_size(MPI_COMM_WORLD, &comm_size); | |
| 103 | |||
| 104 | const size_t global_size = GetInput().size(); | ||
| 105 | |||
| 106 |
1/2✓ Branch 0 taken 30 times.
✗ Branch 1 not taken.
|
30 | if (global_size < 2) { |
| 107 | return true; | ||
| 108 | } | ||
| 109 | |||
| 110 | 30 | std::vector<int> send_counts; | |
| 111 | 30 | std::vector<int> displs; | |
| 112 | |||
| 113 |
1/2✓ Branch 1 taken 30 times.
✗ Branch 2 not taken.
|
30 | CalculateDistribution(static_cast<int>(global_size), comm_size, send_counts, displs); |
| 114 | |||
| 115 |
1/2✓ Branch 1 taken 30 times.
✗ Branch 2 not taken.
|
30 | const int global_offset = displs[rank]; |
| 116 | 30 | const int local_sz = send_counts[rank]; | |
| 117 | |||
| 118 |
3/6✓ Branch 1 taken 30 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 15 times.
✓ Branch 4 taken 15 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
|
30 | std::vector<int> local_data(local_sz); |
| 119 | |||
| 120 |
3/4✓ Branch 0 taken 15 times.
✓ Branch 1 taken 15 times.
✓ Branch 3 taken 30 times.
✗ Branch 4 not taken.
|
45 | MPI_Scatterv(rank == 0 ? GetInput().data() : nullptr, send_counts.data(), displs.data(), MPI_INT, local_data.data(), |
| 121 | local_sz, MPI_INT, 0, MPI_COMM_WORLD); | ||
| 122 | |||
| 123 | Result local_res = {0, 0}; | ||
| 124 | 30 | local_res.diff = std::numeric_limits<int>::max(); | |
| 125 | 30 | local_res.idx = -1; | |
| 126 | |||
| 127 | 30 | LocalFindMinDiff(local_data, local_sz, global_offset, local_res); | |
| 128 | |||
| 129 |
1/2✓ Branch 1 taken 30 times.
✗ Branch 2 not taken.
|
30 | PerformBoundaryCheck(rank, comm_size, local_sz, global_offset, local_data, local_res); |
| 130 | |||
| 131 | Result global_res = {0, 0}; | ||
| 132 |
1/2✓ Branch 1 taken 30 times.
✗ Branch 2 not taken.
|
30 | MPI_Allreduce(&local_res, &global_res, 1, MPI_2INT, MPI_MINLOC, MPI_COMM_WORLD); |
| 133 |
1/2✓ Branch 0 taken 30 times.
✗ Branch 1 not taken.
|
30 | GetOutput() = std::make_tuple(global_res.idx, global_res.idx + 1); |
| 134 | return true; | ||
| 135 | } | ||
| 136 | |||
| 137 | 30 | bool RomanovMClosestElemVecMPI::PostProcessingImpl() { | |
| 138 | 30 | return true; | |
| 139 | } | ||
| 140 | |||
| 141 | } // namespace romanov_m_closest_elem_vec | ||
| 142 |