| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include "chernykh_s_min_matrix_elements/mpi/include/ops_mpi.hpp" | ||
| 2 | |||
| 3 | #include <mpi.h> | ||
| 4 | |||
| 5 | #include <algorithm> | ||
| 6 | #include <cstddef> | ||
| 7 | #include <limits> | ||
| 8 | #include <vector> | ||
| 9 | |||
| 10 | #include "chernykh_s_min_matrix_elements/common/include/common.hpp" | ||
| 11 | |||
| 12 | namespace chernykh_s_min_matrix_elements { | ||
| 13 | |||
| 14 |
1/2✓ Branch 1 taken 12 times.
✗ Branch 2 not taken.
|
12 | ChernykhSMinMatrixElementsMPI::ChernykhSMinMatrixElementsMPI(const InType &in) { |
| 15 | SetTypeOfTask(GetStaticTypeOfTask()); | ||
| 16 |
1/2✓ Branch 1 taken 12 times.
✗ Branch 2 not taken.
|
12 | GetInput() = InType(in); |
| 17 | 12 | GetOutput() = std::numeric_limits<double>::max(); | |
| 18 | 12 | } | |
| 19 | |||
| 20 | 12 | bool ChernykhSMinMatrixElementsMPI::ValidationImpl() { | |
| 21 | 12 | return (GetOutput() == std::numeric_limits<double>::max()); | |
| 22 | } | ||
| 23 | |||
| 24 | 12 | bool ChernykhSMinMatrixElementsMPI::PreProcessingImpl() { | |
| 25 | 12 | return true; | |
| 26 | } | ||
| 27 | |||
| 28 | 12 | bool ChernykhSMinMatrixElementsMPI::RunImpl() { | |
| 29 | 12 | int rank = 0; | |
| 30 | 12 | int size = 0; | |
| 31 | |||
| 32 | 12 | MPI_Comm_rank(MPI_COMM_WORLD, &rank); | |
| 33 | 12 | MPI_Comm_size(MPI_COMM_WORLD, &size); | |
| 34 | |||
| 35 | 12 | std::vector<double> full_data_buffer; | |
| 36 | 12 | std::vector<double> local_portion; | |
| 37 | size_t total_elements = 0; | ||
| 38 | |||
| 39 |
2/2✓ Branch 0 taken 6 times.
✓ Branch 1 taken 6 times.
|
12 | if (rank == 0) { |
| 40 | const std::vector<std::vector<double>> &matrix = GetInput(); | ||
| 41 | |||
| 42 |
2/2✓ Branch 0 taken 127 times.
✓ Branch 1 taken 6 times.
|
133 | for (const auto &row : matrix) { |
| 43 | 127 | full_data_buffer.insert(full_data_buffer.end(), row.begin(), row.end()); | |
| 44 | } | ||
| 45 | total_elements = full_data_buffer.size(); | ||
| 46 | } | ||
| 47 | |||
| 48 | 12 | int total_elements_int = static_cast<int>(total_elements); | |
| 49 |
1/2✓ Branch 1 taken 12 times.
✗ Branch 2 not taken.
|
12 | MPI_Bcast(&total_elements_int, 1, MPI_INT, 0, MPI_COMM_WORLD); |
| 50 | 12 | total_elements = static_cast<size_t>(total_elements_int); | |
| 51 | |||
| 52 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 10 times.
|
12 | if (total_elements == 0) { |
| 53 | 2 | GetOutput() = std::numeric_limits<double>::max(); | |
| 54 | 2 | return true; | |
| 55 | } | ||
| 56 | |||
| 57 | 10 | int avg_elements_per_proc = static_cast<int>(total_elements / size); | |
| 58 | 10 | int remainder = static_cast<int>(total_elements % size); | |
| 59 | |||
| 60 |
1/4✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
|
10 | std::vector<int> elemcnt(size); |
| 61 |
1/4✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
|
10 | std::vector<int> startpos(size); |
| 62 | |||
| 63 |
2/2✓ Branch 0 taken 20 times.
✓ Branch 1 taken 10 times.
|
30 | for (int i = 0; i < size; ++i) { |
| 64 |
2/2✓ Branch 0 taken 10 times.
✓ Branch 1 taken 10 times.
|
20 | elemcnt[i] = avg_elements_per_proc; |
| 65 |
2/2✓ Branch 0 taken 10 times.
✓ Branch 1 taken 10 times.
|
20 | if (i == size - 1) { |
| 66 | 10 | elemcnt[i] += remainder; | |
| 67 | } | ||
| 68 |
2/2✓ Branch 0 taken 10 times.
✓ Branch 1 taken 10 times.
|
20 | if (i == 0) { |
| 69 | 10 | startpos[i] = 0; | |
| 70 | } else { | ||
| 71 | 10 | startpos[i] = startpos[i - 1] + elemcnt[i - 1]; | |
| 72 | } | ||
| 73 | } | ||
| 74 | |||
| 75 |
1/2✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
|
10 | local_portion.resize(elemcnt[rank]); |
| 76 | |||
| 77 | const double *send_buffer = nullptr; | ||
| 78 | |||
| 79 |
2/2✓ Branch 0 taken 5 times.
✓ Branch 1 taken 5 times.
|
10 | if (rank == 0) { |
| 80 | send_buffer = full_data_buffer.data(); | ||
| 81 | } | ||
| 82 | |||
| 83 |
1/2✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
|
10 | MPI_Scatterv(send_buffer, elemcnt.data(), startpos.data(), MPI_DOUBLE, local_portion.data(), elemcnt[rank], |
| 84 | MPI_DOUBLE, 0, MPI_COMM_WORLD); | ||
| 85 | |||
| 86 | 10 | double local_min = std::numeric_limits<double>::max(); | |
| 87 | |||
| 88 |
2/2✓ Branch 0 taken 5362 times.
✓ Branch 1 taken 10 times.
|
5372 | for (double element : local_portion) { |
| 89 | 5362 | local_min = std::min(element, local_min); | |
| 90 | } | ||
| 91 | |||
| 92 | 10 | double global_min = std::numeric_limits<double>::max(); | |
| 93 |
1/2✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
|
10 | MPI_Allreduce(&local_min, &global_min, 1, MPI_DOUBLE, MPI_MIN, MPI_COMM_WORLD); |
| 94 | |||
| 95 |
1/2✓ Branch 0 taken 10 times.
✗ Branch 1 not taken.
|
10 | GetOutput() = global_min; |
| 96 | |||
| 97 | return true; | ||
| 98 | } | ||
| 99 | |||
| 100 | 12 | bool ChernykhSMinMatrixElementsMPI::PostProcessingImpl() { | |
| 101 | 12 | return true; | |
| 102 | } | ||
| 103 | } // namespace chernykh_s_min_matrix_elements | ||
| 104 |