| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include "likhanov_m_elem_vec_sum/mpi/include/ops_mpi.hpp" | ||
| 2 | |||
| 3 | #include <mpi.h> | ||
| 4 | |||
| 5 | #include <algorithm> | ||
| 6 | #include <cstdint> | ||
| 7 | |||
| 8 | #include "likhanov_m_elem_vec_sum/common/include/common.hpp" | ||
| 9 | |||
| 10 | namespace likhanov_m_elem_vec_sum { | ||
| 11 | |||
| 12 | 10 | LikhanovMElemVecSumMPI::LikhanovMElemVecSumMPI(const InType &in) { | |
| 13 | SetTypeOfTask(GetStaticTypeOfTask()); | ||
| 14 | 10 | GetInput() = in; | |
| 15 | GetOutput() = 0; | ||
| 16 | 10 | } | |
| 17 | |||
| 18 | 10 | bool LikhanovMElemVecSumMPI::ValidationImpl() { | |
| 19 | 10 | return GetInput() >= 0; | |
| 20 | } | ||
| 21 | |||
| 22 | 10 | bool LikhanovMElemVecSumMPI::PreProcessingImpl() { | |
| 23 | 10 | return true; | |
| 24 | } | ||
| 25 | |||
| 26 | 10 | bool LikhanovMElemVecSumMPI::RunImpl() { | |
| 27 | 10 | int rank = 0; | |
| 28 | 10 | int size = 0; | |
| 29 | 10 | MPI_Comm_rank(MPI_COMM_WORLD, &rank); | |
| 30 | 10 | MPI_Comm_size(MPI_COMM_WORLD, &size); | |
| 31 | |||
| 32 | 10 | const int64_t n = GetInput(); | |
| 33 | |||
| 34 | 10 | int64_t base = n / size; | |
| 35 | 10 | int64_t rem = n % size; | |
| 36 | |||
| 37 |
2/2✓ Branch 0 taken 9 times.
✓ Branch 1 taken 1 times.
|
10 | int64_t local_begin = (rank * base) + std::min<int64_t>(rank, rem) + 1; |
| 38 | |||
| 39 |
2/2✓ Branch 0 taken 9 times.
✓ Branch 1 taken 1 times.
|
10 | int64_t local_end = local_begin + base - 1 + (rank < rem ? 1 : 0); |
| 40 | |||
| 41 | 10 | int64_t local_sum = 0; | |
| 42 |
2/2✓ Branch 0 taken 1111 times.
✓ Branch 1 taken 10 times.
|
1121 | for (int64_t i = local_begin; i <= local_end; ++i) { |
| 43 | 1111 | local_sum += i; | |
| 44 | } | ||
| 45 | |||
| 46 | 10 | int64_t global_sum = 0; | |
| 47 | 10 | MPI_Reduce(&local_sum, &global_sum, 1, MPI_INT64_T, MPI_SUM, 0, MPI_COMM_WORLD); | |
| 48 | |||
| 49 |
2/2✓ Branch 0 taken 5 times.
✓ Branch 1 taken 5 times.
|
10 | if (rank == 0) { |
| 50 | 5 | GetOutput() = global_sum; | |
| 51 | } | ||
| 52 | |||
| 53 | 10 | return true; | |
| 54 | } | ||
| 55 | |||
| 56 | 10 | bool LikhanovMElemVecSumMPI::PostProcessingImpl() { | |
| 57 | 10 | return true; | |
| 58 | } | ||
| 59 | |||
| 60 | } // namespace likhanov_m_elem_vec_sum | ||
| 61 |