| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include "lifanov_k_adj_inv_count_restore/mpi/include/ops_mpi.hpp" | ||
| 2 | |||
| 3 | #include <mpi.h> | ||
| 4 | |||
| 5 | #include <cstddef> | ||
| 6 | #include <vector> | ||
| 7 | |||
| 8 | #include "lifanov_k_adj_inv_count_restore/common/include/common.hpp" | ||
| 9 | |||
| 10 | namespace lifanov_k_adj_inv_count_restore { | ||
| 11 | |||
| 12 |
1/2✓ Branch 1 taken 58 times.
✗ Branch 2 not taken.
|
58 | LifanovKAdjacentInversionCountMPI::LifanovKAdjacentInversionCountMPI(const InType &in) { |
| 13 | SetTypeOfTask(GetStaticTypeOfTask()); | ||
| 14 |
1/2✓ Branch 1 taken 58 times.
✗ Branch 2 not taken.
|
58 | GetInput() = in; |
| 15 | 58 | GetOutput() = 0; | |
| 16 | 58 | } | |
| 17 | |||
| 18 |
1/2✓ Branch 0 taken 58 times.
✗ Branch 1 not taken.
|
58 | bool LifanovKAdjacentInversionCountMPI::ValidationImpl() { |
| 19 |
2/4✓ Branch 0 taken 58 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 58 times.
|
58 | return !GetInput().empty() && (GetOutput() == 0); |
| 20 | } | ||
| 21 | |||
| 22 | 58 | bool LifanovKAdjacentInversionCountMPI::PreProcessingImpl() { | |
| 23 | 58 | return true; | |
| 24 | } | ||
| 25 | |||
| 26 | 58 | bool LifanovKAdjacentInversionCountMPI::RunImpl() { | |
| 27 | const auto &data = GetInput(); | ||
| 28 | const std::size_t n = data.size(); | ||
| 29 | |||
| 30 | 58 | int rank = 0; | |
| 31 | 58 | int world_size_signed = 1; | |
| 32 | 58 | MPI_Comm_rank(MPI_COMM_WORLD, &rank); | |
| 33 | 58 | MPI_Comm_size(MPI_COMM_WORLD, &world_size_signed); | |
| 34 | |||
| 35 | 58 | const auto world_size = static_cast<std::size_t>(world_size_signed); | |
| 36 | |||
| 37 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 58 times.
|
58 | if (n < 2) { |
| 38 | ✗ | GetOutput() = 0; | |
| 39 | ✗ | return true; | |
| 40 | } | ||
| 41 | |||
| 42 | 58 | const auto total_pairs = n - 1; | |
| 43 | 58 | const auto base = total_pairs / world_size; | |
| 44 | 58 | const auto rem = total_pairs % world_size; | |
| 45 | |||
| 46 | 58 | std::vector<int> sendcounts(world_size, 0); | |
| 47 |
1/4✓ Branch 1 taken 58 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
|
58 | std::vector<int> displs(world_size, 0); |
| 48 | |||
| 49 |
2/2✓ Branch 0 taken 116 times.
✓ Branch 1 taken 58 times.
|
174 | for (std::size_t proc_idx = 0; proc_idx < world_size; ++proc_idx) { |
| 50 |
2/2✓ Branch 0 taken 92 times.
✓ Branch 1 taken 24 times.
|
116 | const auto extra = (proc_idx < rem ? std::size_t{1} : std::size_t{0}); |
| 51 | 116 | const auto local_pairs = base + extra; | |
| 52 | 116 | sendcounts[proc_idx] = static_cast<int>(local_pairs + 1); | |
| 53 | } | ||
| 54 | |||
| 55 |
2/2✓ Branch 0 taken 58 times.
✓ Branch 1 taken 58 times.
|
116 | for (std::size_t proc_idx = 1; proc_idx < world_size; ++proc_idx) { |
| 56 | 58 | displs[proc_idx] = displs[proc_idx - 1] + sendcounts[proc_idx - 1] - 1; | |
| 57 | } | ||
| 58 | |||
| 59 |
1/2✓ Branch 1 taken 58 times.
✗ Branch 2 not taken.
|
58 | int local_size = sendcounts[static_cast<std::size_t>(rank)]; |
| 60 |
2/6✓ Branch 1 taken 58 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 58 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
|
58 | std::vector<int> local(local_size); |
| 61 | |||
| 62 |
1/2✓ Branch 1 taken 58 times.
✗ Branch 2 not taken.
|
58 | MPI_Scatterv(data.data(), sendcounts.data(), displs.data(), MPI_INT, local.data(), local_size, MPI_INT, 0, |
| 63 | MPI_COMM_WORLD); | ||
| 64 | |||
| 65 | 58 | int local_inv = 0; | |
| 66 |
2/2✓ Branch 0 taken 130 times.
✓ Branch 1 taken 58 times.
|
188 | for (int i = 0; i + 1 < local_size; ++i) { |
| 67 |
2/2✓ Branch 0 taken 46 times.
✓ Branch 1 taken 84 times.
|
130 | if (local[i] > local[i + 1]) { |
| 68 | 46 | local_inv++; | |
| 69 | } | ||
| 70 | } | ||
| 71 | |||
| 72 | 58 | int global = 0; | |
| 73 |
1/2✓ Branch 1 taken 58 times.
✗ Branch 2 not taken.
|
58 | MPI_Reduce(&local_inv, &global, 1, MPI_INT, MPI_SUM, 0, MPI_COMM_WORLD); |
| 74 |
1/2✓ Branch 1 taken 58 times.
✗ Branch 2 not taken.
|
58 | MPI_Bcast(&global, 1, MPI_INT, 0, MPI_COMM_WORLD); |
| 75 | |||
| 76 |
1/2✓ Branch 0 taken 58 times.
✗ Branch 1 not taken.
|
58 | GetOutput() = global; |
| 77 | |||
| 78 | return true; | ||
| 79 | } | ||
| 80 | |||
| 81 | 58 | bool LifanovKAdjacentInversionCountMPI::PostProcessingImpl() { | |
| 82 | 58 | return true; | |
| 83 | } | ||
| 84 | |||
| 85 | } // namespace lifanov_k_adj_inv_count_restore | ||
| 86 |