| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include "morozova_s_broadcast/mpi/include/ops_mpi.hpp" | ||
| 2 | |||
| 3 | #include <mpi.h> | ||
| 4 | |||
| 5 | #include <algorithm> | ||
| 6 | #include <cstddef> | ||
| 7 | |||
| 8 | #include "morozova_s_broadcast/common/include/common.hpp" | ||
| 9 | #include "task/include/task.hpp" | ||
| 10 | |||
| 11 | namespace morozova_s_broadcast { | ||
| 12 | |||
| 13 | 112 | ppc::task::TypeOfTask MorozovaSBroadcastMPI::GetStaticTypeOfTask() { | |
| 14 | 112 | return ppc::task::TypeOfTask::kMPI; | |
| 15 | } | ||
| 16 | |||
| 17 |
1/2✓ Branch 1 taken 8 times.
✗ Branch 2 not taken.
|
8 | MorozovaSBroadcastMPI::MorozovaSBroadcastMPI(const InType &in) : root_(0) { |
| 18 | SetTypeOfTask(GetStaticTypeOfTask()); | ||
| 19 |
1/2✓ Branch 1 taken 8 times.
✗ Branch 2 not taken.
|
8 | GetInput() = in; |
| 20 | 8 | } | |
| 21 | |||
| 22 | ✗ | MorozovaSBroadcastMPI::MorozovaSBroadcastMPI(const InType &in, int root) : root_(root) { | |
| 23 | SetTypeOfTask(GetStaticTypeOfTask()); | ||
| 24 | ✗ | GetInput() = in; | |
| 25 | ✗ | } | |
| 26 | |||
| 27 | 8 | bool MorozovaSBroadcastMPI::ValidationImpl() { | |
| 28 | 8 | int size = 0; | |
| 29 | 8 | MPI_Comm_size(MPI_COMM_WORLD, &size); | |
| 30 |
2/4✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 8 times.
✗ Branch 3 not taken.
|
8 | if (root_ < 0 || root_ >= size) { |
| 31 | return false; | ||
| 32 | } | ||
| 33 | 8 | int rank = 0; | |
| 34 | 8 | MPI_Comm_rank(MPI_COMM_WORLD, &rank); | |
| 35 |
2/2✓ Branch 0 taken 4 times.
✓ Branch 1 taken 4 times.
|
8 | if (rank == root_) { |
| 36 | 4 | return !GetInput().empty(); | |
| 37 | } | ||
| 38 | return true; | ||
| 39 | } | ||
| 40 | |||
| 41 | 8 | bool MorozovaSBroadcastMPI::PreProcessingImpl() { | |
| 42 | 8 | return true; | |
| 43 | } | ||
| 44 | |||
| 45 | 8 | bool MorozovaSBroadcastMPI::RunImpl() { | |
| 46 | 8 | int rank = 0; | |
| 47 | 8 | MPI_Comm_rank(MPI_COMM_WORLD, &rank); | |
| 48 | 8 | int data_size = 0; | |
| 49 |
2/2✓ Branch 0 taken 4 times.
✓ Branch 1 taken 4 times.
|
8 | if (rank == root_) { |
| 50 | 4 | data_size = static_cast<int>(GetInput().size()); | |
| 51 | } | ||
| 52 | 8 | CustomBroadcast(&data_size, 1, MPI_INT, root_, MPI_COMM_WORLD); | |
| 53 | 8 | GetOutput().resize(static_cast<size_t>(data_size)); | |
| 54 |
1/2✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
|
8 | if (data_size > 0) { |
| 55 |
2/2✓ Branch 0 taken 4 times.
✓ Branch 1 taken 4 times.
|
8 | if (rank == root_) { |
| 56 | 4 | std::copy(GetInput().begin(), GetInput().end(), GetOutput().begin()); | |
| 57 | } | ||
| 58 | 8 | CustomBroadcast(GetOutput().data(), data_size, MPI_INT, root_, MPI_COMM_WORLD); | |
| 59 | } | ||
| 60 | 8 | return true; | |
| 61 | } | ||
| 62 | |||
| 63 | 8 | bool MorozovaSBroadcastMPI::PostProcessingImpl() { | |
| 64 | 8 | return true; | |
| 65 | } | ||
| 66 | |||
| 67 | 16 | void MorozovaSBroadcastMPI::CustomBroadcast(void *buffer, int count, MPI_Datatype type, int root, MPI_Comm comm) { | |
| 68 | 16 | int rank = 0; | |
| 69 | 16 | int size = 0; | |
| 70 | 16 | MPI_Comm_rank(comm, &rank); | |
| 71 | 16 | MPI_Comm_size(comm, &size); | |
| 72 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 16 times.
|
16 | if (size <= 1) { |
| 73 | ✗ | return; | |
| 74 | } | ||
| 75 | 16 | int vrank = (rank - root + size) % size; | |
| 76 |
2/2✓ Branch 0 taken 16 times.
✓ Branch 1 taken 16 times.
|
32 | for (int step = 1; step < size; step <<= 1) { |
| 77 |
2/2✓ Branch 0 taken 8 times.
✓ Branch 1 taken 8 times.
|
16 | if (vrank < step) { |
| 78 | 8 | int dst_vrank = vrank + step; | |
| 79 |
1/2✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
|
8 | if (dst_vrank < size) { |
| 80 | 8 | int dst_rank = (dst_vrank + root) % size; | |
| 81 | 8 | MPI_Send(buffer, count, type, dst_rank, 0, comm); | |
| 82 | } | ||
| 83 |
1/2✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
|
8 | } else if (vrank < 2 * step) { |
| 84 | 8 | int src_vrank = vrank - step; | |
| 85 | 8 | int src_rank = (src_vrank + root) % size; | |
| 86 | 8 | MPI_Recv(buffer, count, type, src_rank, 0, comm, MPI_STATUS_IGNORE); | |
| 87 | } | ||
| 88 | } | ||
| 89 | } | ||
| 90 | |||
| 91 | } // namespace morozova_s_broadcast | ||
| 92 |