| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include "dorogin_v_min_vector_value/mpi/include/ops_mpi.hpp" | ||
| 2 | |||
| 3 | #include <mpi.h> | ||
| 4 | |||
| 5 | #include <algorithm> | ||
| 6 | #include <limits> | ||
| 7 | #include <vector> | ||
| 8 | |||
| 9 | #include "dorogin_v_min_vector_value/common/include/common.hpp" | ||
| 10 | |||
| 11 | namespace dorogin_v_min_vector_value { | ||
| 12 | |||
| 13 |
1/2✓ Branch 1 taken 88 times.
✗ Branch 2 not taken.
|
88 | DoroginVMinVectorValueMPI::DoroginVMinVectorValueMPI(const InType &in) { |
| 14 | SetTypeOfTask(GetStaticTypeOfTask()); | ||
| 15 |
1/2✓ Branch 1 taken 88 times.
✗ Branch 2 not taken.
|
88 | GetInput() = in; |
| 16 | 88 | GetOutput() = 0; | |
| 17 | 88 | } | |
| 18 | |||
| 19 | 88 | bool DoroginVMinVectorValueMPI::ValidationImpl() { | |
| 20 | 88 | return !GetInput().empty(); | |
| 21 | } | ||
| 22 | |||
| 23 | 88 | bool DoroginVMinVectorValueMPI::PreProcessingImpl() { | |
| 24 | 88 | return true; | |
| 25 | } | ||
| 26 | |||
| 27 | 88 | bool DoroginVMinVectorValueMPI::RunImpl() { | |
| 28 | 88 | int rank = 0; | |
| 29 | 88 | int size = 0; | |
| 30 | 88 | MPI_Comm_rank(MPI_COMM_WORLD, &rank); | |
| 31 | 88 | MPI_Comm_size(MPI_COMM_WORLD, &size); | |
| 32 | |||
| 33 | const auto &data = GetInput(); | ||
| 34 | 88 | const int data_size = static_cast<int>(data.size()); | |
| 35 | |||
| 36 | // Calculate chunk size for each process | ||
| 37 | 88 | const int chunk_size = data_size / size; | |
| 38 | 88 | const int remainder = data_size % size; | |
| 39 |
2/2✓ Branch 0 taken 68 times.
✓ Branch 1 taken 20 times.
|
88 | const int local_size = chunk_size + (rank < remainder ? 1 : 0); |
| 40 | |||
| 41 | // Distribute data: rank 0 sends chunks to other ranks | ||
| 42 | 88 | std::vector<int> local_data(local_size); | |
| 43 |
2/2✓ Branch 0 taken 44 times.
✓ Branch 1 taken 44 times.
|
88 | if (rank == 0) { |
| 44 | // Rank 0 keeps its chunk | ||
| 45 |
2/2✓ Branch 0 taken 546 times.
✓ Branch 1 taken 44 times.
|
590 | for (int i = 0; i < local_size; ++i) { |
| 46 | 546 | local_data[i] = data[i]; | |
| 47 | } | ||
| 48 | // Send chunks to other ranks | ||
| 49 | int offset = local_size; | ||
| 50 |
2/2✓ Branch 0 taken 44 times.
✓ Branch 1 taken 44 times.
|
88 | for (int dest = 1; dest < size; ++dest) { |
| 51 |
2/4✓ Branch 0 taken 44 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 44 times.
✗ Branch 4 not taken.
|
88 | const int dest_size = chunk_size + (dest < remainder ? 1 : 0); |
| 52 |
1/2✓ Branch 1 taken 44 times.
✗ Branch 2 not taken.
|
44 | MPI_Send(data.data() + offset, dest_size, MPI_INT, dest, 0, MPI_COMM_WORLD); |
| 53 | 44 | offset += dest_size; | |
| 54 | } | ||
| 55 | } else { | ||
| 56 | // Other ranks receive their chunk | ||
| 57 |
1/2✓ Branch 1 taken 44 times.
✗ Branch 2 not taken.
|
44 | MPI_Recv(local_data.data(), local_size, MPI_INT, 0, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE); |
| 58 | } | ||
| 59 | |||
| 60 | // Find local minimum | ||
| 61 | 88 | int local_min = 0; | |
| 62 |
2/2✓ Branch 0 taken 86 times.
✓ Branch 1 taken 2 times.
|
88 | if (local_size > 0) { |
| 63 | const auto it_min = std::ranges::min_element(local_data); | ||
| 64 |
1/2✓ Branch 0 taken 86 times.
✗ Branch 1 not taken.
|
86 | local_min = (it_min != local_data.end()) ? *it_min : 0; |
| 65 | } else { | ||
| 66 | 2 | local_min = std::numeric_limits<int>::max(); | |
| 67 | } | ||
| 68 | |||
| 69 | // Reduce to find global minimum | ||
| 70 | 88 | int global_min = 0; | |
| 71 |
1/2✓ Branch 1 taken 88 times.
✗ Branch 2 not taken.
|
88 | MPI_Reduce(&local_min, &global_min, 1, MPI_INT, MPI_MIN, 0, MPI_COMM_WORLD); |
| 72 | |||
| 73 | // Broadcast result to all ranks | ||
| 74 |
1/2✓ Branch 1 taken 88 times.
✗ Branch 2 not taken.
|
88 | MPI_Bcast(&global_min, 1, MPI_INT, 0, MPI_COMM_WORLD); |
| 75 | |||
| 76 |
2/2✓ Branch 0 taken 86 times.
✓ Branch 1 taken 2 times.
|
88 | GetOutput() = global_min; |
| 77 | 88 | return true; | |
| 78 | } | ||
| 79 | |||
| 80 | 88 | bool DoroginVMinVectorValueMPI::PostProcessingImpl() { | |
| 81 | 88 | return true; | |
| 82 | } | ||
| 83 | |||
| 84 | } // namespace dorogin_v_min_vector_value | ||
| 85 |