| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include "tsibareva_e_matrix_column_max/mpi/include/ops_mpi.hpp" | ||
| 2 | |||
| 3 | #include <mpi.h> | ||
| 4 | |||
| 5 | #include <algorithm> | ||
| 6 | #include <cstddef> | ||
| 7 | #include <vector> | ||
| 8 | |||
| 9 | #include "tsibareva_e_matrix_column_max/common/include/common.hpp" | ||
| 10 | |||
| 11 | namespace tsibareva_e_matrix_column_max { | ||
| 12 | |||
| 13 |
1/2✓ Branch 1 taken 42 times.
✗ Branch 2 not taken.
|
42 | TsibarevaEMatrixColumnMaxMPI::TsibarevaEMatrixColumnMaxMPI(const InType &in) { |
| 14 | SetTypeOfTask(GetStaticTypeOfTask()); | ||
| 15 |
1/4✓ Branch 1 taken 42 times.
✗ Branch 2 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
|
42 | GetInput() = std::vector<std::vector<int>>(in); |
| 16 | 42 | GetOutput() = std::vector<int>(); | |
| 17 | 42 | } | |
| 18 | |||
| 19 | 42 | bool TsibarevaEMatrixColumnMaxMPI::ValidationImpl() { | |
| 20 | 42 | return true; | |
| 21 | } | ||
| 22 | |||
| 23 |
2/2✓ Branch 0 taken 40 times.
✓ Branch 1 taken 2 times.
|
42 | bool TsibarevaEMatrixColumnMaxMPI::PreProcessingImpl() { |
| 24 | const auto &matrix = GetInput(); | ||
| 25 | |||
| 26 |
4/4✓ Branch 0 taken 40 times.
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 38 times.
|
42 | if (matrix.empty() || matrix[0].empty()) { |
| 27 | 4 | GetOutput() = std::vector<int>(); | |
| 28 | 4 | final_result_ = std::vector<int>(); | |
| 29 | 4 | return true; | |
| 30 | } | ||
| 31 | |||
| 32 | size_t first_row_size = matrix[0].size(); | ||
| 33 |
2/2✓ Branch 0 taken 170 times.
✓ Branch 1 taken 36 times.
|
206 | for (size_t i = 1; i < matrix.size(); ++i) { |
| 34 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 168 times.
|
170 | if (matrix[i].size() != first_row_size) { |
| 35 | 2 | GetOutput() = std::vector<int>(); | |
| 36 | 2 | final_result_ = std::vector<int>(); | |
| 37 | 2 | return true; | |
| 38 | } | ||
| 39 | } | ||
| 40 | |||
| 41 | 36 | final_result_ = std::vector<int>(GetInput()[0].size(), 0); | |
| 42 | 36 | GetOutput() = std::vector<int>(GetInput()[0].size(), 0); | |
| 43 | 36 | return true; | |
| 44 | } | ||
| 45 | |||
| 46 |
2/2✓ Branch 0 taken 36 times.
✓ Branch 1 taken 6 times.
|
42 | bool TsibarevaEMatrixColumnMaxMPI::RunImpl() { |
| 47 |
2/2✓ Branch 0 taken 36 times.
✓ Branch 1 taken 6 times.
|
42 | if (GetOutput().empty()) { |
| 48 | return true; | ||
| 49 | } | ||
| 50 | |||
| 51 | 36 | int world_rank = 0; | |
| 52 | 36 | int world_size = 0; | |
| 53 | 36 | MPI_Comm_rank(MPI_COMM_WORLD, &world_rank); | |
| 54 | 36 | MPI_Comm_size(MPI_COMM_WORLD, &world_size); | |
| 55 | |||
| 56 | const auto &matrix = GetInput(); | ||
| 57 | size_t rows_count = matrix.size(); | ||
| 58 | size_t cols_count = matrix[0].size(); | ||
| 59 | |||
| 60 | 36 | std::vector<int> local_maxs; | |
| 61 | |||
| 62 |
2/2✓ Branch 0 taken 102 times.
✓ Branch 1 taken 36 times.
|
138 | for (auto col = static_cast<size_t>(world_rank); col < cols_count; col += static_cast<size_t>(world_size)) { |
| 63 | 102 | int max_val = matrix[0][col]; | |
| 64 |
2/2✓ Branch 0 taken 522 times.
✓ Branch 1 taken 102 times.
|
624 | for (size_t row = 1; row < rows_count; ++row) { |
| 65 | 522 | max_val = std::max(matrix[row][col], max_val); | |
| 66 | } | ||
| 67 | local_maxs.push_back(max_val); | ||
| 68 | } | ||
| 69 | |||
| 70 |
2/2✓ Branch 0 taken 18 times.
✓ Branch 1 taken 18 times.
|
36 | if (world_rank == 0) { |
| 71 |
1/2✓ Branch 1 taken 18 times.
✗ Branch 2 not taken.
|
18 | CollectResultsFromAllProcesses(local_maxs, world_size, cols_count); |
| 72 | } else { | ||
| 73 |
2/2✓ Branch 0 taken 16 times.
✓ Branch 1 taken 2 times.
|
18 | if (!local_maxs.empty()) { |
| 74 |
1/2✓ Branch 1 taken 16 times.
✗ Branch 2 not taken.
|
16 | MPI_Send(local_maxs.data(), static_cast<int>(local_maxs.size()), MPI_INT, 0, 0, MPI_COMM_WORLD); |
| 75 | } | ||
| 76 | } | ||
| 77 | |||
| 78 | return true; | ||
| 79 | } | ||
| 80 | |||
| 81 | 18 | void TsibarevaEMatrixColumnMaxMPI::CollectResultsFromAllProcesses(const std::vector<int> &local_maxs, int world_size, | |
| 82 | size_t cols_count) { | ||
| 83 | 18 | final_result_.resize(cols_count); | |
| 84 | |||
| 85 | 18 | int world_rank = 0; | |
| 86 | 18 | MPI_Comm_rank(MPI_COMM_WORLD, &world_rank); | |
| 87 | |||
| 88 | size_t idx = 0; | ||
| 89 |
3/4✓ Branch 0 taken 18 times.
✓ Branch 1 taken 54 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 54 times.
|
72 | for (size_t col = 0; col < cols_count && idx < local_maxs.size(); col += world_size) { |
| 90 | 54 | final_result_[col] = local_maxs[idx++]; | |
| 91 | } | ||
| 92 | |||
| 93 |
2/2✓ Branch 0 taken 18 times.
✓ Branch 1 taken 18 times.
|
36 | for (int proc = 1; proc < world_size; proc++) { |
| 94 | int proc_pass = 0; | ||
| 95 | |||
| 96 |
2/2✓ Branch 0 taken 48 times.
✓ Branch 1 taken 18 times.
|
66 | for (size_t col = proc; col < cols_count; col += world_size) { |
| 97 | 48 | proc_pass++; | |
| 98 | } | ||
| 99 | |||
| 100 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 16 times.
|
18 | if (proc_pass <= 0) { |
| 101 | 2 | continue; | |
| 102 | } | ||
| 103 | |||
| 104 |
1/2✓ Branch 2 taken 16 times.
✗ Branch 3 not taken.
|
16 | std::vector<int> proc_maxs(static_cast<size_t>(proc_pass)); |
| 105 |
1/2✓ Branch 1 taken 16 times.
✗ Branch 2 not taken.
|
16 | MPI_Recv(proc_maxs.data(), proc_pass, MPI_INT, proc, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE); |
| 106 | |||
| 107 | size_t proc_idx = 0; | ||
| 108 |
2/2✓ Branch 0 taken 48 times.
✓ Branch 1 taken 16 times.
|
64 | for (size_t col = proc; col < cols_count; col += world_size) { |
| 109 | 48 | final_result_[col] = proc_maxs[proc_idx++]; | |
| 110 | } | ||
| 111 | } | ||
| 112 | 18 | } | |
| 113 | |||
| 114 |
2/2✓ Branch 0 taken 36 times.
✓ Branch 1 taken 6 times.
|
42 | bool TsibarevaEMatrixColumnMaxMPI::PostProcessingImpl() { |
| 115 |
2/2✓ Branch 0 taken 36 times.
✓ Branch 1 taken 6 times.
|
42 | if (GetOutput().empty()) { |
| 116 | return true; | ||
| 117 | } | ||
| 118 | |||
| 119 | 36 | int world_rank = 0; | |
| 120 | 36 | int world_size = 0; | |
| 121 | 36 | MPI_Comm_rank(MPI_COMM_WORLD, &world_rank); | |
| 122 | 36 | MPI_Comm_size(MPI_COMM_WORLD, &world_size); | |
| 123 | |||
| 124 | const auto &matrix = GetInput(); | ||
| 125 | size_t cols_count = matrix[0].size(); | ||
| 126 | |||
| 127 |
2/2✓ Branch 0 taken 18 times.
✓ Branch 1 taken 18 times.
|
36 | if (world_rank == 0) { |
| 128 | 18 | GetOutput() = final_result_; | |
| 129 | } else { | ||
| 130 | 18 | GetOutput().resize(cols_count); | |
| 131 | } | ||
| 132 | |||
| 133 | 36 | MPI_Bcast(GetOutput().data(), static_cast<int>(cols_count), MPI_INT, 0, MPI_COMM_WORLD); | |
| 134 | |||
| 135 | return true; | ||
| 136 | } | ||
| 137 | |||
| 138 | } // namespace tsibareva_e_matrix_column_max | ||
| 139 |