| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include "afanasyev_a_elem_vec_avg/mpi/include/ops_mpi.hpp" | ||
| 2 | |||
| 3 | #include <mpi.h> | ||
| 4 | |||
| 5 | #include <cstdint> | ||
| 6 | #include <numeric> | ||
| 7 | #include <vector> | ||
| 8 | |||
| 9 | #include "afanasyev_a_elem_vec_avg/common/include/common.hpp" | ||
| 10 | |||
| 11 | namespace afanasyev_a_elem_vec_avg { | ||
| 12 | |||
| 13 |
1/2✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
|
10 | AfanasyevAElemVecAvgMPI::AfanasyevAElemVecAvgMPI(const InType &in) { |
| 14 | SetTypeOfTask(GetStaticTypeOfTask()); | ||
| 15 |
1/2✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
|
10 | GetInput() = in; |
| 16 | 10 | GetOutput() = 0.0; | |
| 17 | 10 | } | |
| 18 | |||
| 19 | 10 | bool AfanasyevAElemVecAvgMPI::ValidationImpl() { | |
| 20 | 10 | return true; | |
| 21 | } | ||
| 22 | |||
| 23 | 10 | bool AfanasyevAElemVecAvgMPI::PreProcessingImpl() { | |
| 24 | 10 | return true; | |
| 25 | } | ||
| 26 | |||
| 27 | 10 | bool AfanasyevAElemVecAvgMPI::RunImpl() { | |
| 28 | 10 | int rank = 0; | |
| 29 | 10 | int num_processes = 0; | |
| 30 | 10 | MPI_Comm_rank(MPI_COMM_WORLD, &rank); | |
| 31 | 10 | MPI_Comm_size(MPI_COMM_WORLD, &num_processes); | |
| 32 | |||
| 33 | const InType &global_vec = GetInput(); | ||
| 34 | 10 | int global_n = static_cast<int>(global_vec.size()); | |
| 35 | |||
| 36 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 8 times.
|
10 | if (global_n == 0) { |
| 37 | 2 | GetOutput() = 0.0; | |
| 38 | 2 | MPI_Bcast(static_cast<void *>(&GetOutput()), 1, MPI_DOUBLE, 0, MPI_COMM_WORLD); | |
| 39 | 2 | return true; | |
| 40 | } | ||
| 41 | |||
| 42 | 8 | std::vector<int> send_counts(num_processes); | |
| 43 |
1/4✓ Branch 1 taken 8 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
|
8 | std::vector<int> displs(num_processes); |
| 44 | 8 | int chunk_size = global_n / num_processes; | |
| 45 | 8 | int remainder = global_n % num_processes; | |
| 46 | int current_displacement = 0; | ||
| 47 | |||
| 48 |
2/2✓ Branch 0 taken 16 times.
✓ Branch 1 taken 8 times.
|
24 | for (int i = 0; i < num_processes; ++i) { |
| 49 |
2/2✓ Branch 0 taken 14 times.
✓ Branch 1 taken 2 times.
|
16 | int count = chunk_size + (i < remainder ? 1 : 0); |
| 50 | 16 | send_counts[i] = count; | |
| 51 | 16 | displs[i] = current_displacement; | |
| 52 | 16 | current_displacement += count; | |
| 53 | } | ||
| 54 | |||
| 55 |
1/2✓ Branch 1 taken 8 times.
✗ Branch 2 not taken.
|
8 | int local_n = send_counts[rank]; |
| 56 |
3/6✓ Branch 1 taken 8 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 4 times.
✓ Branch 4 taken 4 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
|
8 | std::vector<int> local_vec(local_n); |
| 57 | |||
| 58 |
3/4✓ Branch 0 taken 4 times.
✓ Branch 1 taken 4 times.
✓ Branch 3 taken 8 times.
✗ Branch 4 not taken.
|
12 | MPI_Scatterv(rank == 0 ? global_vec.data() : nullptr, send_counts.data(), displs.data(), MPI_INT, local_vec.data(), |
| 59 | local_n, MPI_INT, 0, MPI_COMM_WORLD); | ||
| 60 | |||
| 61 | 8 | int64_t local_sum = std::accumulate(local_vec.begin(), local_vec.end(), static_cast<int64_t>(0)); | |
| 62 | 8 | int64_t global_sum = 0; | |
| 63 | |||
| 64 |
1/2✓ Branch 1 taken 8 times.
✗ Branch 2 not taken.
|
8 | MPI_Reduce(&local_sum, &global_sum, 1, MPI_INT64_T, MPI_SUM, 0, MPI_COMM_WORLD); |
| 65 | |||
| 66 |
2/2✓ Branch 0 taken 4 times.
✓ Branch 1 taken 4 times.
|
8 | if (rank == 0) { |
| 67 | 4 | GetOutput() = static_cast<OutType>(global_sum) / static_cast<double>(global_n); | |
| 68 | } | ||
| 69 | |||
| 70 |
1/2✓ Branch 1 taken 8 times.
✗ Branch 2 not taken.
|
8 | MPI_Bcast(static_cast<void *>(&GetOutput()), 1, MPI_DOUBLE, 0, MPI_COMM_WORLD); |
| 71 | |||
| 72 | return true; | ||
| 73 | } | ||
| 74 | |||
| 75 | 10 | bool AfanasyevAElemVecAvgMPI::PostProcessingImpl() { | |
| 76 | 10 | return true; | |
| 77 | } | ||
| 78 | |||
| 79 | } // namespace afanasyev_a_elem_vec_avg | ||
| 80 |