| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include "badanov_a_max_vec_elem/mpi/include/ops_mpi.hpp" | ||
| 2 | |||
| 3 | #include <mpi.h> | ||
| 4 | |||
| 5 | #include <algorithm> | ||
| 6 | #include <climits> | ||
| 7 | #include <vector> | ||
| 8 | |||
| 9 | #include "badanov_a_max_vec_elem/common/include/common.hpp" | ||
| 10 | |||
| 11 | namespace badanov_a_max_vec_elem { | ||
| 12 | |||
| 13 |
1/2✓ Branch 1 taken 48 times.
✗ Branch 2 not taken.
|
48 | BadanovAMaxVecElemMPI::BadanovAMaxVecElemMPI(const InType &in) { |
| 14 | SetTypeOfTask(GetStaticTypeOfTask()); | ||
| 15 | 48 | int rank = 0; | |
| 16 |
1/2✓ Branch 1 taken 48 times.
✗ Branch 2 not taken.
|
48 | MPI_Comm_rank(MPI_COMM_WORLD, &rank); |
| 17 | |||
| 18 |
2/2✓ Branch 0 taken 24 times.
✓ Branch 1 taken 24 times.
|
48 | if (rank == 0) { |
| 19 |
1/2✓ Branch 1 taken 24 times.
✗ Branch 2 not taken.
|
24 | GetInput() = in; |
| 20 | } else { | ||
| 21 | 24 | GetInput() = InType(); | |
| 22 | } | ||
| 23 | |||
| 24 | 48 | GetOutput() = 0; | |
| 25 | 48 | } | |
| 26 | |||
| 27 | 48 | bool BadanovAMaxVecElemMPI::ValidationImpl() { | |
| 28 | 48 | int rank = 0; | |
| 29 | 48 | MPI_Comm_rank(MPI_COMM_WORLD, &rank); | |
| 30 | |||
| 31 | if (rank == 0) { | ||
| 32 | return true; | ||
| 33 | } | ||
| 34 | |||
| 35 | return true; | ||
| 36 | } | ||
| 37 | |||
| 38 | 48 | bool BadanovAMaxVecElemMPI::PreProcessingImpl() { | |
| 39 | 48 | return true; | |
| 40 | } | ||
| 41 | |||
| 42 | 48 | bool BadanovAMaxVecElemMPI::RunImpl() { | |
| 43 | 48 | int rank = 0; | |
| 44 | 48 | int world_size = 0; | |
| 45 | 48 | MPI_Comm_rank(MPI_COMM_WORLD, &rank); | |
| 46 | 48 | MPI_Comm_size(MPI_COMM_WORLD, &world_size); | |
| 47 | |||
| 48 | 48 | int total_elem = 0; | |
| 49 |
2/2✓ Branch 0 taken 24 times.
✓ Branch 1 taken 24 times.
|
48 | if (rank == 0) { |
| 50 | 24 | total_elem = static_cast<int>(GetInput().size()); | |
| 51 | } | ||
| 52 | |||
| 53 | 48 | MPI_Bcast(&total_elem, 1, MPI_INT, 0, MPI_COMM_WORLD); | |
| 54 | |||
| 55 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 48 times.
|
48 | if (total_elem == 0) { |
| 56 | ✗ | GetOutput() = INT_MIN; | |
| 57 | ✗ | return true; | |
| 58 | } | ||
| 59 | |||
| 60 | 48 | int base_size = total_elem / world_size; | |
| 61 | 48 | int remainder = total_elem % world_size; | |
| 62 | |||
| 63 | 48 | std::vector<int> local_sizes(world_size); | |
| 64 |
1/4✓ Branch 1 taken 48 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
|
48 | std::vector<int> displacements(world_size); |
| 65 | |||
| 66 |
2/2✓ Branch 0 taken 24 times.
✓ Branch 1 taken 24 times.
|
48 | if (rank == 0) { |
| 67 | int offset = 0; | ||
| 68 |
2/2✓ Branch 0 taken 48 times.
✓ Branch 1 taken 24 times.
|
72 | for (int i = 0; i < world_size; ++i) { |
| 69 |
2/2✓ Branch 0 taken 36 times.
✓ Branch 1 taken 12 times.
|
84 | local_sizes[i] = base_size + (i < remainder ? 1 : 0); |
| 70 | 48 | displacements[i] = offset; | |
| 71 | 48 | offset += local_sizes[i]; | |
| 72 | } | ||
| 73 | } | ||
| 74 | |||
| 75 |
2/2✓ Branch 0 taken 36 times.
✓ Branch 1 taken 12 times.
|
48 | int local_size = base_size + (rank < remainder ? 1 : 0); |
| 76 |
3/6✓ Branch 1 taken 48 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 24 times.
✓ Branch 4 taken 24 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
|
48 | std::vector<int> local_data(local_size); |
| 77 | |||
| 78 |
3/4✓ Branch 0 taken 24 times.
✓ Branch 1 taken 24 times.
✓ Branch 3 taken 48 times.
✗ Branch 4 not taken.
|
72 | MPI_Scatterv(rank == 0 ? GetInput().data() : nullptr, local_sizes.data(), displacements.data(), MPI_INT, |
| 79 | local_data.data(), local_size, MPI_INT, 0, MPI_COMM_WORLD); | ||
| 80 | |||
| 81 | 48 | int max_elem_local = INT_MIN; | |
| 82 |
2/2✓ Branch 0 taken 2112 times.
✓ Branch 1 taken 48 times.
|
2160 | for (int i = 0; i < local_size; ++i) { |
| 83 |
2/2✓ Branch 0 taken 26 times.
✓ Branch 1 taken 2086 times.
|
2138 | max_elem_local = std::max(local_data[i], max_elem_local); |
| 84 | } | ||
| 85 | |||
| 86 | 48 | int max_elem_global = INT_MIN; | |
| 87 |
1/2✓ Branch 1 taken 48 times.
✗ Branch 2 not taken.
|
48 | MPI_Allreduce(&max_elem_local, &max_elem_global, 1, MPI_INT, MPI_MAX, MPI_COMM_WORLD); |
| 88 | |||
| 89 |
2/2✓ Branch 0 taken 46 times.
✓ Branch 1 taken 2 times.
|
48 | GetOutput() = max_elem_global; |
| 90 | return true; | ||
| 91 | } | ||
| 92 | |||
| 93 | 48 | bool BadanovAMaxVecElemMPI::PostProcessingImpl() { | |
| 94 | 48 | return true; | |
| 95 | } | ||
| 96 | |||
| 97 | } // namespace badanov_a_max_vec_elem | ||
| 98 |