| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include "belov_e_lexico_order_two_strings/mpi/include/ops_mpi.hpp" | ||
| 2 | |||
| 3 | #include <mpi.h> | ||
| 4 | |||
| 5 | #include <algorithm> | ||
| 6 | #include <limits> | ||
| 7 | #include <string> | ||
| 8 | #include <vector> | ||
| 9 | |||
| 10 | #include "belov_e_lexico_order_two_strings/common/include/common.hpp" | ||
| 11 | |||
| 12 | namespace belov_e_lexico_order_two_strings { | ||
| 13 |
1/2✓ Branch 1 taken 8 times.
✗ Branch 2 not taken.
|
8 | BelovELexicoOrderTwoStringsMPI::BelovELexicoOrderTwoStringsMPI(const InType &in) { |
| 14 | SetTypeOfTask(GetStaticTypeOfTask()); | ||
| 15 | GetInput() = in; | ||
| 16 | 8 | GetOutput() = false; | |
| 17 | 8 | } | |
| 18 | |||
| 19 |
1/2✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
|
8 | bool BelovELexicoOrderTwoStringsMPI::ValidationImpl() { |
| 20 |
2/4✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 8 times.
|
8 | return !std::get<0>(GetInput()).empty() && !std::get<1>(GetInput()).empty(); |
| 21 | } | ||
| 22 | |||
| 23 | 8 | bool BelovELexicoOrderTwoStringsMPI::PreProcessingImpl() { | |
| 24 | 8 | std::vector<std::string> temp; | |
| 25 | std::string current; | ||
| 26 |
2/2✓ Branch 0 taken 88 times.
✓ Branch 1 taken 8 times.
|
96 | for (auto &ch : std::get<0>(GetInput())) { |
| 27 |
2/2✓ Branch 0 taken 16 times.
✓ Branch 1 taken 72 times.
|
88 | if (ch == ' ') { |
| 28 |
1/2✓ Branch 1 taken 16 times.
✗ Branch 2 not taken.
|
16 | temp.push_back(current); |
| 29 | current = ""; | ||
| 30 | } else { | ||
| 31 | current += ch; | ||
| 32 | } | ||
| 33 | } | ||
| 34 |
1/2✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
|
8 | if (!current.empty()) { |
| 35 |
1/2✓ Branch 1 taken 8 times.
✗ Branch 2 not taken.
|
8 | temp.push_back(current); |
| 36 | } | ||
| 37 |
1/2✓ Branch 1 taken 8 times.
✗ Branch 2 not taken.
|
8 | std::get<0>(GetProccesedInput()) = temp; |
| 38 | |||
| 39 |
1/2✓ Branch 2 taken 8 times.
✗ Branch 3 not taken.
|
8 | std::vector<std::string>().swap(temp); |
| 40 | current = ""; | ||
| 41 |
2/2✓ Branch 0 taken 88 times.
✓ Branch 1 taken 8 times.
|
96 | for (auto &ch : std::get<1>(GetInput())) { |
| 42 |
2/2✓ Branch 0 taken 16 times.
✓ Branch 1 taken 72 times.
|
88 | if (ch == ' ') { |
| 43 |
1/2✓ Branch 1 taken 16 times.
✗ Branch 2 not taken.
|
16 | temp.push_back(current); |
| 44 | current = ""; | ||
| 45 | } else { | ||
| 46 | current += ch; | ||
| 47 | } | ||
| 48 | } | ||
| 49 |
1/2✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
|
8 | if (!current.empty()) { |
| 50 |
1/2✓ Branch 1 taken 8 times.
✗ Branch 2 not taken.
|
8 | temp.push_back(current); |
| 51 | } | ||
| 52 |
1/2✓ Branch 1 taken 8 times.
✗ Branch 2 not taken.
|
8 | std::get<1>(GetProccesedInput()) = temp; |
| 53 | |||
| 54 |
2/4✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 8 times.
|
16 | return !std::get<0>(GetProccesedInput()).empty() && !std::get<1>(GetProccesedInput()).empty(); |
| 55 | 8 | } | |
| 56 | |||
| 57 | 8 | ChunkAns ChunkCheck(const std::vector<std::string> &first, const std::vector<std::string> &second, int begin, int end) { | |
| 58 | ChunkAns ans{.index = -1, .cmp_flag = 0}; | ||
| 59 |
2/2✓ Branch 0 taken 10 times.
✓ Branch 1 taken 6 times.
|
16 | for (int i = begin; i < end; i++) { |
| 60 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 8 times.
|
10 | if (first[i] < second[i]) { |
| 61 | ans = {.index = i, .cmp_flag = -1}; | ||
| 62 | 2 | return ans; | |
| 63 | } | ||
| 64 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 8 times.
|
8 | if (first[i] > second[i]) { |
| 65 | ans = {.index = i, .cmp_flag = 1}; | ||
| 66 | ✗ | return ans; | |
| 67 | } | ||
| 68 | } | ||
| 69 | 6 | return ans; | |
| 70 | } | ||
| 71 | |||
| 72 | ✗ | int CeilDiv(int a, int b) { | |
| 73 | 8 | return (a + b - 1) / b; | |
| 74 | } | ||
| 75 | |||
| 76 | 16 | void BcastVectorOfStrings(std::vector<std::string> &vec, int n, MPI_Comm comm) { | |
| 77 | 16 | int rank = 0; | |
| 78 | 16 | MPI_Comm_rank(comm, &rank); | |
| 79 | |||
| 80 |
2/2✓ Branch 0 taken 8 times.
✓ Branch 1 taken 8 times.
|
16 | if (rank != 0) { |
| 81 | 8 | vec.resize(n); | |
| 82 | } | ||
| 83 | |||
| 84 |
2/2✓ Branch 0 taken 44 times.
✓ Branch 1 taken 16 times.
|
60 | for (int i = 0; i < n; i++) { |
| 85 | 44 | int len = 0; | |
| 86 |
2/2✓ Branch 0 taken 22 times.
✓ Branch 1 taken 22 times.
|
44 | if (rank == 0) { |
| 87 | 22 | len = static_cast<int>(vec[i].size()); | |
| 88 | } | ||
| 89 | |||
| 90 | 44 | MPI_Bcast(&len, 1, MPI_INT, 0, comm); | |
| 91 | |||
| 92 |
2/2✓ Branch 0 taken 22 times.
✓ Branch 1 taken 22 times.
|
44 | if (rank != 0) { |
| 93 | 22 | vec[i].resize(len); | |
| 94 | } | ||
| 95 | |||
| 96 | 44 | MPI_Bcast(vec[i].data(), len, MPI_CHAR, 0, comm); | |
| 97 | } | ||
| 98 | 16 | } | |
| 99 | |||
| 100 | 8 | bool BelovELexicoOrderTwoStringsMPI::RunImpl() { | |
| 101 | 8 | int mpi_size = 0; | |
| 102 | 8 | int rank = 0; | |
| 103 | |||
| 104 | 8 | MPI_Comm_size(MPI_COMM_WORLD, &mpi_size); | |
| 105 | 8 | MPI_Comm_rank(MPI_COMM_WORLD, &rank); | |
| 106 | |||
| 107 | 8 | std::vector<std::string> first; | |
| 108 | 8 | std::vector<std::string> second; | |
| 109 | |||
| 110 | 8 | int n1 = 0; | |
| 111 | 8 | int n2 = 0; | |
| 112 | 8 | int n = 0; | |
| 113 | |||
| 114 |
2/2✓ Branch 0 taken 4 times.
✓ Branch 1 taken 4 times.
|
8 | if (rank == 0) { |
| 115 |
1/2✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
|
4 | first = std::get<0>(GetProccesedInput()); |
| 116 |
1/2✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
|
4 | second = std::get<1>(GetProccesedInput()); |
| 117 | |||
| 118 | 4 | n1 = static_cast<int>(first.size()); | |
| 119 | 4 | n2 = static_cast<int>(second.size()); | |
| 120 | } | ||
| 121 | |||
| 122 |
1/2✓ Branch 1 taken 8 times.
✗ Branch 2 not taken.
|
8 | MPI_Bcast(&n1, 1, MPI_INT, 0, MPI_COMM_WORLD); |
| 123 |
1/2✓ Branch 1 taken 8 times.
✗ Branch 2 not taken.
|
8 | MPI_Bcast(&n2, 1, MPI_INT, 0, MPI_COMM_WORLD); |
| 124 | |||
| 125 | 8 | n = std::min(n1, n2); | |
| 126 | |||
| 127 |
1/2✓ Branch 1 taken 8 times.
✗ Branch 2 not taken.
|
8 | BcastVectorOfStrings(first, n, MPI_COMM_WORLD); |
| 128 |
1/2✓ Branch 1 taken 8 times.
✗ Branch 2 not taken.
|
8 | BcastVectorOfStrings(second, n, MPI_COMM_WORLD); |
| 129 | |||
| 130 | 8 | int chunk = CeilDiv(n, mpi_size); | |
| 131 | 8 | int begin = rank * chunk; | |
| 132 |
1/2✓ Branch 1 taken 8 times.
✗ Branch 2 not taken.
|
8 | int end = std::min(n, begin + chunk); |
| 133 | |||
| 134 | 8 | ChunkAns local_ans = {.index = -1, .cmp_flag = 0}; | |
| 135 | |||
| 136 | 8 | local_ans = ChunkCheck(first, second, begin, end); | |
| 137 | |||
| 138 |
2/4✓ Branch 1 taken 8 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 8 times.
✗ Branch 5 not taken.
|
8 | std::vector<ChunkAns> results(mpi_size); |
| 139 |
1/2✓ Branch 1 taken 8 times.
✗ Branch 2 not taken.
|
8 | MPI_Gather(&local_ans, sizeof(ChunkAns), MPI_BYTE, results.data(), sizeof(ChunkAns), MPI_BYTE, 0, MPI_COMM_WORLD); |
| 140 | |||
| 141 | 8 | bool result = false; | |
| 142 | |||
| 143 |
2/2✓ Branch 0 taken 4 times.
✓ Branch 1 taken 4 times.
|
8 | if (rank == 0) { |
| 144 | int best_index = std::numeric_limits<int>::max(); | ||
| 145 | int cmp_ans = 0; | ||
| 146 | |||
| 147 |
2/2✓ Branch 0 taken 8 times.
✓ Branch 1 taken 4 times.
|
12 | for (auto &p : results) { |
| 148 |
3/4✓ Branch 0 taken 2 times.
✓ Branch 1 taken 6 times.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
|
8 | if (p.index >= 0 && p.index < best_index) { |
| 149 | best_index = p.index; | ||
| 150 | 2 | cmp_ans = p.cmp_flag; | |
| 151 | } | ||
| 152 | } | ||
| 153 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 2 times.
|
4 | if (best_index != std::numeric_limits<int>::max()) { |
| 154 | 2 | result = (cmp_ans < 0); | |
| 155 | } else { | ||
| 156 | 2 | result = (n1 < n2); | |
| 157 | } | ||
| 158 | } | ||
| 159 | |||
| 160 |
1/2✓ Branch 1 taken 8 times.
✗ Branch 2 not taken.
|
8 | MPI_Bcast(&result, 1, MPI_C_BOOL, 0, MPI_COMM_WORLD); |
| 161 | |||
| 162 | 8 | GetOutput() = result; | |
| 163 | |||
| 164 |
1/2✓ Branch 1 taken 8 times.
✗ Branch 2 not taken.
|
8 | MPI_Barrier(MPI_COMM_WORLD); |
| 165 | 8 | return true; | |
| 166 | 8 | } | |
| 167 | |||
| 168 | 8 | bool BelovELexicoOrderTwoStringsMPI::PostProcessingImpl() { | |
| 169 | 8 | return true; | |
| 170 | } | ||
| 171 | } // namespace belov_e_lexico_order_two_strings | ||
| 172 |