| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include "orehov_n_character_frequency/mpi/include/ops_mpi.hpp" | ||
| 2 | |||
| 3 | #include <mpi.h> | ||
| 4 | |||
| 5 | #include <algorithm> | ||
| 6 | #include <cstddef> | ||
| 7 | #include <string> | ||
| 8 | #include <tuple> | ||
| 9 | #include <vector> | ||
| 10 | |||
| 11 | #include "orehov_n_character_frequency/common/include/common.hpp" | ||
| 12 | |||
| 13 | namespace orehov_n_character_frequency { | ||
| 14 | |||
| 15 |
1/2✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
|
2 | OrehovNCharacterFrequencyMPI::OrehovNCharacterFrequencyMPI(const InType &in) { |
| 16 | SetTypeOfTask(GetStaticTypeOfTask()); | ||
| 17 | GetInput() = in; | ||
| 18 | 2 | GetOutput() = 0; | |
| 19 | 2 | } | |
| 20 | |||
| 21 | 2 | bool OrehovNCharacterFrequencyMPI::ValidationImpl() { | |
| 22 | 2 | int rank = 0; | |
| 23 | 2 | int check = 0; | |
| 24 | |||
| 25 | 2 | MPI_Comm_rank(MPI_COMM_WORLD, &rank); | |
| 26 | |||
| 27 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
|
2 | if (rank == 0) { |
| 28 | std::string text = std::get<0>(GetInput()); | ||
| 29 | std::string target_char = std::get<1>(GetInput()); | ||
| 30 | |||
| 31 |
2/4✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
|
1 | check = static_cast<int>((!text.empty()) && (target_char.length() == 1)); |
| 32 | } | ||
| 33 | |||
| 34 | 2 | MPI_Bcast(&check, 1, MPI_INT, 0, MPI_COMM_WORLD); | |
| 35 | 2 | return (check == 1); | |
| 36 | } | ||
| 37 | |||
| 38 | 2 | bool OrehovNCharacterFrequencyMPI::PreProcessingImpl() { | |
| 39 | 2 | return true; | |
| 40 | } | ||
| 41 | |||
| 42 | 2 | bool OrehovNCharacterFrequencyMPI::RunImpl() { | |
| 43 | 2 | int rank = 0; | |
| 44 | 2 | int size = 0; | |
| 45 | |||
| 46 | 2 | MPI_Comm_size(MPI_COMM_WORLD, &size); | |
| 47 | 2 | MPI_Comm_rank(MPI_COMM_WORLD, &rank); | |
| 48 | |||
| 49 | std::string local_str; | ||
| 50 | std::string symbol; | ||
| 51 | 2 | int length = 0; | |
| 52 | 2 | int global_result = 0; | |
| 53 | 2 | int local_result = 0; | |
| 54 | |||
| 55 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
|
2 | if (rank == 0) { |
| 56 | std::string str = std::get<0>(GetInput()); | ||
| 57 | symbol = std::get<1>(GetInput()); | ||
| 58 |
1/2✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
|
1 | length = static_cast<int>(str.length()); |
| 59 |
1/2✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
|
1 | MPI_Bcast(symbol.data(), 1, MPI_CHAR, 0, MPI_COMM_WORLD); |
| 60 |
1/2✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
|
1 | MPI_Bcast(&length, 1, MPI_INT, 0, MPI_COMM_WORLD); |
| 61 | |||
| 62 | 1 | int part_size = length / size; | |
| 63 | 1 | int remains = length % size; | |
| 64 | |||
| 65 |
1/2✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
|
1 | std::vector<int> sendcounts(size); |
| 66 |
1/4✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
|
1 | std::vector<int> displs(size); |
| 67 | |||
| 68 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1 times.
|
3 | for (int i = 0; i < size; i++) { |
| 69 |
1/2✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
|
4 | sendcounts[i] = part_size + (i < remains ? 1 : 0); |
| 70 | 2 | displs[i] = (i * part_size) + std::min(i, remains); | |
| 71 | } | ||
| 72 | |||
| 73 |
1/2✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
|
1 | local_str.resize(sendcounts[0]); |
| 74 | |||
| 75 |
1/2✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
|
1 | MPI_Scatterv(str.data(), sendcounts.data(), displs.data(), MPI_CHAR, local_str.data(), sendcounts[0], MPI_CHAR, 0, |
| 76 | MPI_COMM_WORLD); | ||
| 77 | } else { | ||
| 78 | symbol.resize(1); | ||
| 79 |
1/2✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
|
1 | MPI_Bcast(symbol.data(), 1, MPI_CHAR, 0, MPI_COMM_WORLD); |
| 80 |
1/2✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
|
1 | MPI_Bcast(&length, 1, MPI_INT, 0, MPI_COMM_WORLD); |
| 81 | |||
| 82 | 1 | int part_size = length / size; | |
| 83 | 1 | int remains = length % size; | |
| 84 | |||
| 85 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | int local_size = part_size + (rank < remains ? 1 : 0); |
| 86 |
1/2✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
|
1 | local_str.resize(local_size); |
| 87 | |||
| 88 |
1/2✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
|
1 | MPI_Scatterv(nullptr, nullptr, nullptr, MPI_CHAR, local_str.data(), local_size, MPI_CHAR, 0, MPI_COMM_WORLD); |
| 89 | } | ||
| 90 | |||
| 91 |
2/2✓ Branch 0 taken 36 times.
✓ Branch 1 taken 2 times.
|
38 | for (size_t i = 0; i < local_str.length(); i++) { |
| 92 |
2/2✓ Branch 0 taken 3 times.
✓ Branch 1 taken 33 times.
|
36 | if (local_str[i] == symbol[0]) { |
| 93 | 3 | local_result++; | |
| 94 | } | ||
| 95 | } | ||
| 96 | |||
| 97 |
1/2✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
|
2 | MPI_Allreduce(&local_result, &global_result, 1, MPI_INT, MPI_SUM, MPI_COMM_WORLD); |
| 98 | 2 | GetOutput() = global_result; | |
| 99 | |||
| 100 | 2 | return true; | |
| 101 | } | ||
| 102 | |||
| 103 | 2 | bool OrehovNCharacterFrequencyMPI::PostProcessingImpl() { | |
| 104 | 2 | return true; | |
| 105 | } | ||
| 106 | |||
| 107 | } // namespace orehov_n_character_frequency | ||
| 108 |