| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include "frolova_s_star_topology/mpi/include/ops_mpi.hpp" | ||
| 2 | |||
| 3 | #include <mpi.h> | ||
| 4 | |||
| 5 | #include <vector> | ||
| 6 | |||
| 7 | #include "frolova_s_star_topology/common/include/common.hpp" | ||
| 8 | |||
| 9 | constexpr int kTerm = -1; // terminating parameter | ||
| 10 | |||
| 11 | namespace frolova_s_star_topology { | ||
| 12 | |||
| 13 | 6 | FrolovaSStarTopologyMPI::FrolovaSStarTopologyMPI(const InType &in) { | |
| 14 | SetTypeOfTask(GetStaticTypeOfTask()); | ||
| 15 | 6 | GetInput() = in; | |
| 16 | GetOutput() = 0; | ||
| 17 | 6 | } | |
| 18 | |||
| 19 | 6 | bool FrolovaSStarTopologyMPI::ValidationImpl() { | |
| 20 | // int rank = 0; | ||
| 21 | // int size = 0; | ||
| 22 | // MPI_Comm_rank(MPI_COMM_WORLD, &rank); | ||
| 23 | // MPI_Comm_size(MPI_COMM_WORLD, &size); | ||
| 24 | // return size >= 2 && (rank == 0 || GetInput() > 0); | ||
| 25 | 6 | return true; | |
| 26 | } | ||
| 27 | |||
| 28 | 6 | bool FrolovaSStarTopologyMPI::PreProcessingImpl() { | |
| 29 | // int rank = 0; | ||
| 30 | // int size = 0; | ||
| 31 | // MPI_Comm_rank(MPI_COMM_WORLD, &rank); | ||
| 32 | // MPI_Comm_size(MPI_COMM_WORLD, &size); | ||
| 33 | |||
| 34 | // if (rank != 0) { | ||
| 35 | // dest_ = GetInput(); | ||
| 36 | |||
| 37 | // // Гарантируем, что dest_ в диапазоне [1, size-1] | ||
| 38 | // if (dest_ < 1 || dest_ >= size) { | ||
| 39 | // dest_ = 1; // По умолчанию процесс 1 | ||
| 40 | // } | ||
| 41 | |||
| 42 | // // Не отправляем самому себе | ||
| 43 | // if (dest_ == rank) { | ||
| 44 | // dest_ = (dest_ == 1) ? 2 : 1; | ||
| 45 | // if (dest_ >= size) { | ||
| 46 | // dest_ = 1; | ||
| 47 | // } | ||
| 48 | // } | ||
| 49 | |||
| 50 | // output_.resize(GetInput()); | ||
| 51 | // } | ||
| 52 | |||
| 53 | // return true; | ||
| 54 | 6 | int rank = 0; | |
| 55 | 6 | int size = 0; | |
| 56 | 6 | MPI_Comm_rank(MPI_COMM_WORLD, &rank); | |
| 57 | 6 | MPI_Comm_size(MPI_COMM_WORLD, &size); | |
| 58 | |||
| 59 |
2/2✓ Branch 0 taken 3 times.
✓ Branch 1 taken 3 times.
|
6 | if (rank != 0) { |
| 60 | 3 | int data_size = GetInput(); | |
| 61 | |||
| 62 | 3 | dest_ = 1 + (rank % (size - 1)); | |
| 63 |
1/2✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
|
3 | if (dest_ == rank) { |
| 64 | 3 | dest_ = (dest_ + 1) % size; | |
| 65 | } | ||
| 66 |
1/2✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
|
3 | if (dest_ == 0) { |
| 67 | 3 | dest_ = 1; | |
| 68 | } | ||
| 69 | |||
| 70 | 3 | data_.resize(data_size); | |
| 71 | 3 | output_.resize(data_size); | |
| 72 | |||
| 73 |
2/2✓ Branch 0 taken 6 times.
✓ Branch 1 taken 3 times.
|
9 | for (int i = 0; i < data_size; ++i) { |
| 74 | 6 | data_[i] = i; | |
| 75 | } | ||
| 76 | } | ||
| 77 | |||
| 78 | 6 | return true; | |
| 79 | } | ||
| 80 | |||
| 81 | 6 | bool FrolovaSStarTopologyMPI::RunImpl() { | |
| 82 | 6 | int rank = 0; | |
| 83 | 6 | int size = 0; | |
| 84 | 6 | MPI_Comm_rank(MPI_COMM_WORLD, &rank); | |
| 85 | 6 | MPI_Comm_size(MPI_COMM_WORLD, &size); | |
| 86 | |||
| 87 |
2/2✓ Branch 0 taken 3 times.
✓ Branch 1 taken 3 times.
|
6 | if (rank == 0) { |
| 88 | 3 | const int nodes = size - 1; | |
| 89 | 3 | std::vector<int> buf; | |
| 90 | MPI_Status status; | ||
| 91 | |||
| 92 |
2/2✓ Branch 0 taken 3 times.
✓ Branch 1 taken 3 times.
|
6 | for (int i = 0; i < nodes; ++i) { |
| 93 | 3 | int dst = 0; | |
| 94 |
1/2✓ Branch 1 taken 3 times.
✗ Branch 2 not taken.
|
3 | MPI_Recv(&dst, 1, MPI_INT, MPI_ANY_SOURCE, 0, MPI_COMM_WORLD, &status); |
| 95 | 3 | const int src = status.MPI_SOURCE; | |
| 96 | |||
| 97 |
1/2✓ Branch 1 taken 3 times.
✗ Branch 2 not taken.
|
3 | MPI_Probe(src, 0, MPI_COMM_WORLD, &status); |
| 98 | 3 | int buf_size = 0; | |
| 99 |
1/2✓ Branch 1 taken 3 times.
✗ Branch 2 not taken.
|
3 | MPI_Get_count(&status, MPI_INT, &buf_size); |
| 100 |
1/2✓ Branch 1 taken 3 times.
✗ Branch 2 not taken.
|
3 | buf.resize(buf_size); |
| 101 |
1/2✓ Branch 1 taken 3 times.
✗ Branch 2 not taken.
|
3 | MPI_Recv(buf.data(), buf_size, MPI_INT, src, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE); |
| 102 | |||
| 103 |
1/2✓ Branch 1 taken 3 times.
✗ Branch 2 not taken.
|
3 | MPI_Send(&src, 1, MPI_INT, dst, 0, MPI_COMM_WORLD); |
| 104 |
1/2✓ Branch 1 taken 3 times.
✗ Branch 2 not taken.
|
3 | MPI_Send(buf.data(), buf_size, MPI_INT, dst, 0, MPI_COMM_WORLD); |
| 105 | } | ||
| 106 | |||
| 107 |
2/2✓ Branch 0 taken 3 times.
✓ Branch 1 taken 3 times.
|
6 | for (int i = 0; i < nodes; ++i) { |
| 108 |
1/2✓ Branch 1 taken 3 times.
✗ Branch 2 not taken.
|
3 | MPI_Send(&kTerm, 1, MPI_INT, i + 1, 0, MPI_COMM_WORLD); |
| 109 | } | ||
| 110 | |||
| 111 | } else { | ||
| 112 | 3 | MPI_Send(&dest_, 1, MPI_INT, 0, 0, MPI_COMM_WORLD); | |
| 113 |
1/2✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
|
3 | int send_size = data_.empty() ? 0 : static_cast<int>(data_.size()); |
| 114 | 3 | MPI_Send(data_.data(), send_size, MPI_INT, 0, 0, MPI_COMM_WORLD); | |
| 115 | |||
| 116 | while (true) { | ||
| 117 | 6 | int src = 0; | |
| 118 | 6 | MPI_Recv(&src, 1, MPI_INT, 0, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE); | |
| 119 |
2/2✓ Branch 0 taken 3 times.
✓ Branch 1 taken 3 times.
|
6 | if (src == kTerm) { |
| 120 | break; | ||
| 121 | } | ||
| 122 | |||
| 123 | MPI_Status status; | ||
| 124 | 3 | MPI_Probe(0, 0, MPI_COMM_WORLD, &status); | |
| 125 | 3 | int buf_size = 0; | |
| 126 | 3 | MPI_Get_count(&status, MPI_INT, &buf_size); | |
| 127 | 3 | output_.resize(buf_size); | |
| 128 | 3 | MPI_Recv(output_.data(), buf_size, MPI_INT, 0, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE); | |
| 129 | 3 | } | |
| 130 | } | ||
| 131 | |||
| 132 | 6 | return true; | |
| 133 | } | ||
| 134 | |||
| 135 | 6 | bool FrolovaSStarTopologyMPI::PostProcessingImpl() { | |
| 136 | 6 | int rank = 0; | |
| 137 | 6 | MPI_Comm_rank(MPI_COMM_WORLD, &rank); | |
| 138 | |||
| 139 |
2/2✓ Branch 0 taken 3 times.
✓ Branch 1 taken 3 times.
|
6 | if (rank != 0) { |
| 140 | 3 | GetOutput() = static_cast<OutType>(output_.size()); | |
| 141 | } else { | ||
| 142 | 3 | GetOutput() = 0; | |
| 143 | } | ||
| 144 | |||
| 145 | 6 | return true; | |
| 146 | } | ||
| 147 | |||
| 148 | } // namespace frolova_s_star_topology | ||
| 149 |