| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include "tsyplakov_k_vec_neighbours/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 "tsyplakov_k_vec_neighbours/common/include/common.hpp" | ||
| 11 | |||
| 12 | namespace tsyplakov_k_vec_neighbours { | ||
| 13 | |||
| 14 |
1/2✓ Branch 1 taken 44 times.
✗ Branch 2 not taken.
|
44 | TsyplakovKVecNeighboursMPI::TsyplakovKVecNeighboursMPI(const InType &in) { |
| 15 | SetTypeOfTask(GetStaticTypeOfTask()); | ||
| 16 |
1/2✓ Branch 1 taken 44 times.
✗ Branch 2 not taken.
|
44 | GetInput() = in; |
| 17 | GetOutput() = std::make_tuple(-1, -1); | ||
| 18 | 44 | } | |
| 19 | |||
| 20 | 44 | bool TsyplakovKVecNeighboursMPI::ValidationImpl() { | |
| 21 | 44 | return true; | |
| 22 | } | ||
| 23 | |||
| 24 | 44 | bool TsyplakovKVecNeighboursMPI::PreProcessingImpl() { | |
| 25 | 44 | return true; | |
| 26 | } | ||
| 27 | |||
| 28 | 44 | bool TsyplakovKVecNeighboursMPI::RunImpl() { | |
| 29 | 44 | int rank = 0; | |
| 30 | 44 | int size = 0; | |
| 31 | 44 | MPI_Comm_rank(MPI_COMM_WORLD, &rank); | |
| 32 | 44 | MPI_Comm_size(MPI_COMM_WORLD, &size); | |
| 33 | |||
| 34 | const auto &full_arr = GetInput(); | ||
| 35 | 44 | int n = static_cast<int>(full_arr.size()); | |
| 36 | |||
| 37 |
2/2✓ Branch 0 taken 4 times.
✓ Branch 1 taken 40 times.
|
44 | if (n < 2) { |
| 38 | GetOutput() = std::make_tuple(-1, -1); | ||
| 39 | 4 | return true; | |
| 40 | } | ||
| 41 | |||
| 42 | 40 | std::vector<int> sendcounts(size); | |
| 43 |
1/4✓ Branch 1 taken 40 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
|
40 | std::vector<int> displs(size); |
| 44 | |||
| 45 | 40 | int base = n / size; | |
| 46 | 40 | int extra = n % size; | |
| 47 | |||
| 48 | int offset = 0; | ||
| 49 |
2/2✓ Branch 0 taken 80 times.
✓ Branch 1 taken 40 times.
|
120 | for (int i = 0; i < size; i++) { |
| 50 |
2/2✓ Branch 0 taken 58 times.
✓ Branch 1 taken 22 times.
|
138 | sendcounts[i] = base + (i < extra ? 1 : 0); |
| 51 | 80 | displs[i] = offset; | |
| 52 | 80 | offset += sendcounts[i]; | |
| 53 | } | ||
| 54 | |||
| 55 |
2/2✓ Branch 0 taken 40 times.
✓ Branch 1 taken 40 times.
|
80 | for (int i = 0; i < size - 1; i++) { |
| 56 | 40 | sendcounts[i]++; | |
| 57 | } | ||
| 58 | |||
| 59 |
1/2✓ Branch 1 taken 40 times.
✗ Branch 2 not taken.
|
40 | int recv_count = sendcounts[rank]; |
| 60 |
2/6✓ Branch 1 taken 40 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 40 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
|
40 | std::vector<int> local_arr(recv_count); |
| 61 | |||
| 62 |
1/2✓ Branch 1 taken 40 times.
✗ Branch 2 not taken.
|
40 | MPI_Scatterv(full_arr.data(), sendcounts.data(), displs.data(), MPI_INT, local_arr.data(), recv_count, MPI_INT, 0, |
| 63 | MPI_COMM_WORLD); | ||
| 64 | |||
| 65 | 40 | int best_local_gap = std::numeric_limits<int>::max(); | |
| 66 | int best_local_pos = -1; | ||
| 67 | |||
| 68 | 40 | int global_offset = displs[rank]; | |
| 69 | |||
| 70 |
2/2✓ Branch 0 taken 139 times.
✓ Branch 1 taken 40 times.
|
179 | for (int i = 0; i < recv_count - 1; i++) { |
| 71 |
2/2✓ Branch 0 taken 53 times.
✓ Branch 1 taken 86 times.
|
139 | int diff = std::abs(local_arr[i] - local_arr[i + 1]); |
| 72 |
2/2✓ Branch 0 taken 53 times.
✓ Branch 1 taken 86 times.
|
139 | if (diff < best_local_gap) { |
| 73 | 53 | best_local_gap = diff; | |
| 74 | 53 | best_local_pos = global_offset + i; | |
| 75 | } | ||
| 76 | } | ||
| 77 | |||
| 78 | 40 | int best_global_gap = 0; | |
| 79 |
1/2✓ Branch 1 taken 40 times.
✗ Branch 2 not taken.
|
40 | MPI_Allreduce(&best_local_gap, &best_global_gap, 1, MPI_INT, MPI_MIN, MPI_COMM_WORLD); |
| 80 | |||
| 81 |
2/2✓ Branch 0 taken 7 times.
✓ Branch 1 taken 33 times.
|
40 | int candidate_idx = (best_local_gap == best_global_gap ? best_local_pos : std::numeric_limits<int>::max()); |
| 82 | 40 | int final_idx = 0; | |
| 83 | |||
| 84 |
1/2✓ Branch 1 taken 40 times.
✗ Branch 2 not taken.
|
40 | MPI_Allreduce(&candidate_idx, &final_idx, 1, MPI_INT, MPI_MIN, MPI_COMM_WORLD); |
| 85 | |||
| 86 |
1/2✓ Branch 0 taken 40 times.
✗ Branch 1 not taken.
|
40 | GetOutput() = std::make_tuple(final_idx, final_idx + 1); |
| 87 | return true; | ||
| 88 | } | ||
| 89 | |||
| 90 | 44 | bool TsyplakovKVecNeighboursMPI::PostProcessingImpl() { | |
| 91 | 44 | return true; | |
| 92 | } | ||
| 93 | |||
| 94 | } // namespace tsyplakov_k_vec_neighbours | ||
| 95 |