| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include "krymova_k_lex_order/mpi/include/ops_mpi.hpp" | ||
| 2 | |||
| 3 | #include <mpi.h> | ||
| 4 | |||
| 5 | #include <algorithm> | ||
| 6 | #include <string> | ||
| 7 | #include <vector> | ||
| 8 | |||
| 9 | #include "krymova_k_lex_order/common/include/common.hpp" | ||
| 10 | |||
| 11 | namespace krymova_k_lex_order { | ||
| 12 | |||
| 13 |
1/2✓ Branch 1 taken 20 times.
✗ Branch 2 not taken.
|
20 | KrymovaKLexOrderMPI::KrymovaKLexOrderMPI(const InType &in) { |
| 14 | SetTypeOfTask(GetStaticTypeOfTask()); | ||
| 15 | GetInput() = in; | ||
| 16 | 20 | GetOutput() = 0; | |
| 17 | 20 | } | |
| 18 | |||
| 19 | 20 | bool KrymovaKLexOrderMPI::ValidationImpl() { | |
| 20 | 20 | return true; | |
| 21 | } | ||
| 22 | |||
| 23 | 20 | bool KrymovaKLexOrderMPI::PreProcessingImpl() { | |
| 24 | 20 | return true; | |
| 25 | } | ||
| 26 | |||
| 27 | 20 | bool KrymovaKLexOrderMPI::RunImpl() { | |
| 28 | const std::string &str1 = std::get<0>(GetInput()); | ||
| 29 | const std::string &str2 = std::get<1>(GetInput()); | ||
| 30 | |||
| 31 | 20 | int rank = 0; | |
| 32 | 20 | int size = 1; | |
| 33 | 20 | MPI_Comm_rank(MPI_COMM_WORLD, &rank); | |
| 34 | 20 | MPI_Comm_size(MPI_COMM_WORLD, &size); | |
| 35 | |||
| 36 | 20 | int len1 = 0; | |
| 37 | 20 | int len2 = 0; | |
| 38 |
2/2✓ Branch 0 taken 10 times.
✓ Branch 1 taken 10 times.
|
20 | if (rank == 0) { |
| 39 | 10 | len1 = static_cast<int>(str1.length()); | |
| 40 | 10 | len2 = static_cast<int>(str2.length()); | |
| 41 | } | ||
| 42 | |||
| 43 | 20 | MPI_Bcast(&len1, 1, MPI_INT, 0, MPI_COMM_WORLD); | |
| 44 | 20 | MPI_Bcast(&len2, 1, MPI_INT, 0, MPI_COMM_WORLD); | |
| 45 | |||
| 46 | 20 | int min_len = std::min(len1, len2); | |
| 47 | 20 | int chunk_size = (min_len + size - 1) / size; | |
| 48 | 20 | int total_size = chunk_size * size; | |
| 49 | |||
| 50 | 20 | std::vector<char> sendbuf1(total_size, 0); | |
| 51 |
1/4✓ Branch 1 taken 20 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
|
20 | std::vector<char> sendbuf2(total_size, 0); |
| 52 |
1/4✓ Branch 1 taken 20 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
|
20 | std::vector<char> local_str1(chunk_size, 0); |
| 53 |
1/4✓ Branch 1 taken 20 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
|
20 | std::vector<char> local_str2(chunk_size, 0); |
| 54 | |||
| 55 |
2/2✓ Branch 0 taken 10 times.
✓ Branch 1 taken 10 times.
|
20 | if (rank == 0) { |
| 56 | 10 | std::copy(str1.begin(), str1.begin() + min_len, sendbuf1.begin()); | |
| 57 | 10 | std::copy(str2.begin(), str2.begin() + min_len, sendbuf2.begin()); | |
| 58 | } | ||
| 59 | |||
| 60 |
1/2✓ Branch 1 taken 20 times.
✗ Branch 2 not taken.
|
20 | MPI_Scatter(sendbuf1.data(), chunk_size, MPI_CHAR, local_str1.data(), chunk_size, MPI_CHAR, 0, MPI_COMM_WORLD); |
| 61 |
1/2✓ Branch 1 taken 20 times.
✗ Branch 2 not taken.
|
20 | MPI_Scatter(sendbuf2.data(), chunk_size, MPI_CHAR, local_str2.data(), chunk_size, MPI_CHAR, 0, MPI_COMM_WORLD); |
| 62 | |||
| 63 | 20 | int start = rank * chunk_size; | |
| 64 | 20 | int end = std::min(start + chunk_size, min_len); | |
| 65 | 20 | int actual_size = end - start; | |
| 66 | |||
| 67 | 20 | int local_diff_pos = min_len; | |
| 68 | |||
| 69 |
2/2✓ Branch 0 taken 21 times.
✓ Branch 1 taken 13 times.
|
34 | for (int i = 0; i < actual_size; ++i) { |
| 70 |
2/2✓ Branch 0 taken 7 times.
✓ Branch 1 taken 14 times.
|
21 | if (local_str1[i] != local_str2[i]) { |
| 71 | 7 | local_diff_pos = start + i; | |
| 72 | 7 | break; | |
| 73 | } | ||
| 74 | } | ||
| 75 | |||
| 76 | 20 | int global_first_diff = min_len; | |
| 77 |
1/2✓ Branch 1 taken 20 times.
✗ Branch 2 not taken.
|
20 | MPI_Allreduce(&local_diff_pos, &global_first_diff, 1, MPI_INT, MPI_MIN, MPI_COMM_WORLD); |
| 78 | |||
| 79 | int result = 0; | ||
| 80 | |||
| 81 |
2/2✓ Branch 0 taken 10 times.
✓ Branch 1 taken 10 times.
|
20 | if (global_first_diff < min_len) { |
| 82 | 10 | char char1 = 0; | |
| 83 | 10 | char char2 = 0; | |
| 84 | |||
| 85 |
2/2✓ Branch 0 taken 5 times.
✓ Branch 1 taken 5 times.
|
10 | if (rank == 0) { |
| 86 | 5 | char1 = str1[global_first_diff]; | |
| 87 | 5 | char2 = str2[global_first_diff]; | |
| 88 | } | ||
| 89 | |||
| 90 |
1/2✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
|
10 | MPI_Bcast(&char1, 1, MPI_CHAR, 0, MPI_COMM_WORLD); |
| 91 |
1/2✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
|
10 | MPI_Bcast(&char2, 1, MPI_CHAR, 0, MPI_COMM_WORLD); |
| 92 | |||
| 93 |
2/2✓ Branch 0 taken 4 times.
✓ Branch 1 taken 6 times.
|
10 | result = (char1 < char2) ? -1 : 1; |
| 94 | } else { | ||
| 95 |
2/2✓ Branch 0 taken 8 times.
✓ Branch 1 taken 2 times.
|
10 | if (len1 < len2) { |
| 96 | result = -1; | ||
| 97 |
2/2✓ Branch 0 taken 4 times.
✓ Branch 1 taken 4 times.
|
8 | } else if (len1 > len2) { |
| 98 | result = 1; | ||
| 99 | } else { | ||
| 100 | result = 0; | ||
| 101 | } | ||
| 102 | } | ||
| 103 | |||
| 104 |
1/2✓ Branch 0 taken 20 times.
✗ Branch 1 not taken.
|
20 | GetOutput() = result; |
| 105 | 20 | return true; | |
| 106 | } | ||
| 107 | |||
| 108 | 20 | bool KrymovaKLexOrderMPI::PostProcessingImpl() { | |
| 109 | 20 | return true; | |
| 110 | } | ||
| 111 | |||
| 112 | } // namespace krymova_k_lex_order | ||
| 113 |