| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include "zyuzin_n_sum_elements_of_matrix/mpi/include/ops_mpi.hpp" | ||
| 2 | |||
| 3 | #include <mpi.h> | ||
| 4 | |||
| 5 | #include <cstddef> | ||
| 6 | #include <numeric> | ||
| 7 | #include <vector> | ||
| 8 | |||
| 9 | #include "zyuzin_n_sum_elements_of_matrix/common/include/common.hpp" | ||
| 10 | |||
| 11 | namespace zyuzin_n_sum_elements_of_matrix { | ||
| 12 | |||
| 13 |
1/2✓ Branch 1 taken 20 times.
✗ Branch 2 not taken.
|
20 | ZyuzinNSumElementsOfMatrixMPI::ZyuzinNSumElementsOfMatrixMPI(const InType &in) { |
| 14 | SetTypeOfTask(GetStaticTypeOfTask()); | ||
| 15 | GetInput() = in; | ||
| 16 | 20 | GetOutput() = 0.0; | |
| 17 | 20 | } | |
| 18 | |||
| 19 | 20 | bool ZyuzinNSumElementsOfMatrixMPI::ValidationImpl() { | |
| 20 | const auto &matrix = GetInput(); | ||
| 21 |
2/4✓ Branch 0 taken 20 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 20 times.
|
20 | return std::get<0>(matrix) > 0 && std::get<1>(matrix) > 0 && |
| 22 | std::get<2>(matrix).size() == | ||
| 23 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 20 times.
|
20 | static_cast<std::size_t>(std::get<0>(matrix) * static_cast<std::size_t>(std::get<1>(matrix))); |
| 24 | } | ||
| 25 | |||
| 26 | 20 | bool ZyuzinNSumElementsOfMatrixMPI::PreProcessingImpl() { | |
| 27 | 20 | return true; | |
| 28 | } | ||
| 29 | |||
| 30 | 20 | bool ZyuzinNSumElementsOfMatrixMPI::RunImpl() { | |
| 31 | const auto &matrix = GetInput(); | ||
| 32 | 20 | int rank = 0; | |
| 33 | 20 | int size = 0; | |
| 34 | 20 | MPI_Comm_rank(MPI_COMM_WORLD, &rank); | |
| 35 | 20 | MPI_Comm_size(MPI_COMM_WORLD, &size); | |
| 36 | |||
| 37 | 20 | int rows_per_proc = std::get<0>(matrix) / size; | |
| 38 | 20 | int ost = std::get<0>(matrix) % size; | |
| 39 | |||
| 40 | 20 | std::vector<int> send_counts(size, 0); | |
| 41 |
1/4✓ Branch 1 taken 20 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
|
20 | std::vector<int> start_indexs(size, 0); |
| 42 | int offset = 0; | ||
| 43 |
2/2✓ Branch 0 taken 40 times.
✓ Branch 1 taken 20 times.
|
60 | for (int i = 0; i < size; ++i) { |
| 44 |
2/2✓ Branch 0 taken 10 times.
✓ Branch 1 taken 30 times.
|
40 | int rows = rows_per_proc + (ost > 0 ? 1 : 0); |
| 45 | if (ost > 0) { | ||
| 46 | 10 | --ost; | |
| 47 | } | ||
| 48 | 40 | send_counts[i] = rows * std::get<1>(matrix); | |
| 49 | 40 | start_indexs[i] = offset; | |
| 50 | 40 | offset += send_counts[i]; | |
| 51 | } | ||
| 52 | |||
| 53 |
1/4✓ Branch 1 taken 20 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
|
20 | std::vector<double> local_data(send_counts[rank]); |
| 54 |
1/2✓ Branch 1 taken 20 times.
✗ Branch 2 not taken.
|
20 | MPI_Scatterv(std::get<2>(matrix).data(), send_counts.data(), start_indexs.data(), MPI_DOUBLE, local_data.data(), |
| 55 |
1/2✓ Branch 1 taken 20 times.
✗ Branch 2 not taken.
|
20 | send_counts[rank], MPI_DOUBLE, 0, MPI_COMM_WORLD); |
| 56 | |||
| 57 | 20 | double local_sum = std::accumulate(local_data.begin(), local_data.end(), 0.0); | |
| 58 | 20 | double global_sum = 0.0; | |
| 59 |
1/2✓ Branch 1 taken 20 times.
✗ Branch 2 not taken.
|
20 | MPI_Allreduce(&local_sum, &global_sum, 1, MPI_DOUBLE, MPI_SUM, MPI_COMM_WORLD); |
| 60 | |||
| 61 |
2/2✓ Branch 0 taken 18 times.
✓ Branch 1 taken 2 times.
|
20 | GetOutput() = global_sum; |
| 62 | 20 | return true; | |
| 63 | } | ||
| 64 | |||
| 65 | 20 | bool ZyuzinNSumElementsOfMatrixMPI::PostProcessingImpl() { | |
| 66 | 20 | return true; | |
| 67 | } | ||
| 68 | |||
| 69 | } // namespace zyuzin_n_sum_elements_of_matrix | ||
| 70 |