| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include "baranov_a_sign_alternations/mpi/include/ops_mpi.hpp" | ||
| 2 | |||
| 3 | #include <mpi.h> | ||
| 4 | |||
| 5 | #include <vector> | ||
| 6 | |||
| 7 | #include "baranov_a_sign_alternations/common/include/common.hpp" | ||
| 8 | |||
| 9 | namespace baranov_a_sign_alternations { | ||
| 10 | |||
| 11 | namespace { | ||
| 12 | 20 | int CountAlternationsInRange(const std::vector<int> &input, int start, int end) { | |
| 13 | int count = 0; | ||
| 14 |
3/4✓ Branch 0 taken 37 times.
✓ Branch 1 taken 20 times.
✓ Branch 2 taken 37 times.
✗ Branch 3 not taken.
|
57 | for (int i = start; i < end && i < static_cast<int>(input.size()) - 1; i++) { |
| 15 |
2/2✓ Branch 0 taken 27 times.
✓ Branch 1 taken 10 times.
|
37 | int current = input[i]; |
| 16 | 37 | int next = input[i + 1]; | |
| 17 |
2/2✓ Branch 0 taken 27 times.
✓ Branch 1 taken 10 times.
|
37 | if (current != 0 && next != 0) { |
| 18 |
4/4✓ Branch 0 taken 20 times.
✓ Branch 1 taken 7 times.
✓ Branch 2 taken 3 times.
✓ Branch 3 taken 17 times.
|
27 | if ((current > 0 && next < 0) || (current < 0 && next > 0)) { |
| 19 | 10 | count++; | |
| 20 | } | ||
| 21 | } | ||
| 22 | } | ||
| 23 | 20 | return count; | |
| 24 | } | ||
| 25 | |||
| 26 | void CalculateChunkBounds(int world_rank, int world_size, int pairs_count, int &start_pair, int &end_pair) { | ||
| 27 | 18 | int pairs_per_process = pairs_count / world_size; | |
| 28 | 18 | int remainder = pairs_count % world_size; | |
| 29 | |||
| 30 | 18 | if (world_rank < remainder) { | |
| 31 | 7 | start_pair = world_rank * (pairs_per_process + 1); | |
| 32 | 7 | end_pair = start_pair + pairs_per_process + 1; | |
| 33 | } else { | ||
| 34 | 11 | start_pair = (remainder * (pairs_per_process + 1)) + ((world_rank - remainder) * pairs_per_process); | |
| 35 | 11 | end_pair = start_pair + pairs_per_process; | |
| 36 | } | ||
| 37 | } | ||
| 38 | } // namespace | ||
| 39 | |||
| 40 |
1/2✓ Branch 1 taken 26 times.
✗ Branch 2 not taken.
|
26 | BaranovASignAlternationsMPI::BaranovASignAlternationsMPI(const InType &in) { |
| 41 | SetTypeOfTask(GetStaticTypeOfTask()); | ||
| 42 |
1/2✓ Branch 1 taken 26 times.
✗ Branch 2 not taken.
|
26 | GetInput() = in; |
| 43 | 26 | GetOutput() = 0; | |
| 44 | 26 | } | |
| 45 | |||
| 46 | 26 | bool BaranovASignAlternationsMPI::ValidationImpl() { | |
| 47 | 26 | return GetOutput() >= 0; | |
| 48 | } | ||
| 49 | |||
| 50 | 26 | bool BaranovASignAlternationsMPI::PreProcessingImpl() { | |
| 51 | 26 | return true; | |
| 52 | } | ||
| 53 | |||
| 54 | 26 | bool BaranovASignAlternationsMPI::RunImpl() { | |
| 55 | const auto &input = GetInput(); | ||
| 56 | |||
| 57 | 26 | int world_size = 0; | |
| 58 | 26 | int world_rank = 0; | |
| 59 | 26 | MPI_Comm_size(MPI_COMM_WORLD, &world_size); | |
| 60 | 26 | MPI_Comm_rank(MPI_COMM_WORLD, &world_rank); | |
| 61 | |||
| 62 |
2/2✓ Branch 0 taken 4 times.
✓ Branch 1 taken 22 times.
|
26 | if (input.size() < 2) { |
| 63 | 4 | GetOutput() = 0; | |
| 64 | 4 | MPI_Barrier(MPI_COMM_WORLD); | |
| 65 | 4 | return true; | |
| 66 | } | ||
| 67 | |||
| 68 | 22 | int pairs_count = static_cast<int>(input.size()) - 1; | |
| 69 | |||
| 70 |
2/2✓ Branch 0 taken 4 times.
✓ Branch 1 taken 18 times.
|
22 | if (pairs_count < world_size) { |
| 71 | 4 | int alternations_count = 0; | |
| 72 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 2 times.
|
4 | if (world_rank == 0) { |
| 73 | 2 | alternations_count = CountAlternationsInRange(input, 0, pairs_count); | |
| 74 | } | ||
| 75 | |||
| 76 | 4 | MPI_Bcast(&alternations_count, 1, MPI_INT, 0, MPI_COMM_WORLD); | |
| 77 | 4 | GetOutput() = alternations_count; | |
| 78 | 4 | MPI_Barrier(MPI_COMM_WORLD); | |
| 79 | return true; | ||
| 80 | } | ||
| 81 | |||
| 82 | int start_pair = 0; | ||
| 83 | int end_pair = 0; | ||
| 84 |
2/2✓ Branch 0 taken 7 times.
✓ Branch 1 taken 11 times.
|
18 | CalculateChunkBounds(world_rank, world_size, pairs_count, start_pair, end_pair); |
| 85 | |||
| 86 | 18 | int local_alternations = CountAlternationsInRange(input, start_pair, end_pair); | |
| 87 | |||
| 88 | 18 | int total_alternations = 0; | |
| 89 | |||
| 90 | 18 | MPI_Reduce(&local_alternations, &total_alternations, 1, MPI_INT, MPI_SUM, 0, MPI_COMM_WORLD); | |
| 91 | |||
| 92 |
2/2✓ Branch 0 taken 9 times.
✓ Branch 1 taken 9 times.
|
18 | if (world_rank == 0) { |
| 93 | 9 | GetOutput() = total_alternations; | |
| 94 |
2/2✓ Branch 0 taken 9 times.
✓ Branch 1 taken 9 times.
|
18 | for (int i = 1; i < world_size; i++) { |
| 95 | 9 | MPI_Send(&total_alternations, 1, MPI_INT, i, 0, MPI_COMM_WORLD); | |
| 96 | } | ||
| 97 | |||
| 98 | } else { | ||
| 99 | 9 | MPI_Recv(&total_alternations, 1, MPI_INT, 0, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE); | |
| 100 | 9 | GetOutput() = total_alternations; | |
| 101 | } | ||
| 102 | |||
| 103 | 18 | MPI_Barrier(MPI_COMM_WORLD); | |
| 104 | return true; | ||
| 105 | } | ||
| 106 | |||
| 107 | 26 | bool BaranovASignAlternationsMPI::PostProcessingImpl() { | |
| 108 | 26 | return true; | |
| 109 | } | ||
| 110 | |||
| 111 | } // namespace baranov_a_sign_alternations | ||
| 112 |