| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include "lukin_i_elem_vec_sum/mpi/include/ops_mpi.hpp" | ||
| 2 | |||
| 3 | #include <mpi.h> | ||
| 4 | |||
| 5 | #include <numeric> | ||
| 6 | #include <vector> | ||
| 7 | |||
| 8 | #include "lukin_i_elem_vec_sum/common/include/common.hpp" | ||
| 9 | |||
| 10 | namespace lukin_i_elem_vec_sum { | ||
| 11 | |||
| 12 |
1/2✓ Branch 1 taken 22 times.
✗ Branch 2 not taken.
|
22 | LukinIElemVecSumMPI::LukinIElemVecSumMPI(const InType &in) { |
| 13 | SetTypeOfTask(GetStaticTypeOfTask()); | ||
| 14 | |||
| 15 | 22 | int rank = 0; | |
| 16 |
1/2✓ Branch 1 taken 22 times.
✗ Branch 2 not taken.
|
22 | MPI_Comm_rank(MPI_COMM_WORLD, &rank); |
| 17 |
2/2✓ Branch 0 taken 11 times.
✓ Branch 1 taken 11 times.
|
22 | if (rank == 0) { |
| 18 |
1/2✓ Branch 1 taken 11 times.
✗ Branch 2 not taken.
|
11 | GetInput() = in; |
| 19 | } | ||
| 20 | |||
| 21 | 22 | GetOutput() = 0; | |
| 22 | 22 | } | |
| 23 | |||
| 24 | 22 | bool LukinIElemVecSumMPI::ValidationImpl() { | |
| 25 | 22 | int rank = 0; | |
| 26 | 22 | MPI_Comm_rank(MPI_COMM_WORLD, &rank); | |
| 27 | |||
| 28 |
2/2✓ Branch 0 taken 11 times.
✓ Branch 1 taken 11 times.
|
22 | if (rank == 0) { |
| 29 | 11 | return (static_cast<int>(GetInput().size()) != 0); | |
| 30 | } | ||
| 31 | return true; | ||
| 32 | } | ||
| 33 | |||
| 34 | 22 | bool LukinIElemVecSumMPI::PreProcessingImpl() { | |
| 35 | 22 | int rank = 0; | |
| 36 | 22 | MPI_Comm_rank(MPI_COMM_WORLD, &rank); | |
| 37 | |||
| 38 |
2/2✓ Branch 0 taken 11 times.
✓ Branch 1 taken 11 times.
|
22 | if (rank == 0) { |
| 39 | 11 | vec_size_ = static_cast<int>(GetInput().size()); | |
| 40 | } | ||
| 41 | 22 | return true; | |
| 42 | } | ||
| 43 | |||
| 44 | 22 | bool LukinIElemVecSumMPI::RunImpl() { | |
| 45 | 22 | int proc_count = 0; | |
| 46 | 22 | int rank = 0; | |
| 47 | 22 | MPI_Comm_size(MPI_COMM_WORLD, &proc_count); | |
| 48 | 22 | MPI_Comm_rank(MPI_COMM_WORLD, &rank); | |
| 49 | |||
| 50 | 22 | MPI_Bcast(&vec_size_, 1, MPI_INT, 0, MPI_COMM_WORLD); | |
| 51 | |||
| 52 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 20 times.
|
22 | if (proc_count > vec_size_) { |
| 53 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
|
2 | if (rank == 0) { |
| 54 | 1 | GetOutput() = std::accumulate(GetInput().begin(), GetInput().end(), 0LL); | |
| 55 | } else { | ||
| 56 | 1 | GetOutput() = 0; | |
| 57 | } | ||
| 58 | 2 | OutType output = GetOutput(); | |
| 59 | 2 | MPI_Bcast(&output, 1, MPI_LONG_LONG, 0, MPI_COMM_WORLD); | |
| 60 | 2 | GetOutput() = output; | |
| 61 | return true; | ||
| 62 | } | ||
| 63 | |||
| 64 | 20 | std::vector<int> sendcounts(proc_count); | |
| 65 |
1/4✓ Branch 1 taken 20 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
|
20 | std::vector<int> offsets(proc_count); |
| 66 | |||
| 67 | 20 | const int part = vec_size_ / proc_count; | |
| 68 | 20 | const int reminder = vec_size_ % proc_count; | |
| 69 | |||
| 70 | int offset = 0; | ||
| 71 |
2/2✓ Branch 0 taken 40 times.
✓ Branch 1 taken 20 times.
|
60 | for (int i = 0; i < proc_count; i++) { |
| 72 |
2/2✓ Branch 0 taken 30 times.
✓ Branch 1 taken 10 times.
|
70 | sendcounts[i] = part + (i < reminder ? 1 : 0); |
| 73 | 40 | offsets[i] = offset; | |
| 74 | 40 | offset += sendcounts[i]; | |
| 75 | } | ||
| 76 | |||
| 77 |
1/2✓ Branch 1 taken 20 times.
✗ Branch 2 not taken.
|
20 | int local_size = sendcounts[rank]; |
| 78 |
3/6✓ Branch 1 taken 20 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 10 times.
✓ Branch 4 taken 10 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
|
20 | std::vector<int> local_vec(local_size); |
| 79 | |||
| 80 |
3/4✓ Branch 0 taken 10 times.
✓ Branch 1 taken 10 times.
✓ Branch 3 taken 20 times.
✗ Branch 4 not taken.
|
30 | MPI_Scatterv(rank == 0 ? GetInput().data() : nullptr, sendcounts.data(), offsets.data(), MPI_INT, local_vec.data(), |
| 81 | local_size, MPI_INT, 0, MPI_COMM_WORLD); | ||
| 82 | |||
| 83 | 20 | OutType local_sum = std::accumulate(local_vec.begin(), local_vec.end(), 0LL); | |
| 84 | |||
| 85 | 20 | OutType global_sum = 0LL; | |
| 86 | |||
| 87 |
1/2✓ Branch 1 taken 20 times.
✗ Branch 2 not taken.
|
20 | MPI_Allreduce(&local_sum, &global_sum, 1, MPI_LONG_LONG, MPI_SUM, MPI_COMM_WORLD); |
| 88 | |||
| 89 |
1/2✓ Branch 0 taken 20 times.
✗ Branch 1 not taken.
|
20 | GetOutput() = global_sum; |
| 90 | |||
| 91 | return true; | ||
| 92 | } | ||
| 93 | |||
| 94 | 22 | bool LukinIElemVecSumMPI::PostProcessingImpl() { | |
| 95 | 22 | return true; | |
| 96 | } | ||
| 97 | |||
| 98 | } // namespace lukin_i_elem_vec_sum | ||
| 99 |