| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include "gutyansky_a_matrix_column_sum/mpi/include/ops_mpi.hpp" | ||
| 2 | |||
| 3 | #include <mpi.h> | ||
| 4 | |||
| 5 | #include <cstddef> | ||
| 6 | #include <cstdint> | ||
| 7 | #include <utility> | ||
| 8 | #include <vector> | ||
| 9 | |||
| 10 | #include "gutyansky_a_matrix_column_sum/common/include/common.hpp" | ||
| 11 | |||
| 12 | namespace gutyansky_a_matrix_column_sum { | ||
| 13 | |||
| 14 |
1/2✓ Branch 1 taken 14 times.
✗ Branch 2 not taken.
|
14 | GutyanskyAMatrixColumnSumMPI::GutyanskyAMatrixColumnSumMPI(const InType &in) { |
| 15 | SetTypeOfTask(GetStaticTypeOfTask()); | ||
| 16 | |||
| 17 | GetInput() = in; | ||
| 18 | GetOutput() = {}; | ||
| 19 | 14 | } | |
| 20 | |||
| 21 | 14 | bool GutyanskyAMatrixColumnSumMPI::ValidationImpl() { | |
| 22 | 14 | int rank = -1; | |
| 23 | 14 | MPI_Comm_rank(MPI_COMM_WORLD, &rank); | |
| 24 | |||
| 25 |
2/2✓ Branch 0 taken 7 times.
✓ Branch 1 taken 7 times.
|
14 | if (rank == 0) { |
| 26 | return GetInput().IsValid(); | ||
| 27 | } | ||
| 28 | |||
| 29 | return true; | ||
| 30 | } | ||
| 31 | |||
| 32 | 14 | bool GutyanskyAMatrixColumnSumMPI::PreProcessingImpl() { | |
| 33 | 14 | return true; | |
| 34 | } | ||
| 35 | |||
| 36 | 14 | bool GutyanskyAMatrixColumnSumMPI::RunImpl() { | |
| 37 | 14 | int rank = -1; | |
| 38 | 14 | int p_count = -1; | |
| 39 | |||
| 40 | 14 | MPI_Comm_rank(MPI_COMM_WORLD, &rank); | |
| 41 | 14 | MPI_Comm_size(MPI_COMM_WORLD, &p_count); | |
| 42 | |||
| 43 | 14 | size_t row_count = GetInput().rows; | |
| 44 | 14 | size_t col_count = GetInput().cols; | |
| 45 | |||
| 46 | 14 | MPI_Bcast(&row_count, 1, MPI_UINT64_T, 0, MPI_COMM_WORLD); | |
| 47 | 14 | MPI_Bcast(&col_count, 1, MPI_UINT64_T, 0, MPI_COMM_WORLD); | |
| 48 | |||
| 49 | 14 | size_t chunk_size = row_count / static_cast<size_t>(p_count); | |
| 50 | 14 | size_t remainder_size = row_count % static_cast<size_t>(p_count); | |
| 51 | |||
| 52 |
1/2✓ Branch 0 taken 14 times.
✗ Branch 1 not taken.
|
14 | size_t rows_count = chunk_size + (std::cmp_less(rank, remainder_size) ? 1 : 0); |
| 53 | 14 | size_t elements_count = col_count * rows_count; | |
| 54 | |||
| 55 | 14 | std::vector<int32_t> input_data_chunk(elements_count); | |
| 56 |
1/4✓ Branch 1 taken 14 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
|
14 | std::vector<int32_t> partial_res(col_count, static_cast<int32_t>(0)); |
| 57 | |||
| 58 |
2/2✓ Branch 0 taken 7 times.
✓ Branch 1 taken 7 times.
|
14 | if (rank == 0) { |
| 59 |
1/4✓ Branch 1 taken 7 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
|
7 | std::vector<int> send_counts(p_count); |
| 60 |
1/4✓ Branch 1 taken 7 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
|
7 | std::vector<int> locs(p_count); |
| 61 | |||
| 62 |
2/2✓ Branch 0 taken 14 times.
✓ Branch 1 taken 7 times.
|
21 | for (int i = 0; i < p_count; i++) { |
| 63 | 14 | size_t rows_for_proc = chunk_size + (std::cmp_less(i, remainder_size) ? 1 : 0); | |
| 64 | 14 | send_counts[i] = static_cast<int>(rows_for_proc * col_count); | |
| 65 | } | ||
| 66 | |||
| 67 |
2/2✓ Branch 0 taken 7 times.
✓ Branch 1 taken 7 times.
|
14 | for (int i = 1; i < p_count; i++) { |
| 68 | 7 | locs[i] = locs[i - 1] + send_counts[i - 1]; | |
| 69 | } | ||
| 70 | |||
| 71 |
1/2✓ Branch 1 taken 7 times.
✗ Branch 2 not taken.
|
7 | MPI_Scatterv(GetInput().data.data(), send_counts.data(), locs.data(), MPI_INTEGER4, input_data_chunk.data(), |
| 72 | static_cast<int>(elements_count), MPI_INTEGER4, 0, MPI_COMM_WORLD); | ||
| 73 | } else { | ||
| 74 |
1/2✓ Branch 1 taken 7 times.
✗ Branch 2 not taken.
|
7 | MPI_Scatterv(nullptr, nullptr, nullptr, MPI_INTEGER4, input_data_chunk.data(), static_cast<int>(elements_count), |
| 75 | MPI_INTEGER4, 0, MPI_COMM_WORLD); | ||
| 76 | } | ||
| 77 | |||
| 78 |
2/2✓ Branch 0 taken 1047 times.
✓ Branch 1 taken 14 times.
|
1061 | for (size_t i = 0; i < rows_count; i++) { |
| 79 |
2/2✓ Branch 0 taken 1000525 times.
✓ Branch 1 taken 1047 times.
|
1001572 | for (size_t j = 0; j < col_count; j++) { |
| 80 | 1000525 | partial_res[j] += input_data_chunk[(i * col_count) + j]; | |
| 81 | } | ||
| 82 | } | ||
| 83 | |||
| 84 |
2/2✓ Branch 0 taken 7 times.
✓ Branch 1 taken 7 times.
|
14 | if (rank == 0) { |
| 85 |
1/2✓ Branch 1 taken 7 times.
✗ Branch 2 not taken.
|
7 | GetOutput().resize(col_count); |
| 86 | } | ||
| 87 | |||
| 88 |
1/2✓ Branch 1 taken 14 times.
✗ Branch 2 not taken.
|
14 | MPI_Reduce(partial_res.data(), GetOutput().data(), static_cast<int>(col_count), MPI_INTEGER4, MPI_SUM, 0, |
| 89 | MPI_COMM_WORLD); | ||
| 90 | |||
| 91 | 14 | return true; | |
| 92 | } | ||
| 93 | |||
| 94 | 14 | bool GutyanskyAMatrixColumnSumMPI::PostProcessingImpl() { | |
| 95 | 14 | return true; | |
| 96 | } | ||
| 97 | |||
| 98 | } // namespace gutyansky_a_matrix_column_sum | ||
| 99 |