| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include "olesnitskiy_v_find_viol/mpi/include/ops_mpi.hpp" | ||
| 2 | |||
| 3 | #include <mpi.h> | ||
| 4 | |||
| 5 | #include <vector> | ||
| 6 | |||
| 7 | #include "olesnitskiy_v_find_viol/common/include/common.hpp" | ||
| 8 | |||
| 9 | namespace olesnitskiy_v_find_viol { | ||
| 10 | |||
| 11 | ✗ | [[nodiscard]] int OlesnitskiyVFindViolMPI::CountViolation(double current, double next) { | |
| 12 | const double epsilon = 1e-10; | ||
| 13 |
3/6✓ Branch 0 taken 8 times.
✓ Branch 1 taken 7 times.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
|
16 | return (current - next > epsilon) ? 1 : 0; |
| 14 | } | ||
| 15 | |||
| 16 |
1/2✓ Branch 1 taken 20 times.
✗ Branch 2 not taken.
|
20 | OlesnitskiyVFindViolMPI::OlesnitskiyVFindViolMPI(const InType &in) { |
| 17 | SetTypeOfTask(GetStaticTypeOfTask()); | ||
| 18 |
1/2✓ Branch 1 taken 20 times.
✗ Branch 2 not taken.
|
20 | GetInput() = in; |
| 19 | 20 | GetOutput() = 0; | |
| 20 | 20 | } | |
| 21 | |||
| 22 | 20 | bool OlesnitskiyVFindViolMPI::ValidationImpl() { | |
| 23 | 20 | return true; | |
| 24 | } | ||
| 25 | |||
| 26 | 20 | bool OlesnitskiyVFindViolMPI::PreProcessingImpl() { | |
| 27 | 20 | return true; | |
| 28 | } | ||
| 29 | |||
| 30 | 2 | bool OlesnitskiyVFindViolMPI::RunSequentialCase() { | |
| 31 | const auto &input_data = GetInput(); | ||
| 32 | 2 | int world_rank = 0; | |
| 33 | 2 | MPI_Comm_rank(MPI_COMM_WORLD, &world_rank); | |
| 34 | |||
| 35 | 2 | int viol = 0; | |
| 36 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
|
2 | if (world_rank == 0) { |
| 37 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
|
2 | for (int i = 0; i < static_cast<int>(input_data.size()) - 1; i++) { |
| 38 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
2 | viol += CountViolation(input_data[i], input_data[i + 1]); |
| 39 | } | ||
| 40 | } | ||
| 41 | 2 | MPI_Bcast(&viol, 1, MPI_INT, 0, MPI_COMM_WORLD); | |
| 42 | 2 | GetOutput() = viol; | |
| 43 | 2 | return true; | |
| 44 | } | ||
| 45 | |||
| 46 |
2/2✓ Branch 0 taken 4 times.
✓ Branch 1 taken 16 times.
|
20 | bool OlesnitskiyVFindViolMPI::RunImpl() { |
| 47 |
2/2✓ Branch 0 taken 4 times.
✓ Branch 1 taken 16 times.
|
20 | if (GetInput().size() < 2) { |
| 48 | 4 | GetOutput() = 0; | |
| 49 | 4 | return true; | |
| 50 | } | ||
| 51 | |||
| 52 | const auto &input_data = GetInput(); | ||
| 53 | 16 | int world_size = 0; | |
| 54 | 16 | int world_rank = 0; | |
| 55 | 16 | MPI_Comm_size(MPI_COMM_WORLD, &world_size); | |
| 56 | 16 | MPI_Comm_rank(MPI_COMM_WORLD, &world_rank); | |
| 57 | |||
| 58 | 16 | int total_size = static_cast<int>(GetInput().size()); | |
| 59 | |||
| 60 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 14 times.
|
16 | if (total_size <= world_size) { |
| 61 | 2 | return RunSequentialCase(); | |
| 62 | } | ||
| 63 | 14 | int base_chunk = total_size / world_size; | |
| 64 | 14 | int remainder = total_size % world_size; | |
| 65 | 14 | std::vector<int> send_counts(world_size); | |
| 66 |
1/4✓ Branch 1 taken 14 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
|
14 | std::vector<int> displacements(world_size); |
| 67 | int displacement = 0; | ||
| 68 |
2/2✓ Branch 0 taken 28 times.
✓ Branch 1 taken 14 times.
|
42 | for (int i = 0; i < world_size; i++) { |
| 69 |
2/2✓ Branch 0 taken 14 times.
✓ Branch 1 taken 14 times.
|
42 | send_counts[i] = base_chunk + (i < remainder ? 1 : 0); |
| 70 | 28 | displacements[i] = displacement; | |
| 71 | 28 | displacement += send_counts[i]; | |
| 72 | } | ||
| 73 |
1/2✓ Branch 1 taken 14 times.
✗ Branch 2 not taken.
|
14 | int my_chunk_size = send_counts[world_rank]; |
| 74 |
2/6✓ Branch 1 taken 14 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 14 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
|
14 | std::vector<double> local_data(my_chunk_size); |
| 75 |
1/2✓ Branch 1 taken 14 times.
✗ Branch 2 not taken.
|
14 | MPI_Scatterv(input_data.data(), send_counts.data(), displacements.data(), MPI_DOUBLE, local_data.data(), |
| 76 | my_chunk_size, MPI_DOUBLE, 0, MPI_COMM_WORLD); | ||
| 77 |
1/4✓ Branch 1 taken 14 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
|
14 | std::vector<double> prev_elements(world_size, 0.0); |
| 78 |
2/2✓ Branch 0 taken 7 times.
✓ Branch 1 taken 7 times.
|
14 | if (world_rank == 0) { |
| 79 |
2/2✓ Branch 0 taken 7 times.
✓ Branch 1 taken 7 times.
|
14 | for (int i = 1; i < world_size; i++) { |
| 80 | 7 | int prev_block_end = displacements[i] - 1; | |
| 81 | 7 | prev_elements[i] = input_data[prev_block_end]; | |
| 82 | } | ||
| 83 | } | ||
| 84 |
1/2✓ Branch 1 taken 14 times.
✗ Branch 2 not taken.
|
14 | double my_prev_element = 0.0; |
| 85 |
1/2✓ Branch 1 taken 14 times.
✗ Branch 2 not taken.
|
14 | MPI_Scatter(prev_elements.data(), 1, MPI_DOUBLE, &my_prev_element, 1, MPI_DOUBLE, 0, MPI_COMM_WORLD); |
| 86 | 14 | int local_viol = 0; | |
| 87 |
2/2✓ Branch 0 taken 7 times.
✓ Branch 1 taken 7 times.
|
14 | if (world_rank > 0) { |
| 88 |
2/2✓ Branch 0 taken 5 times.
✓ Branch 1 taken 2 times.
|
12 | local_viol += CountViolation(my_prev_element, local_data[0]); |
| 89 | } | ||
| 90 |
2/2✓ Branch 0 taken 15 times.
✓ Branch 1 taken 14 times.
|
29 | for (int i = 0; i < my_chunk_size - 1; i++) { |
| 91 |
2/2✓ Branch 0 taken 8 times.
✓ Branch 1 taken 7 times.
|
23 | local_viol += CountViolation(local_data[i], local_data[i + 1]); |
| 92 | } | ||
| 93 | 14 | int total_viol = 0; | |
| 94 |
1/2✓ Branch 1 taken 14 times.
✗ Branch 2 not taken.
|
14 | MPI_Allreduce(&local_viol, &total_viol, 1, MPI_INT, MPI_SUM, MPI_COMM_WORLD); |
| 95 | |||
| 96 |
1/2✓ Branch 0 taken 14 times.
✗ Branch 1 not taken.
|
14 | GetOutput() = total_viol; |
| 97 | return true; | ||
| 98 | } | ||
| 99 | |||
| 100 | 20 | bool OlesnitskiyVFindViolMPI::PostProcessingImpl() { | |
| 101 | 20 | return true; | |
| 102 | } | ||
| 103 | |||
| 104 | } // namespace olesnitskiy_v_find_viol | ||
| 105 |