| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include "rozenberg_a_matrix_column_sum/mpi/include/ops_mpi.hpp" | ||
| 2 | |||
| 3 | #include <mpi.h> | ||
| 4 | |||
| 5 | #include <cstddef> | ||
| 6 | #include <vector> | ||
| 7 | |||
| 8 | #include "rozenberg_a_matrix_column_sum/common/include/common.hpp" | ||
| 9 | |||
| 10 | namespace rozenberg_a_matrix_column_sum { | ||
| 11 | |||
| 12 | 16 | RozenbergAMatrixColumnSumMPI::RozenbergAMatrixColumnSumMPI(const InType &in) { | |
| 13 | SetTypeOfTask(GetStaticTypeOfTask()); | ||
| 14 | |||
| 15 | 16 | InType empty; | |
| 16 | GetInput().swap(empty); | ||
| 17 | |||
| 18 |
2/2✓ Branch 0 taken 127 times.
✓ Branch 1 taken 16 times.
|
143 | for (const auto &row : in) { |
| 19 |
1/2✓ Branch 1 taken 127 times.
✗ Branch 2 not taken.
|
127 | GetInput().push_back(row); |
| 20 | } | ||
| 21 | |||
| 22 | GetOutput().clear(); | ||
| 23 | 16 | } | |
| 24 | |||
| 25 | 16 | bool RozenbergAMatrixColumnSumMPI::ValidationImpl() { | |
| 26 | 16 | int rank = 0; | |
| 27 | 16 | MPI_Comm_rank(MPI_COMM_WORLD, &rank); | |
| 28 |
2/2✓ Branch 0 taken 8 times.
✓ Branch 1 taken 8 times.
|
16 | if (rank == 0) { |
| 29 |
2/4✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 8 times.
✗ Branch 3 not taken.
|
8 | return (!(GetInput().empty())) && (GetOutput().empty()); |
| 30 | } | ||
| 31 | return true; | ||
| 32 | } | ||
| 33 | |||
| 34 | 16 | bool RozenbergAMatrixColumnSumMPI::PreProcessingImpl() { | |
| 35 | 16 | int rank = 0; | |
| 36 | 16 | MPI_Comm_rank(MPI_COMM_WORLD, &rank); | |
| 37 |
2/2✓ Branch 0 taken 8 times.
✓ Branch 1 taken 8 times.
|
16 | if (rank == 0) { |
| 38 | size_t rows = GetInput().size(); | ||
| 39 | size_t columns = GetInput()[0].size(); | ||
| 40 | 8 | flat_.resize(rows * columns); | |
| 41 |
2/2✓ Branch 0 taken 127 times.
✓ Branch 1 taken 8 times.
|
135 | for (size_t i = 0; i < rows; i++) { |
| 42 |
2/2✓ Branch 0 taken 3661 times.
✓ Branch 1 taken 127 times.
|
3788 | for (size_t j = 0; j < columns; j++) { |
| 43 | 3661 | flat_[j + (i * columns)] = GetInput()[i][j]; | |
| 44 | } | ||
| 45 | } | ||
| 46 | 8 | GetOutput().resize(GetInput()[0].size()); | |
| 47 | } | ||
| 48 | 16 | return true; | |
| 49 | } | ||
| 50 | |||
| 51 | 16 | bool RozenbergAMatrixColumnSumMPI::RunImpl() { | |
| 52 | 16 | int rank = 0; | |
| 53 | 16 | int size = 0; | |
| 54 | 16 | MPI_Comm_rank(MPI_COMM_WORLD, &rank); | |
| 55 | 16 | MPI_Comm_size(MPI_COMM_WORLD, &size); | |
| 56 | |||
| 57 | 16 | int rows = 0; | |
| 58 | 16 | int columns = 0; | |
| 59 |
2/2✓ Branch 0 taken 8 times.
✓ Branch 1 taken 8 times.
|
16 | if (rank == 0) { |
| 60 | 8 | rows = static_cast<int>(GetInput().size()); | |
| 61 | 8 | columns = static_cast<int>(GetInput()[0].size()); | |
| 62 | } | ||
| 63 | |||
| 64 | 16 | MPI_Bcast(&rows, 1, MPI_INT, 0, MPI_COMM_WORLD); | |
| 65 | 16 | MPI_Bcast(&columns, 1, MPI_INT, 0, MPI_COMM_WORLD); | |
| 66 | |||
| 67 | 16 | int chunk = rows / size; | |
| 68 | 16 | int remainder = rows % size; | |
| 69 | |||
| 70 |
2/2✓ Branch 0 taken 11 times.
✓ Branch 1 taken 5 times.
|
16 | int rows_count = chunk + (rank < remainder ? 1 : 0); |
| 71 | |||
| 72 | 16 | std::vector<int> sendcounts; | |
| 73 | 16 | std::vector<int> displs; | |
| 74 | |||
| 75 |
2/2✓ Branch 0 taken 8 times.
✓ Branch 1 taken 8 times.
|
16 | if (rank == 0) { |
| 76 |
1/2✓ Branch 1 taken 8 times.
✗ Branch 2 not taken.
|
8 | sendcounts.resize(size); |
| 77 |
1/2✓ Branch 1 taken 8 times.
✗ Branch 2 not taken.
|
8 | displs.resize(size); |
| 78 | |||
| 79 | int offset = 0; | ||
| 80 |
2/2✓ Branch 0 taken 16 times.
✓ Branch 1 taken 8 times.
|
24 | for (int i = 0; i < size; i++) { |
| 81 |
2/2✓ Branch 0 taken 11 times.
✓ Branch 1 taken 5 times.
|
16 | int r = chunk + (i < remainder ? 1 : 0); |
| 82 | 16 | sendcounts[i] = r * columns; | |
| 83 | 16 | displs[i] = offset; | |
| 84 | 16 | offset += r * columns; | |
| 85 | } | ||
| 86 | } | ||
| 87 | |||
| 88 |
1/4✓ Branch 1 taken 16 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
|
16 | std::vector<int> local_buf(static_cast<size_t>(rows_count * columns)); |
| 89 | |||
| 90 |
2/2✓ Branch 0 taken 8 times.
✓ Branch 1 taken 8 times.
|
16 | if (rank == 0) { |
| 91 |
1/2✓ Branch 1 taken 8 times.
✗ Branch 2 not taken.
|
8 | MPI_Scatterv(flat_.data(), sendcounts.data(), displs.data(), MPI_INT, local_buf.data(), rows_count * columns, |
| 92 | MPI_INT, 0, MPI_COMM_WORLD); | ||
| 93 | } else { | ||
| 94 |
1/2✓ Branch 1 taken 8 times.
✗ Branch 2 not taken.
|
8 | MPI_Scatterv(nullptr, nullptr, nullptr, MPI_INT, local_buf.data(), rows_count * columns, MPI_INT, 0, |
| 95 | MPI_COMM_WORLD); | ||
| 96 | } | ||
| 97 | |||
| 98 |
1/4✓ Branch 1 taken 16 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
|
16 | OutType local_res(columns, 0); |
| 99 |
2/2✓ Branch 0 taken 127 times.
✓ Branch 1 taken 16 times.
|
143 | for (int i = 0; i < rows_count; i++) { |
| 100 |
2/2✓ Branch 0 taken 3661 times.
✓ Branch 1 taken 127 times.
|
3788 | for (int j = 0; j < columns; j++) { |
| 101 | 3661 | local_res[j] += local_buf[j + (i * columns)]; | |
| 102 | } | ||
| 103 | } | ||
| 104 | |||
| 105 |
3/4✓ Branch 0 taken 8 times.
✓ Branch 1 taken 8 times.
✓ Branch 3 taken 16 times.
✗ Branch 4 not taken.
|
24 | MPI_Reduce(local_res.data(), rank == 0 ? GetOutput().data() : nullptr, columns, MPI_INT, MPI_SUM, 0, MPI_COMM_WORLD); |
| 106 | |||
| 107 | 16 | return true; | |
| 108 | } | ||
| 109 | |||
| 110 | 16 | bool RozenbergAMatrixColumnSumMPI::PostProcessingImpl() { | |
| 111 | 16 | return true; | |
| 112 | } | ||
| 113 | |||
| 114 | } // namespace rozenberg_a_matrix_column_sum | ||
| 115 |