| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include "savva_d_min_elem_vec/mpi/include/ops_mpi.hpp" | ||
| 2 | |||
| 3 | #include <mpi.h> | ||
| 4 | |||
| 5 | #include <algorithm> | ||
| 6 | #include <limits> | ||
| 7 | #include <vector> | ||
| 8 | |||
| 9 | #include "savva_d_min_elem_vec/common/include/common.hpp" | ||
| 10 | |||
| 11 | namespace savva_d_min_elem_vec { | ||
| 12 | |||
| 13 |
1/2✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
|
10 | SavvaDMinElemVecMPI::SavvaDMinElemVecMPI(const InType &in) { // эта функция не изменяется в задачах |
| 14 | SetTypeOfTask(GetStaticTypeOfTask()); // конструктор правильная постановка задачи | ||
| 15 |
1/2✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
|
10 | GetInput() = in; // GetInput() нужен чтобы больше не использовать сами данные in |
| 16 | 10 | GetOutput() = 0; | |
| 17 | 10 | } | |
| 18 | |||
| 19 |
1/2✓ Branch 0 taken 10 times.
✗ Branch 1 not taken.
|
10 | bool SavvaDMinElemVecMPI::ValidationImpl() { |
| 20 |
2/4✓ Branch 0 taken 10 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 10 times.
|
10 | return (!GetInput().empty()) && (GetOutput() == 0); |
| 21 | } | ||
| 22 | |||
| 23 | 10 | bool SavvaDMinElemVecMPI::PreProcessingImpl() { | |
| 24 | 10 | return true; | |
| 25 | } | ||
| 26 | |||
| 27 | 10 | bool SavvaDMinElemVecMPI::RunImpl() { | |
| 28 | 10 | int rank = 0; | |
| 29 | 10 | int size = 0; | |
| 30 | 10 | MPI_Comm_rank(MPI_COMM_WORLD, &rank); | |
| 31 | 10 | MPI_Comm_size(MPI_COMM_WORLD, &size); | |
| 32 | int local_n = 0; | ||
| 33 | 10 | int global_n = 0; | |
| 34 | int *local_data = nullptr; | ||
| 35 | const int *global_data = nullptr; | ||
| 36 |
3/4✓ Branch 0 taken 10 times.
✗ Branch 1 not taken.
✓ Branch 4 taken 20 times.
✓ Branch 5 taken 10 times.
|
30 | int *counts = new int[size](); |
| 37 |
3/4✓ Branch 0 taken 10 times.
✗ Branch 1 not taken.
✓ Branch 4 taken 20 times.
✓ Branch 5 taken 10 times.
|
30 | int *displacements = new int[size](); |
| 38 | |||
| 39 | // если вектор - пустой, то false | ||
| 40 |
2/2✓ Branch 0 taken 5 times.
✓ Branch 1 taken 5 times.
|
10 | if (rank == 0) { |
| 41 | global_data = GetInput().data(); | ||
| 42 | 5 | global_n = static_cast<int>(GetInput().size()); | |
| 43 | 5 | int elements_per_proc = global_n / size; | |
| 44 | 5 | int remainder = global_n % size; | |
| 45 | int offset = 0; | ||
| 46 | |||
| 47 |
2/2✓ Branch 0 taken 10 times.
✓ Branch 1 taken 5 times.
|
15 | for (int i = 0; i < size; ++i) { |
| 48 |
2/2✓ Branch 0 taken 7 times.
✓ Branch 1 taken 3 times.
|
10 | counts[i] = elements_per_proc + (i < remainder ? 1 : 0); |
| 49 | 10 | displacements[i] = offset; | |
| 50 | 10 | offset += counts[i]; | |
| 51 | } | ||
| 52 | } | ||
| 53 | |||
| 54 | 10 | MPI_Bcast(&global_n, 1, MPI_INT, 0, MPI_COMM_WORLD); | |
| 55 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 10 times.
|
10 | if (global_n == 0) { |
| 56 | ✗ | delete[] counts; | |
| 57 | ✗ | delete[] displacements; | |
| 58 | ✗ | return false; | |
| 59 | } | ||
| 60 | 10 | MPI_Bcast(counts, size, MPI_INT, 0, MPI_COMM_WORLD); | |
| 61 |
1/2✓ Branch 0 taken 10 times.
✗ Branch 1 not taken.
|
10 | local_data = new int[counts[rank]]; |
| 62 | 10 | local_n = counts[rank]; | |
| 63 | 10 | MPI_Scatterv(global_data, counts, displacements, MPI_INT, local_data, local_n, MPI_INT, 0, MPI_COMM_WORLD); | |
| 64 | |||
| 65 | 10 | int local_min = std::numeric_limits<int>::max(); | |
| 66 |
2/2✓ Branch 0 taken 29 times.
✓ Branch 1 taken 10 times.
|
39 | for (int i = 0; i < local_n; ++i) { |
| 67 |
2/2✓ Branch 0 taken 7 times.
✓ Branch 1 taken 22 times.
|
36 | local_min = std::min(local_data[i], local_min); |
| 68 | } | ||
| 69 | |||
| 70 | 10 | int global_min = 0; | |
| 71 | 10 | MPI_Allreduce(&local_min, &global_min, 1, MPI_INT, MPI_MIN, MPI_COMM_WORLD); | |
| 72 | |||
| 73 | 10 | GetOutput() = global_min; | |
| 74 | |||
| 75 | 10 | delete[] counts; | |
| 76 | 10 | delete[] displacements; | |
| 77 | 10 | delete[] local_data; | |
| 78 | |||
| 79 | // Синхронизация | ||
| 80 | 10 | MPI_Barrier(MPI_COMM_WORLD); | |
| 81 | |||
| 82 | return true; | ||
| 83 | } | ||
| 84 | |||
| 85 | 10 | bool SavvaDMinElemVecMPI::PostProcessingImpl() { | |
| 86 | 10 | return true; | |
| 87 | } | ||
| 88 | |||
| 89 | } // namespace savva_d_min_elem_vec | ||
| 90 |