| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include "redkina_a_ruler/mpi/include/ops_mpi.hpp" | ||
| 2 | |||
| 3 | #include <mpi.h> | ||
| 4 | |||
| 5 | #include <cstddef> | ||
| 6 | #include <cstdint> | ||
| 7 | #include <vector> | ||
| 8 | |||
| 9 | #include "redkina_a_ruler/common/include/common.hpp" | ||
| 10 | |||
| 11 | namespace redkina_a_ruler { | ||
| 12 | |||
| 13 | namespace { | ||
| 14 | |||
| 15 | bool IsInStartEndChain(const int rank, const int start, const int end, const bool go_right) { | ||
| 16 |
2/2✓ Branch 0 taken 48 times.
✓ Branch 1 taken 36 times.
|
84 | if (go_right) { |
| 17 | 48 | return (rank > start && rank <= end); | |
| 18 | } | ||
| 19 | 36 | return (rank < start && rank >= end); | |
| 20 | } | ||
| 21 | |||
| 22 | 42 | void SendSizeAndData(const int end_rank, const uint64_t data_size, const std::vector<int> &data, const int size_tag = 0, | |
| 23 | const int data_tag = 1) { | ||
| 24 |
1/2✓ Branch 0 taken 42 times.
✗ Branch 1 not taken.
|
42 | if (end_rank == MPI_PROC_NULL) { |
| 25 | return; | ||
| 26 | } | ||
| 27 | |||
| 28 | 42 | MPI_Send(&data_size, 1, MPI_UINT64_T, end_rank, size_tag, MPI_COMM_WORLD); | |
| 29 |
1/2✓ Branch 0 taken 42 times.
✗ Branch 1 not taken.
|
42 | if (data_size > 0U) { |
| 30 | 42 | MPI_Send(data.data(), static_cast<int>(data_size), MPI_INT, end_rank, data_tag, MPI_COMM_WORLD); | |
| 31 | } | ||
| 32 | } | ||
| 33 | |||
| 34 | 42 | void ReceiveSizeAndData(const int start_rank, uint64_t &data_size, std::vector<int> &data, const int size_tag = 0, | |
| 35 | const int data_tag = 1) { | ||
| 36 |
1/2✓ Branch 0 taken 42 times.
✗ Branch 1 not taken.
|
42 | if (start_rank == MPI_PROC_NULL) { |
| 37 | return; | ||
| 38 | } | ||
| 39 | |||
| 40 | 42 | MPI_Recv(&data_size, 1, MPI_UINT64_T, start_rank, size_tag, MPI_COMM_WORLD, MPI_STATUS_IGNORE); | |
| 41 |
1/2✓ Branch 0 taken 42 times.
✗ Branch 1 not taken.
|
42 | if (data_size > 0U) { |
| 42 | 42 | data.resize(static_cast<std::size_t>(data_size)); | |
| 43 | 42 | MPI_Recv(data.data(), static_cast<int>(data_size), MPI_INT, start_rank, data_tag, MPI_COMM_WORLD, | |
| 44 | MPI_STATUS_IGNORE); | ||
| 45 | } | ||
| 46 | } | ||
| 47 | |||
| 48 | 84 | void BroadcastEndResult(const int rank, const int root, std::vector<int> &output) { | |
| 49 | 84 | uint64_t data_size = 0; | |
| 50 | |||
| 51 |
2/2✓ Branch 0 taken 42 times.
✓ Branch 1 taken 42 times.
|
84 | if (rank == root) { |
| 52 | 42 | data_size = static_cast<uint64_t>(output.size()); | |
| 53 | } | ||
| 54 | |||
| 55 | 84 | MPI_Bcast(&data_size, 1, MPI_UINT64_T, root, MPI_COMM_WORLD); | |
| 56 | |||
| 57 |
2/2✓ Branch 0 taken 42 times.
✓ Branch 1 taken 42 times.
|
84 | if (rank != root) { |
| 58 | 42 | output.resize(static_cast<std::size_t>(data_size)); | |
| 59 | } | ||
| 60 | |||
| 61 |
1/2✓ Branch 0 taken 84 times.
✗ Branch 1 not taken.
|
84 | if (data_size > 0U) { |
| 62 | 84 | MPI_Bcast(output.data(), static_cast<int>(data_size), MPI_INT, root, MPI_COMM_WORLD); | |
| 63 | } | ||
| 64 | 84 | } | |
| 65 | |||
| 66 | 24 | void HandleSameStartEnd(const int rank, const int start, const std::vector<int> &input_data, std::vector<int> &output) { | |
| 67 |
2/2✓ Branch 0 taken 12 times.
✓ Branch 1 taken 12 times.
|
24 | if (rank == start) { |
| 68 | 12 | output = input_data; | |
| 69 | } | ||
| 70 | |||
| 71 | 24 | auto data_size = static_cast<uint64_t>(input_data.size()); | |
| 72 | 24 | MPI_Bcast(&data_size, 1, MPI_UINT64_T, start, MPI_COMM_WORLD); | |
| 73 | |||
| 74 |
2/2✓ Branch 0 taken 12 times.
✓ Branch 1 taken 12 times.
|
24 | if (rank != start) { |
| 75 | 12 | output.resize(static_cast<std::size_t>(data_size)); | |
| 76 | } | ||
| 77 | |||
| 78 |
1/2✓ Branch 0 taken 24 times.
✗ Branch 1 not taken.
|
24 | if (data_size > 0U) { |
| 79 | 24 | MPI_Bcast(output.data(), static_cast<int>(data_size), MPI_INT, start, MPI_COMM_WORLD); | |
| 80 | } | ||
| 81 | 24 | } | |
| 82 | |||
| 83 | 84 | void RouteDataStartEnd(const int rank, const int start, const int end, const bool go_right, const int left_n, | |
| 84 | const int right_n, const std::vector<int> &input_data, std::vector<int> &output) { | ||
| 85 | 84 | std::vector<int> buffer; | |
| 86 | 84 | uint64_t data_size = 0; | |
| 87 | |||
| 88 |
2/2✓ Branch 0 taken 42 times.
✓ Branch 1 taken 42 times.
|
84 | if (rank == start) { |
| 89 |
1/2✓ Branch 1 taken 42 times.
✗ Branch 2 not taken.
|
42 | buffer = input_data; |
| 90 | 42 | data_size = static_cast<uint64_t>(buffer.size()); | |
| 91 |
2/2✓ Branch 0 taken 18 times.
✓ Branch 1 taken 24 times.
|
42 | const int target = go_right ? right_n : left_n; |
| 92 |
1/2✓ Branch 1 taken 42 times.
✗ Branch 2 not taken.
|
42 | SendSizeAndData(target, data_size, buffer); |
| 93 | } | ||
| 94 | |||
| 95 |
2/2✓ Branch 0 taken 42 times.
✓ Branch 1 taken 42 times.
|
84 | if (!IsInStartEndChain(rank, start, end, go_right)) { |
| 96 | return; | ||
| 97 | } | ||
| 98 | |||
| 99 |
2/2✓ Branch 0 taken 18 times.
✓ Branch 1 taken 24 times.
|
42 | const int recv_from = go_right ? left_n : right_n; |
| 100 | const int send_to = go_right ? right_n : left_n; | ||
| 101 | |||
| 102 |
1/2✓ Branch 1 taken 42 times.
✗ Branch 2 not taken.
|
42 | ReceiveSizeAndData(recv_from, data_size, buffer); |
| 103 | |||
| 104 |
1/2✓ Branch 0 taken 42 times.
✗ Branch 1 not taken.
|
42 | if (rank == end) { |
| 105 |
1/2✓ Branch 1 taken 42 times.
✗ Branch 2 not taken.
|
42 | output = buffer; |
| 106 | } else { | ||
| 107 | ✗ | SendSizeAndData(send_to, data_size, buffer); | |
| 108 | } | ||
| 109 | } | ||
| 110 | |||
| 111 | } // namespace | ||
| 112 | |||
| 113 |
1/2✓ Branch 1 taken 108 times.
✗ Branch 2 not taken.
|
108 | RedkinaARulerMPI::RedkinaARulerMPI(const InType &in) { |
| 114 | SetTypeOfTask(GetStaticTypeOfTask()); | ||
| 115 | GetInput() = in; | ||
| 116 | GetOutput().clear(); | ||
| 117 | 108 | } | |
| 118 | |||
| 119 | 108 | bool RedkinaARulerMPI::ValidationImpl() { | |
| 120 | const auto &input = GetInput(); | ||
| 121 |
3/6✓ Branch 0 taken 108 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 108 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 108 times.
|
108 | return input.start >= 0 && input.end >= 0 && !input.data.empty(); |
| 122 | } | ||
| 123 | |||
| 124 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 108 times.
|
108 | bool RedkinaARulerMPI::PreProcessingImpl() { |
| 125 | GetOutput().clear(); | ||
| 126 | 108 | return true; | |
| 127 | } | ||
| 128 | |||
| 129 | 108 | bool RedkinaARulerMPI::RunImpl() { | |
| 130 | 108 | int rank = 0; | |
| 131 | 108 | int size = 0; | |
| 132 | 108 | MPI_Comm_rank(MPI_COMM_WORLD, &rank); | |
| 133 | 108 | MPI_Comm_size(MPI_COMM_WORLD, &size); | |
| 134 | |||
| 135 | const auto &input = GetInput(); | ||
| 136 | 108 | const int start = input.start; | |
| 137 | 108 | const int end = input.end; | |
| 138 | |||
| 139 |
4/8✓ Branch 0 taken 108 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 108 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 108 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 108 times.
✗ Branch 7 not taken.
|
108 | if (start < 0 || start >= size || end < 0 || end >= size) { |
| 140 | return false; | ||
| 141 | } | ||
| 142 | |||
| 143 |
2/2✓ Branch 0 taken 24 times.
✓ Branch 1 taken 84 times.
|
108 | if (start == end) { |
| 144 | 24 | HandleSameStartEnd(rank, start, input.data, GetOutput()); | |
| 145 | 24 | return true; | |
| 146 | } | ||
| 147 | |||
| 148 | 84 | const bool go_right = end > start; | |
| 149 |
2/2✓ Branch 0 taken 42 times.
✓ Branch 1 taken 42 times.
|
84 | const int left_n = (rank > 0) ? (rank - 1) : MPI_PROC_NULL; |
| 150 |
2/2✓ Branch 0 taken 42 times.
✓ Branch 1 taken 42 times.
|
84 | const int right_n = (rank + 1 < size) ? (rank + 1) : MPI_PROC_NULL; |
| 151 | |||
| 152 | 84 | RouteDataStartEnd(rank, start, end, go_right, left_n, right_n, input.data, GetOutput()); | |
| 153 | |||
| 154 | 84 | BroadcastEndResult(rank, end, GetOutput()); | |
| 155 | return true; | ||
| 156 | } | ||
| 157 | |||
| 158 | 108 | bool RedkinaARulerMPI::PostProcessingImpl() { | |
| 159 | 108 | return true; | |
| 160 | } | ||
| 161 | |||
| 162 | } // namespace redkina_a_ruler | ||
| 163 |