| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include "remizov_k_max_in_matrix_string/mpi/include/ops_mpi.hpp" | ||
| 2 | |||
| 3 | #include <mpi.h> | ||
| 4 | |||
| 5 | #include <algorithm> | ||
| 6 | #include <cstddef> | ||
| 7 | #include <limits> | ||
| 8 | #include <vector> | ||
| 9 | |||
| 10 | #include "remizov_k_max_in_matrix_string/common/include/common.hpp" | ||
| 11 | |||
| 12 | namespace remizov_k_max_in_matrix_string { | ||
| 13 | |||
| 14 |
1/2✓ Branch 1 taken 12 times.
✗ Branch 2 not taken.
|
12 | RemizovKMaxInMatrixStringMPI::RemizovKMaxInMatrixStringMPI(const InType &in) { |
| 15 | SetTypeOfTask(GetStaticTypeOfTask()); | ||
| 16 |
1/2✓ Branch 1 taken 12 times.
✗ Branch 2 not taken.
|
12 | InType tmp(in); |
| 17 | GetInput().swap(tmp); | ||
| 18 | 12 | } | |
| 19 | |||
| 20 | 12 | bool RemizovKMaxInMatrixStringMPI::ValidationImpl() { | |
| 21 | 12 | return true; | |
| 22 | } | ||
| 23 | |||
| 24 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 12 times.
|
12 | bool RemizovKMaxInMatrixStringMPI::PreProcessingImpl() { |
| 25 | GetOutput().clear(); | ||
| 26 | 12 | return true; | |
| 27 | } | ||
| 28 | |||
| 29 | 10 | std::vector<int> RemizovKMaxInMatrixStringMPI::FindMaxValues(const int start, const int end) { | |
| 30 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 10 times.
|
10 | if (start < 0) { |
| 31 | ✗ | return {}; | |
| 32 | } | ||
| 33 | |||
| 34 | const std::size_t input_size = GetInput().size(); | ||
| 35 | 10 | const int input_size_int = static_cast<int>(input_size); | |
| 36 | |||
| 37 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 10 times.
|
10 | if (start >= input_size_int) { |
| 38 | ✗ | return {}; | |
| 39 | } | ||
| 40 | |||
| 41 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 10 times.
|
10 | const int actual_end = (end >= input_size_int) ? input_size_int - 1 : end; |
| 42 | 10 | const int num_rows = actual_end - start + 1; | |
| 43 | |||
| 44 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 10 times.
|
10 | if (num_rows <= 0) { |
| 45 | ✗ | return {}; | |
| 46 | } | ||
| 47 | |||
| 48 | 10 | std::vector<int> result; | |
| 49 |
1/2✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
|
10 | result.reserve(static_cast<std::size_t>(num_rows)); |
| 50 | |||
| 51 |
2/2✓ Branch 0 taken 11 times.
✓ Branch 1 taken 10 times.
|
21 | for (int i = start; i <= actual_end; ++i) { |
| 52 |
1/2✓ Branch 0 taken 11 times.
✗ Branch 1 not taken.
|
11 | if (!GetInput()[i].empty()) { |
| 53 | 11 | int max_val = std::numeric_limits<int>::min(); | |
| 54 |
2/2✓ Branch 0 taken 33 times.
✓ Branch 1 taken 11 times.
|
44 | for (const auto &val : GetInput()[i]) { |
| 55 | 33 | max_val = std::max(val, max_val); | |
| 56 | } | ||
| 57 | result.push_back(max_val); | ||
| 58 | } else { | ||
| 59 | ✗ | result.push_back(std::numeric_limits<int>::min()); | |
| 60 | } | ||
| 61 | } | ||
| 62 | |||
| 63 | return result; | ||
| 64 | } | ||
| 65 | |||
| 66 | 12 | std::vector<int> RemizovKMaxInMatrixStringMPI::CalculatingInterval(int size_prcs, int rank, int count_rows) { | |
| 67 |
2/4✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 12 times.
|
12 | if (count_rows <= 0 || size_prcs <= 0 || rank < 0 || rank >= size_prcs) { |
| 68 | ✗ | return {-1, -1}; | |
| 69 | } | ||
| 70 | |||
| 71 | 12 | const int base_rows = count_rows / size_prcs; | |
| 72 | 12 | const int extra_rows = count_rows % size_prcs; | |
| 73 | |||
| 74 | int start = 0; | ||
| 75 | int end = 0; | ||
| 76 | |||
| 77 |
2/2✓ Branch 0 taken 3 times.
✓ Branch 1 taken 9 times.
|
12 | if (rank < extra_rows) { |
| 78 | 3 | start = rank * (base_rows + 1); | |
| 79 | 3 | end = start + base_rows; | |
| 80 | } else { | ||
| 81 | 9 | start = (extra_rows * (base_rows + 1)) + ((rank - extra_rows) * base_rows); | |
| 82 | 9 | end = start + base_rows - 1; | |
| 83 | } | ||
| 84 | |||
| 85 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 10 times.
|
12 | if (start >= count_rows) { |
| 86 | 2 | return {-1, -1}; | |
| 87 | } | ||
| 88 | |||
| 89 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 10 times.
|
10 | if (end >= count_rows) { |
| 90 | ✗ | end = count_rows - 1; | |
| 91 | } | ||
| 92 | |||
| 93 | 10 | return {start, end}; | |
| 94 | } | ||
| 95 | |||
| 96 |
1/2✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
|
12 | bool RemizovKMaxInMatrixStringMPI::RunImpl() { |
| 97 |
1/2✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
|
12 | if (GetInput().empty()) { |
| 98 | return true; | ||
| 99 | } | ||
| 100 | |||
| 101 | 12 | int size = 0; | |
| 102 | 12 | int rank = 0; | |
| 103 | 12 | MPI_Comm_size(MPI_COMM_WORLD, &size); | |
| 104 | 12 | MPI_Comm_rank(MPI_COMM_WORLD, &rank); | |
| 105 | |||
| 106 | 12 | const int count_rows = static_cast<int>(GetInput().size()); | |
| 107 | |||
| 108 | 12 | std::vector<int> local_interval(2); | |
| 109 | |||
| 110 |
2/2✓ Branch 0 taken 6 times.
✓ Branch 1 taken 6 times.
|
12 | if (rank == 0) { |
| 111 |
1/4✓ Branch 1 taken 6 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
|
6 | std::vector<int> all_intervals(static_cast<std::size_t>(size) * 2); |
| 112 |
2/2✓ Branch 0 taken 12 times.
✓ Branch 1 taken 6 times.
|
18 | for (int i = 0; i < size; ++i) { |
| 113 |
1/2✓ Branch 1 taken 12 times.
✗ Branch 2 not taken.
|
12 | std::vector<int> interval = CalculatingInterval(size, i, count_rows); |
| 114 | 12 | const std::size_t idx1 = static_cast<std::size_t>(2) * static_cast<std::size_t>(i); | |
| 115 | const std::size_t idx2 = idx1 + 1; | ||
| 116 | 12 | all_intervals[idx1] = interval[0]; | |
| 117 | 12 | all_intervals[idx2] = interval[1]; | |
| 118 | } | ||
| 119 | |||
| 120 |
1/2✓ Branch 1 taken 6 times.
✗ Branch 2 not taken.
|
6 | MPI_Scatter(all_intervals.data(), 2, MPI_INT, local_interval.data(), 2, MPI_INT, 0, MPI_COMM_WORLD); |
| 121 | } else { | ||
| 122 |
1/2✓ Branch 1 taken 6 times.
✗ Branch 2 not taken.
|
6 | MPI_Scatter(nullptr, 2, MPI_INT, local_interval.data(), 2, MPI_INT, 0, MPI_COMM_WORLD); |
| 123 | } | ||
| 124 | |||
| 125 |
2/2✓ Branch 0 taken 10 times.
✓ Branch 1 taken 2 times.
|
12 | std::vector<int> local_max_values; |
| 126 |
3/4✓ Branch 0 taken 10 times.
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 10 times.
✗ Branch 3 not taken.
|
12 | if (local_interval[0] != -1 && local_interval[1] != -1) { |
| 127 |
1/2✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
|
20 | local_max_values = FindMaxValues(local_interval[0], local_interval[1]); |
| 128 | } | ||
| 129 | |||
| 130 | 12 | const int local_size = static_cast<int>(local_max_values.size()); | |
| 131 | |||
| 132 |
2/6✓ Branch 1 taken 12 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 12 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
|
12 | std::vector<int> all_sizes(static_cast<std::size_t>(size)); |
| 133 |
1/2✓ Branch 1 taken 12 times.
✗ Branch 2 not taken.
|
12 | MPI_Gather(&local_size, 1, MPI_INT, all_sizes.data(), 1, MPI_INT, 0, MPI_COMM_WORLD); |
| 134 | |||
| 135 |
2/2✓ Branch 0 taken 6 times.
✓ Branch 1 taken 6 times.
|
12 | if (rank == 0) { |
| 136 |
1/4✓ Branch 1 taken 6 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
|
6 | std::vector<int> displacements(static_cast<std::size_t>(size), 0); |
| 137 | int total_size = 0; | ||
| 138 | |||
| 139 |
2/2✓ Branch 0 taken 12 times.
✓ Branch 1 taken 6 times.
|
18 | for (int i = 0; i < size; ++i) { |
| 140 | 12 | displacements[static_cast<std::size_t>(i)] = total_size; | |
| 141 | 12 | total_size += all_sizes[static_cast<std::size_t>(i)]; | |
| 142 | } | ||
| 143 | |||
| 144 |
1/2✓ Branch 1 taken 6 times.
✗ Branch 2 not taken.
|
6 | GetOutput().resize(static_cast<std::size_t>(total_size)); |
| 145 | |||
| 146 |
1/2✓ Branch 1 taken 6 times.
✗ Branch 2 not taken.
|
6 | MPI_Gatherv(local_max_values.data(), local_size, MPI_INT, GetOutput().data(), all_sizes.data(), |
| 147 | displacements.data(), MPI_INT, 0, MPI_COMM_WORLD); | ||
| 148 | } else { | ||
| 149 |
1/2✓ Branch 1 taken 6 times.
✗ Branch 2 not taken.
|
6 | MPI_Gatherv(local_max_values.data(), local_size, MPI_INT, nullptr, nullptr, nullptr, MPI_INT, 0, MPI_COMM_WORLD); |
| 150 | } | ||
| 151 | |||
| 152 |
1/2✓ Branch 1 taken 12 times.
✗ Branch 2 not taken.
|
12 | BroadcastResults(rank); |
| 153 | |||
| 154 | return true; | ||
| 155 | } | ||
| 156 | |||
| 157 | 12 | void RemizovKMaxInMatrixStringMPI::BroadcastResults(int rank) { | |
| 158 | 12 | int total_size = 0; | |
| 159 | |||
| 160 |
2/2✓ Branch 0 taken 6 times.
✓ Branch 1 taken 6 times.
|
12 | if (rank == 0) { |
| 161 | 6 | total_size = static_cast<int>(GetOutput().size()); | |
| 162 | } | ||
| 163 | |||
| 164 | 12 | MPI_Bcast(&total_size, 1, MPI_INT, 0, MPI_COMM_WORLD); | |
| 165 | |||
| 166 |
2/2✓ Branch 0 taken 6 times.
✓ Branch 1 taken 6 times.
|
12 | if (rank != 0) { |
| 167 | 6 | GetOutput().resize(static_cast<std::size_t>(total_size)); | |
| 168 | } | ||
| 169 | |||
| 170 |
1/2✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
|
12 | if (total_size > 0) { |
| 171 | 12 | MPI_Bcast(GetOutput().data(), total_size, MPI_INT, 0, MPI_COMM_WORLD); | |
| 172 | } | ||
| 173 | 12 | } | |
| 174 | |||
| 175 | 12 | bool RemizovKMaxInMatrixStringMPI::PostProcessingImpl() { | |
| 176 | 12 | return true; | |
| 177 | } | ||
| 178 | |||
| 179 | } // namespace remizov_k_max_in_matrix_string | ||
| 180 |