| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include "kamalagin_a_vec_mult/mpi/include/ops_mpi.hpp" | ||
| 2 | |||
| 3 | #include <mpi.h> | ||
| 4 | |||
| 5 | #include <cstdint> | ||
| 6 | #include <utility> | ||
| 7 | #include <vector> | ||
| 8 | |||
| 9 | #include "kamalagin_a_vec_mult/common/include/common.hpp" | ||
| 10 | |||
| 11 | namespace kamalagin_a_vec_mult { | ||
| 12 | |||
| 13 |
1/2✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
|
10 | KamalaginAVecMultMPI::KamalaginAVecMultMPI(const InType &in) { |
| 14 | SetTypeOfTask(GetStaticTypeOfTask()); | ||
| 15 | GetInput() = in; | ||
| 16 | 10 | GetOutput() = 0; | |
| 17 | 10 | } | |
| 18 | |||
| 19 | 10 | bool KamalaginAVecMultMPI::ValidationImpl() { | |
| 20 | const auto &[a, b] = GetInput(); | ||
| 21 | 10 | return a.size() == b.size(); | |
| 22 | } | ||
| 23 | |||
| 24 | 10 | bool KamalaginAVecMultMPI::PreProcessingImpl() { | |
| 25 | 10 | GetOutput() = 0; | |
| 26 | 10 | return true; | |
| 27 | } | ||
| 28 | |||
| 29 | namespace { | ||
| 30 | 8 | void BuildCountsDispls(int n, int size, std::vector<int> *counts, std::vector<int> *displs) { | |
| 31 | 8 | counts->assign(size, 0); | |
| 32 | 8 | displs->assign(size, 0); | |
| 33 | |||
| 34 | 8 | const int base = n / size; | |
| 35 | 8 | const int rem = n % size; | |
| 36 | |||
| 37 | int offset = 0; | ||
| 38 |
2/2✓ Branch 0 taken 16 times.
✓ Branch 1 taken 8 times.
|
24 | for (int proc = 0; proc < size; ++proc) { |
| 39 |
2/2✓ Branch 0 taken 12 times.
✓ Branch 1 taken 4 times.
|
16 | const int cnt = base + (proc < rem ? 1 : 0); |
| 40 | 16 | (*counts)[proc] = cnt; | |
| 41 | 16 | (*displs)[proc] = offset; | |
| 42 | 16 | offset += cnt; | |
| 43 | } | ||
| 44 | 8 | } | |
| 45 | } // namespace | ||
| 46 | |||
| 47 | 10 | bool KamalaginAVecMultMPI::RunImpl() { | |
| 48 | 10 | int rank = 0; | |
| 49 | 10 | int size = 1; | |
| 50 | 10 | MPI_Comm_rank(MPI_COMM_WORLD, &rank); | |
| 51 | 10 | MPI_Comm_size(MPI_COMM_WORLD, &size); | |
| 52 | |||
| 53 | 10 | std::vector<int> a_root; | |
| 54 | 10 | std::vector<int> b_root; | |
| 55 | 10 | int n = 0; | |
| 56 | |||
| 57 |
2/2✓ Branch 0 taken 5 times.
✓ Branch 1 taken 5 times.
|
10 | if (rank == 0) { |
| 58 | const auto &[a, b] = GetInput(); | ||
| 59 |
1/2✓ Branch 1 taken 5 times.
✗ Branch 2 not taken.
|
5 | a_root = a; |
| 60 |
1/2✓ Branch 1 taken 5 times.
✗ Branch 2 not taken.
|
5 | b_root = b; |
| 61 | 5 | n = static_cast<int>(a_root.size()); | |
| 62 | } | ||
| 63 | |||
| 64 |
1/2✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
|
10 | MPI_Bcast(&n, 1, MPI_INT, 0, MPI_COMM_WORLD); |
| 65 | |||
| 66 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 8 times.
|
10 | if (n == 0) { |
| 67 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
|
2 | if (rank == 0) { |
| 68 | 1 | GetOutput() = 0; | |
| 69 | } | ||
| 70 | 2 | return true; | |
| 71 | } | ||
| 72 | |||
| 73 | 8 | std::vector<int> counts; | |
| 74 | 8 | std::vector<int> displs; | |
| 75 |
1/2✓ Branch 1 taken 8 times.
✗ Branch 2 not taken.
|
8 | BuildCountsDispls(n, size, &counts, &displs); |
| 76 | |||
| 77 |
1/2✓ Branch 1 taken 8 times.
✗ Branch 2 not taken.
|
8 | const int local_n = counts[rank]; |
| 78 |
1/4✓ Branch 1 taken 8 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
|
8 | std::vector<int> a_local(local_n); |
| 79 |
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> b_local(local_n); |
| 80 | |||
| 81 |
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 ? a_root.data() : nullptr, counts.data(), displs.data(), MPI_INT, a_local.data(), local_n, |
| 82 | MPI_INT, 0, MPI_COMM_WORLD); | ||
| 83 | |||
| 84 |
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 ? b_root.data() : nullptr, counts.data(), displs.data(), MPI_INT, b_local.data(), local_n, |
| 85 | MPI_INT, 0, MPI_COMM_WORLD); | ||
| 86 | |||
| 87 | 8 | std::int64_t local_sum = 0; | |
| 88 |
2/2✓ Branch 0 taken 1268 times.
✓ Branch 1 taken 8 times.
|
1276 | for (int i = 0; i < local_n; ++i) { |
| 89 | 1268 | local_sum += static_cast<std::int64_t>(a_local[i]) * static_cast<std::int64_t>(b_local[i]); | |
| 90 | } | ||
| 91 | |||
| 92 | 8 | std::int64_t global_sum = 0; | |
| 93 |
1/2✓ Branch 1 taken 8 times.
✗ Branch 2 not taken.
|
8 | MPI_Allreduce(&local_sum, &global_sum, 1, MPI_INT64_T, MPI_SUM, MPI_COMM_WORLD); |
| 94 |
2/2✓ Branch 0 taken 7 times.
✓ Branch 1 taken 1 times.
|
8 | GetOutput() = global_sum; |
| 95 | |||
| 96 | return true; | ||
| 97 | } | ||
| 98 | |||
| 99 | 10 | bool KamalaginAVecMultMPI::PostProcessingImpl() { | |
| 100 | 10 | return true; | |
| 101 | } | ||
| 102 | |||
| 103 | } // namespace kamalagin_a_vec_mult | ||
| 104 |