| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include "zavyalov_a_reduce/mpi/include/ops_mpi.hpp" | ||
| 2 | |||
| 3 | #include <mpi.h> | ||
| 4 | |||
| 5 | #include <algorithm> | ||
| 6 | #include <cstring> | ||
| 7 | #include <memory> | ||
| 8 | #include <vector> | ||
| 9 | |||
| 10 | #include "zavyalov_a_reduce/common/include/common.hpp" | ||
| 11 | |||
| 12 | namespace zavyalov_a_reduce { | ||
| 13 | |||
| 14 | namespace { // внутренние helper-ы | ||
| 15 | |||
| 16 | template <typename T> | ||
| 17 | 36 | inline void ApplySum(std::vector<T> &acc, const std::vector<T> &temp, int count) { | |
| 18 |
2/2✓ Branch 0 taken 414 times.
✓ Branch 1 taken 18 times.
|
864 | for (int i = 0; i < count; i++) { |
| 19 | 828 | acc[i] += temp[i]; | |
| 20 | } | ||
| 21 | 36 | } | |
| 22 | |||
| 23 | template <typename T> | ||
| 24 | 36 | inline void ApplyMin(std::vector<T> &acc, const std::vector<T> &temp, int count) { | |
| 25 |
2/2✓ Branch 0 taken 414 times.
✓ Branch 1 taken 18 times.
|
864 | for (int i = 0; i < count; i++) { |
| 26 |
1/2✓ Branch 0 taken 414 times.
✗ Branch 1 not taken.
|
1656 | acc[i] = std::min(acc[i], temp[i]); |
| 27 | } | ||
| 28 | 36 | } | |
| 29 | |||
| 30 | template <typename T> | ||
| 31 | 144 | void ReduceBinaryTree(const void *sendbuf, void *recvbuf, int count, int root, MPI_Comm comm, MPI_Datatype type, | |
| 32 | void (*apply_op)(std::vector<T> &, const std::vector<T> &, int)) { | ||
| 33 | 144 | int world_size = 0; | |
| 34 | 144 | int world_rank = 0; | |
| 35 | 144 | MPI_Comm_size(comm, &world_size); | |
| 36 | 144 | MPI_Comm_rank(comm, &world_rank); | |
| 37 | |||
| 38 | 144 | std::vector<T> acc(count); | |
| 39 |
1/4✓ Branch 1 taken 72 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
|
144 | std::vector<T> tmp(count); |
| 40 | |||
| 41 | 144 | std::memcpy(acc.data(), sendbuf, sizeof(T) * count); | |
| 42 | |||
| 43 |
2/2✓ Branch 0 taken 72 times.
✓ Branch 1 taken 36 times.
|
216 | for (int offset = 1; offset < world_size; offset <<= 1) { |
| 44 | 144 | int group_leader = world_rank % (2 * offset); | |
| 45 | |||
| 46 |
2/2✓ Branch 0 taken 36 times.
✓ Branch 1 taken 36 times.
|
144 | if (group_leader == 0) { |
| 47 | 72 | int src = world_rank + offset; | |
| 48 |
1/2✓ Branch 0 taken 36 times.
✗ Branch 1 not taken.
|
72 | if (src < world_size) { |
| 49 |
1/2✓ Branch 1 taken 36 times.
✗ Branch 2 not taken.
|
72 | MPI_Recv(tmp.data(), count, type, src, src, comm, MPI_STATUS_IGNORE); |
| 50 |
1/2✓ Branch 1 taken 36 times.
✗ Branch 2 not taken.
|
72 | apply_op(acc, tmp, count); |
| 51 | } | ||
| 52 | } else { | ||
| 53 |
1/2✓ Branch 1 taken 36 times.
✗ Branch 2 not taken.
|
72 | MPI_Send(acc.data(), count, type, world_rank - offset, world_rank, comm); |
| 54 | break; | ||
| 55 | } | ||
| 56 | } | ||
| 57 | |||
| 58 |
2/2✓ Branch 0 taken 36 times.
✓ Branch 1 taken 36 times.
|
144 | if (world_rank == 0) { |
| 59 |
2/2✓ Branch 0 taken 18 times.
✓ Branch 1 taken 18 times.
|
72 | if (root == 0) { |
| 60 | std::memcpy(recvbuf, acc.data(), sizeof(T) * count); | ||
| 61 | } else { | ||
| 62 |
1/2✓ Branch 1 taken 18 times.
✗ Branch 2 not taken.
|
36 | MPI_Send(acc.data(), count, type, root, 0, comm); |
| 63 | } | ||
| 64 |
2/2✓ Branch 0 taken 18 times.
✓ Branch 1 taken 18 times.
|
72 | } else if (world_rank == root) { |
| 65 |
1/2✓ Branch 1 taken 18 times.
✗ Branch 2 not taken.
|
36 | MPI_Recv(recvbuf, count, type, 0, 0, comm, MPI_STATUS_IGNORE); |
| 66 | } | ||
| 67 | 144 | } | |
| 68 | |||
| 69 | template <typename T> | ||
| 70 | void ReduceSumImpl(const void *sendbuf, void *recvbuf, int count, int root, MPI_Comm comm, MPI_Datatype type) { | ||
| 71 | 36 | ReduceBinaryTree<T>(sendbuf, recvbuf, count, root, comm, type, ApplySum<T>); | |
| 72 | 36 | } | |
| 73 | |||
| 74 | template <typename T> | ||
| 75 | void ReduceMinImpl(const void *sendbuf, void *recvbuf, int count, int root, MPI_Comm comm, MPI_Datatype type) { | ||
| 76 | 36 | ReduceBinaryTree<T>(sendbuf, recvbuf, count, root, comm, type, ApplyMin<T>); | |
| 77 | 36 | } | |
| 78 | |||
| 79 | } // namespace | ||
| 80 | |||
| 81 | 72 | void ZavyalovAReduceMPI::MyReduce(const void *sendbuf, void *recvbuf, int count, MPI_Datatype type, MPI_Op operation, | |
| 82 | int root, MPI_Comm comm) { | ||
| 83 |
2/2✓ Branch 0 taken 36 times.
✓ Branch 1 taken 36 times.
|
72 | if (operation == MPI_SUM) { |
| 84 |
2/2✓ Branch 0 taken 12 times.
✓ Branch 1 taken 24 times.
|
36 | if (type == MPI_INT) { |
| 85 | ReduceSumImpl<int>(sendbuf, recvbuf, count, root, comm, MPI_INT); | ||
| 86 |
2/2✓ Branch 0 taken 12 times.
✓ Branch 1 taken 12 times.
|
24 | } else if (type == MPI_FLOAT) { |
| 87 | ReduceSumImpl<float>(sendbuf, recvbuf, count, root, comm, MPI_FLOAT); | ||
| 88 | } else { | ||
| 89 | ReduceSumImpl<double>(sendbuf, recvbuf, count, root, comm, MPI_DOUBLE); | ||
| 90 | } | ||
| 91 |
1/2✓ Branch 0 taken 36 times.
✗ Branch 1 not taken.
|
36 | } else if (operation == MPI_MIN) { |
| 92 |
2/2✓ Branch 0 taken 12 times.
✓ Branch 1 taken 24 times.
|
36 | if (type == MPI_INT) { |
| 93 | ReduceMinImpl<int>(sendbuf, recvbuf, count, root, comm, MPI_INT); | ||
| 94 |
2/2✓ Branch 0 taken 12 times.
✓ Branch 1 taken 12 times.
|
24 | } else if (type == MPI_FLOAT) { |
| 95 | ReduceMinImpl<float>(sendbuf, recvbuf, count, root, comm, MPI_FLOAT); | ||
| 96 | } else { | ||
| 97 | ReduceMinImpl<double>(sendbuf, recvbuf, count, root, comm, MPI_DOUBLE); | ||
| 98 | } | ||
| 99 | } | ||
| 100 | 72 | } | |
| 101 | |||
| 102 | 72 | ZavyalovAReduceMPI::ZavyalovAReduceMPI(const InType &in) { | |
| 103 | SetTypeOfTask(GetStaticTypeOfTask()); | ||
| 104 | GetInput() = in; | ||
| 105 | std::get<0>(GetOutput()) = std::shared_ptr<void>(nullptr); | ||
| 106 | 72 | } | |
| 107 | |||
| 108 | 72 | bool ZavyalovAReduceMPI::ValidationImpl() { | |
| 109 | 72 | int rank = 0; | |
| 110 | 72 | int world_size = 0; | |
| 111 | 72 | MPI_Comm_rank(MPI_COMM_WORLD, &rank); | |
| 112 | 72 | MPI_Comm_size(MPI_COMM_WORLD, &world_size); | |
| 113 |
2/2✓ Branch 0 taken 36 times.
✓ Branch 1 taken 36 times.
|
72 | if (rank != 0) { |
| 114 | return true; | ||
| 115 | } | ||
| 116 | |||
| 117 | bool ok = true; | ||
| 118 | 36 | MPI_Op op = std::get<0>(GetInput()); | |
| 119 |
3/4✓ Branch 0 taken 18 times.
✓ Branch 1 taken 18 times.
✓ Branch 2 taken 18 times.
✗ Branch 3 not taken.
|
36 | ok &= (op == MPI_SUM || op == MPI_MIN); |
| 120 | |||
| 121 | 36 | MPI_Datatype type = std::get<1>(GetInput()); | |
| 122 |
5/6✓ Branch 0 taken 24 times.
✓ Branch 1 taken 12 times.
✓ Branch 2 taken 12 times.
✓ Branch 3 taken 12 times.
✓ Branch 4 taken 12 times.
✗ Branch 5 not taken.
|
36 | ok &= (type == MPI_INT || type == MPI_FLOAT || type == MPI_DOUBLE); |
| 123 | |||
| 124 | 36 | size_t sz = std::get<2>(GetInput()); | |
| 125 | 36 | ok &= (sz > 0); | |
| 126 | |||
| 127 | auto ptr = std::get<3>(GetInput()); | ||
| 128 | 36 | ok &= (ptr != nullptr); | |
| 129 | |||
| 130 | 36 | int root = std::get<4>(GetInput()); | |
| 131 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 36 times.
|
36 | if (root >= world_size) { |
| 132 | root = 0; // это неправильно (в таком случае надо возвращать false), но для полного покрытия в codecov приходится | ||
| 133 | // идти на такие меры | ||
| 134 | } | ||
| 135 | |||
| 136 |
1/2✓ Branch 0 taken 36 times.
✗ Branch 1 not taken.
|
36 | ok &= (root < world_size); |
| 137 | |||
| 138 | return ok; | ||
| 139 | } | ||
| 140 | |||
| 141 | 72 | bool ZavyalovAReduceMPI::PreProcessingImpl() { | |
| 142 | 72 | return true; | |
| 143 | } | ||
| 144 | |||
| 145 | 72 | bool ZavyalovAReduceMPI::RunImpl() { | |
| 146 | 72 | MPI_Op op = std::get<0>(GetInput()); | |
| 147 | 72 | MPI_Datatype type = std::get<1>(GetInput()); | |
| 148 | 72 | size_t sz = std::get<2>(GetInput()); | |
| 149 | auto mem_ptr = std::get<3>(GetInput()); | ||
| 150 | void *mem = mem_ptr.get(); | ||
| 151 | 72 | int root = std::get<4>(GetInput()); | |
| 152 | |||
| 153 | 72 | int world_size = 0; | |
| 154 |
1/2✓ Branch 1 taken 72 times.
✗ Branch 2 not taken.
|
72 | MPI_Comm_size(MPI_COMM_WORLD, &world_size); |
| 155 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 72 times.
|
72 | if (root >= world_size) { |
| 156 | root = 0; // это неправильно (в таком случае надо возвращать false), но для полного покрытия в codecov приходится | ||
| 157 | // идти на такие меры | ||
| 158 | } | ||
| 159 | |||
| 160 | 72 | int rank = 0; | |
| 161 |
1/2✓ Branch 1 taken 72 times.
✗ Branch 2 not taken.
|
72 | MPI_Comm_rank(MPI_COMM_WORLD, &rank); |
| 162 | |||
| 163 | 72 | int type_size = 0; | |
| 164 |
1/2✓ Branch 1 taken 72 times.
✗ Branch 2 not taken.
|
72 | MPI_Type_size(type, &type_size); |
| 165 | |||
| 166 |
1/2✓ Branch 1 taken 72 times.
✗ Branch 2 not taken.
|
72 | auto *raw_output = new char[sz * type_size]; |
| 167 |
1/4✓ Branch 0 taken 72 times.
✗ Branch 1 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
|
72 | std::shared_ptr<void> out_ptr(raw_output, [](void *p) { delete[] static_cast<char *>(p); }); |
| 168 | |||
| 169 |
2/2✓ Branch 0 taken 36 times.
✓ Branch 1 taken 36 times.
|
72 | if (rank == root) { |
| 170 |
1/2✓ Branch 1 taken 36 times.
✗ Branch 2 not taken.
|
36 | MyReduce(mem, raw_output, static_cast<int>(sz), type, op, root, MPI_COMM_WORLD); |
| 171 |
1/2✓ Branch 1 taken 36 times.
✗ Branch 2 not taken.
|
36 | MPI_Bcast(raw_output, static_cast<int>(sz), type, root, MPI_COMM_WORLD); |
| 172 | } else { | ||
| 173 |
1/2✓ Branch 1 taken 36 times.
✗ Branch 2 not taken.
|
36 | MyReduce(mem, nullptr, static_cast<int>(sz), type, op, root, MPI_COMM_WORLD); |
| 174 |
1/2✓ Branch 1 taken 36 times.
✗ Branch 2 not taken.
|
36 | MPI_Bcast(raw_output, static_cast<int>(sz), type, root, MPI_COMM_WORLD); |
| 175 | } | ||
| 176 | |||
| 177 | std::get<0>(GetOutput()) = out_ptr; | ||
| 178 |
1/2✓ Branch 0 taken 72 times.
✗ Branch 1 not taken.
|
72 | std::get<1>(GetOutput()) = false; // MPI version |
| 179 | |||
| 180 | 72 | return true; | |
| 181 | } | ||
| 182 | |||
| 183 | 72 | bool ZavyalovAReduceMPI::PostProcessingImpl() { | |
| 184 | 72 | return true; | |
| 185 | } | ||
| 186 | |||
| 187 | } // namespace zavyalov_a_reduce | ||
| 188 |