| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include "zaharov_g_matrix_col_sum/mpi/include/ops_mpi.hpp" | ||
| 2 | |||
| 3 | #include <mpi.h> | ||
| 4 | |||
| 5 | #include <algorithm> | ||
| 6 | #include <cstddef> | ||
| 7 | #include <utility> | ||
| 8 | #include <vector> | ||
| 9 | |||
| 10 | #include "zaharov_g_matrix_col_sum/common/include/common.hpp" | ||
| 11 | |||
| 12 | namespace zaharov_g_matrix_col_sum { | ||
| 13 | |||
| 14 |
1/2✓ Branch 1 taken 14 times.
✗ Branch 2 not taken.
|
14 | ZaharovGMatrixColSumMPI::ZaharovGMatrixColSumMPI(const InType &in) { |
| 15 | SetTypeOfTask(GetStaticTypeOfTask()); | ||
| 16 |
1/2✓ Branch 1 taken 14 times.
✗ Branch 2 not taken.
|
14 | InType tmp(in); |
| 17 | GetInput().swap(tmp); | ||
| 18 | 14 | } | |
| 19 | |||
| 20 |
1/2✓ Branch 0 taken 14 times.
✗ Branch 1 not taken.
|
14 | bool ZaharovGMatrixColSumMPI::ValidationImpl() { |
| 21 |
1/2✓ Branch 0 taken 14 times.
✗ Branch 1 not taken.
|
14 | if (GetInput().empty()) { |
| 22 | return true; | ||
| 23 | } | ||
| 24 | |||
| 25 | size_t cols = GetInput()[0].size(); | ||
| 26 | 14 | return std::all_of(GetInput().begin(), GetInput().end(), [cols](const auto &row) { return row.size() == cols; }); | |
| 27 | } | ||
| 28 | |||
| 29 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 14 times.
|
14 | bool ZaharovGMatrixColSumMPI::PreProcessingImpl() { |
| 30 | GetOutput().clear(); | ||
| 31 | 14 | return true; | |
| 32 | } | ||
| 33 | |||
| 34 |
1/2✓ Branch 0 taken 14 times.
✗ Branch 1 not taken.
|
14 | bool ZaharovGMatrixColSumMPI::RunImpl() { |
| 35 | const InType &in = GetInput(); | ||
| 36 |
1/2✓ Branch 0 taken 14 times.
✗ Branch 1 not taken.
|
14 | if (in.empty()) { |
| 37 | return false; | ||
| 38 | } | ||
| 39 | |||
| 40 | 14 | int size = 0; | |
| 41 | 14 | int rank = 0; | |
| 42 | 14 | MPI_Comm_size(MPI_COMM_WORLD, &size); | |
| 43 | 14 | MPI_Comm_rank(MPI_COMM_WORLD, &rank); | |
| 44 | |||
| 45 | 14 | const int column_amount = static_cast<int>(in[0].size()); | |
| 46 | |||
| 47 |
2/2✓ Branch 0 taken 7 times.
✓ Branch 1 taken 7 times.
|
14 | if (rank == 0) { |
| 48 |
2/2✓ Branch 0 taken 7 times.
✓ Branch 1 taken 7 times.
|
14 | for (int i = 1; i < size; i++) { |
| 49 | 7 | std::vector<int> interval = CalcInterval(size, i, column_amount); | |
| 50 |
1/2✓ Branch 1 taken 7 times.
✗ Branch 2 not taken.
|
7 | MPI_Send(interval.data(), 2, MPI_INT, i, 0, MPI_COMM_WORLD); |
| 51 | } | ||
| 52 | |||
| 53 | 7 | std::vector<int> interval = CalcInterval(size, 0, column_amount); | |
| 54 |
1/2✓ Branch 1 taken 7 times.
✗ Branch 2 not taken.
|
7 | OutType elems = SumColValues(interval[0], interval[1]); |
| 55 | |||
| 56 |
1/2✓ Branch 1 taken 7 times.
✗ Branch 2 not taken.
|
7 | GetOutput().resize(column_amount); |
| 57 |
2/2✓ Branch 0 taken 15 times.
✓ Branch 1 taken 7 times.
|
22 | for (size_t i = 0; i < elems.size(); i++) { |
| 58 | 15 | GetOutput()[interval[0] + i] = elems[i]; | |
| 59 | } | ||
| 60 | |||
| 61 | MPI_Status status; | ||
| 62 |
2/2✓ Branch 0 taken 7 times.
✓ Branch 1 taken 7 times.
|
14 | for (int i = 1; i < size; i++) { |
| 63 | 7 | int elems_size = 0; | |
| 64 |
1/2✓ Branch 1 taken 7 times.
✗ Branch 2 not taken.
|
7 | MPI_Recv(&elems_size, 1, MPI_INT, i, 1, MPI_COMM_WORLD, &status); |
| 65 | |||
| 66 |
2/6✓ Branch 1 taken 7 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 7 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
|
7 | std::vector<int> remote_interval(2); |
| 67 |
1/2✓ Branch 1 taken 7 times.
✗ Branch 2 not taken.
|
7 | MPI_Recv(remote_interval.data(), 2, MPI_INT, i, 3, MPI_COMM_WORLD, &status); |
| 68 | |||
| 69 |
1/4✓ Branch 1 taken 7 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
|
7 | OutType buf(elems_size); |
| 70 |
1/2✓ Branch 1 taken 7 times.
✗ Branch 2 not taken.
|
7 | MPI_Recv(buf.data(), elems_size, MPI_DOUBLE, i, 2, MPI_COMM_WORLD, &status); |
| 71 | |||
| 72 |
2/2✓ Branch 0 taken 11 times.
✓ Branch 1 taken 7 times.
|
18 | for (size_t j = 0; j < buf.size(); j++) { |
| 73 | 11 | GetOutput()[remote_interval[0] + j] = buf[j]; | |
| 74 | } | ||
| 75 | } | ||
| 76 | } else { | ||
| 77 | MPI_Status status; | ||
| 78 | |||
| 79 |
1/2✓ Branch 2 taken 7 times.
✗ Branch 3 not taken.
|
7 | std::vector<int> buf(2); |
| 80 |
1/2✓ Branch 1 taken 7 times.
✗ Branch 2 not taken.
|
7 | MPI_Recv(buf.data(), 2, MPI_INT, 0, 0, MPI_COMM_WORLD, &status); |
| 81 | |||
| 82 |
1/2✓ Branch 1 taken 7 times.
✗ Branch 2 not taken.
|
7 | OutType elems = SumColValues(buf[0], buf[1]); |
| 83 | 7 | int elems_size = static_cast<int>(elems.size()); | |
| 84 | |||
| 85 |
1/2✓ Branch 1 taken 7 times.
✗ Branch 2 not taken.
|
7 | MPI_Send(buf.data(), 2, MPI_INT, 0, 3, MPI_COMM_WORLD); |
| 86 |
1/2✓ Branch 1 taken 7 times.
✗ Branch 2 not taken.
|
7 | MPI_Send(&elems_size, 1, MPI_INT, 0, 1, MPI_COMM_WORLD); |
| 87 |
1/2✓ Branch 1 taken 7 times.
✗ Branch 2 not taken.
|
7 | MPI_Send(elems.data(), elems_size, MPI_DOUBLE, 0, 2, MPI_COMM_WORLD); |
| 88 | } | ||
| 89 | |||
| 90 | 14 | int total_size = 0; | |
| 91 |
2/2✓ Branch 0 taken 7 times.
✓ Branch 1 taken 7 times.
|
14 | if (rank == 0) { |
| 92 | 7 | total_size = static_cast<int>(GetOutput().size()); | |
| 93 | } | ||
| 94 | |||
| 95 | 14 | MPI_Bcast(&total_size, 1, MPI_INT, 0, MPI_COMM_WORLD); | |
| 96 | |||
| 97 |
2/2✓ Branch 0 taken 7 times.
✓ Branch 1 taken 7 times.
|
14 | if (rank != 0) { |
| 98 | 7 | GetOutput().resize(total_size); | |
| 99 | } | ||
| 100 | |||
| 101 | 14 | MPI_Bcast(GetOutput().data(), total_size, MPI_DOUBLE, 0, MPI_COMM_WORLD); | |
| 102 | |||
| 103 | return true; | ||
| 104 | } | ||
| 105 | |||
| 106 | 14 | bool ZaharovGMatrixColSumMPI::PostProcessingImpl() { | |
| 107 | 14 | return true; | |
| 108 | } | ||
| 109 | |||
| 110 | 14 | std::vector<int> ZaharovGMatrixColSumMPI::CalcInterval(int thread_amount, int rank, int column_amount) { | |
| 111 | 14 | int base_chunk = column_amount / thread_amount; | |
| 112 | 14 | int remainder = column_amount % thread_amount; | |
| 113 | |||
| 114 |
2/2✓ Branch 0 taken 10 times.
✓ Branch 1 taken 4 times.
|
14 | int start = (rank * base_chunk) + std::min(rank, remainder); |
| 115 |
2/2✓ Branch 0 taken 10 times.
✓ Branch 1 taken 4 times.
|
14 | int end = start + base_chunk + (rank < remainder ? 1 : 0); |
| 116 | |||
| 117 | 14 | return {start, end}; // end is exclusive | |
| 118 | } | ||
| 119 | |||
| 120 | 14 | OutType ZaharovGMatrixColSumMPI::SumColValues(const int start, const int end) { | |
| 121 | 14 | OutType out; | |
| 122 | const InType &in = GetInput(); | ||
| 123 | |||
| 124 |
3/4✓ Branch 0 taken 14 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 13 times.
✓ Branch 3 taken 1 times.
|
28 | if (start < 0 || std::cmp_greater(end, in[0].size()) || start >= end) { |
| 125 | return out; | ||
| 126 | } | ||
| 127 | |||
| 128 |
1/2✓ Branch 1 taken 13 times.
✗ Branch 2 not taken.
|
13 | out.reserve(end - start); |
| 129 | |||
| 130 |
2/2✓ Branch 0 taken 26 times.
✓ Branch 1 taken 13 times.
|
39 | for (int col = start; col < end; col++) { |
| 131 | 26 | double sum = 0.0; | |
| 132 | |||
| 133 |
2/2✓ Branch 0 taken 78 times.
✓ Branch 1 taken 26 times.
|
104 | for (const auto &row : in) { |
| 134 | 78 | sum += row[col]; | |
| 135 | } | ||
| 136 | |||
| 137 | out.push_back(sum); | ||
| 138 | } | ||
| 139 | |||
| 140 | return out; | ||
| 141 | } | ||
| 142 | |||
| 143 | } // namespace zaharov_g_matrix_col_sum | ||
| 144 |