| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include "balchunayte_z_dot_product/mpi/include/ops_mpi.hpp" | ||
| 2 | |||
| 3 | #include <mpi.h> | ||
| 4 | |||
| 5 | #include <vector> | ||
| 6 | |||
| 7 | namespace balchunayte_z_dot_product { | ||
| 8 | |||
| 9 | 6 | bool BalchunayteZDotProductMPI::ValidationImpl() { | |
| 10 | 6 | MPI_Comm_rank(MPI_COMM_WORLD, &world_rank_); | |
| 11 | 6 | MPI_Comm_size(MPI_COMM_WORLD, &world_size_); | |
| 12 | |||
| 13 | 6 | int valid_flag = 0; | |
| 14 |
2/2✓ Branch 0 taken 3 times.
✓ Branch 1 taken 3 times.
|
6 | if (world_rank_ == 0) { |
| 15 | const auto &in = GetInput(); | ||
| 16 | const auto &a = in.a; | ||
| 17 | const auto &b = in.b; | ||
| 18 | |||
| 19 | bool is_valid = true; | ||
| 20 |
3/6✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 3 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 3 times.
|
3 | if (a.empty() || b.empty() || a.size() != b.size()) { |
| 21 | is_valid = false; | ||
| 22 | } | ||
| 23 | 3 | valid_flag = is_valid ? 1 : 0; | |
| 24 | } | ||
| 25 | |||
| 26 | 6 | MPI_Bcast(&valid_flag, 1, MPI_INT, 0, MPI_COMM_WORLD); | |
| 27 | 6 | return valid_flag == 1; | |
| 28 | } | ||
| 29 | |||
| 30 | 6 | bool BalchunayteZDotProductMPI::PreProcessingImpl() { | |
| 31 | 6 | MPI_Comm_rank(MPI_COMM_WORLD, &world_rank_); | |
| 32 | 6 | MPI_Comm_size(MPI_COMM_WORLD, &world_size_); | |
| 33 | |||
| 34 | 6 | int global_size = 0; | |
| 35 |
2/2✓ Branch 0 taken 3 times.
✓ Branch 1 taken 3 times.
|
6 | if (world_rank_ == 0) { |
| 36 | 3 | global_size = static_cast<int>(GetInput().a.size()); | |
| 37 | } | ||
| 38 | |||
| 39 | 6 | MPI_Bcast(&global_size, 1, MPI_INT, 0, MPI_COMM_WORLD); | |
| 40 | |||
| 41 | 6 | std::vector<int> counts(world_size_, 0); | |
| 42 |
1/4✓ Branch 1 taken 6 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
|
6 | std::vector<int> displs(world_size_, 0); |
| 43 | |||
| 44 | 6 | int base = global_size / world_size_; | |
| 45 | 6 | int rem = global_size % world_size_; | |
| 46 |
2/2✓ Branch 0 taken 12 times.
✓ Branch 1 taken 6 times.
|
18 | for (int rank_index = 0; rank_index < world_size_; ++rank_index) { |
| 47 |
2/2✓ Branch 0 taken 6 times.
✓ Branch 1 taken 6 times.
|
18 | counts[rank_index] = base + (rank_index < rem ? 1 : 0); |
| 48 | } | ||
| 49 |
2/2✓ Branch 0 taken 6 times.
✓ Branch 1 taken 6 times.
|
12 | for (int rank_index = 1; rank_index < world_size_; ++rank_index) { |
| 50 | 6 | displs[rank_index] = displs[rank_index - 1] + counts[rank_index - 1]; | |
| 51 | } | ||
| 52 | |||
| 53 |
1/2✓ Branch 1 taken 6 times.
✗ Branch 2 not taken.
|
6 | local_size_ = counts[world_rank_]; |
| 54 |
1/2✓ Branch 1 taken 6 times.
✗ Branch 2 not taken.
|
6 | local_a_.resize(local_size_); |
| 55 |
1/2✓ Branch 1 taken 6 times.
✗ Branch 2 not taken.
|
6 | local_b_.resize(local_size_); |
| 56 | |||
| 57 | double *send_a = nullptr; | ||
| 58 | double *send_b = nullptr; | ||
| 59 |
2/2✓ Branch 0 taken 3 times.
✓ Branch 1 taken 3 times.
|
6 | if (world_rank_ == 0) { |
| 60 | send_a = GetInput().a.data(); | ||
| 61 | send_b = GetInput().b.data(); | ||
| 62 | } | ||
| 63 | |||
| 64 |
1/2✓ Branch 1 taken 6 times.
✗ Branch 2 not taken.
|
6 | MPI_Scatterv(send_a, counts.data(), displs.data(), MPI_DOUBLE, local_a_.data(), local_size_, MPI_DOUBLE, 0, |
| 65 | MPI_COMM_WORLD); | ||
| 66 | |||
| 67 |
1/2✓ Branch 1 taken 6 times.
✗ Branch 2 not taken.
|
6 | MPI_Scatterv(send_b, counts.data(), displs.data(), MPI_DOUBLE, local_b_.data(), local_size_, MPI_DOUBLE, 0, |
| 68 | MPI_COMM_WORLD); | ||
| 69 | |||
| 70 |
1/2✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
|
6 | GetOutput() = 0.0; |
| 71 | 6 | return true; | |
| 72 | } | ||
| 73 | |||
| 74 | 6 | bool BalchunayteZDotProductMPI::RunImpl() { | |
| 75 | 6 | double local_sum = 0.0; | |
| 76 |
2/2✓ Branch 0 taken 9 times.
✓ Branch 1 taken 6 times.
|
15 | for (int i = 0; i < local_size_; ++i) { |
| 77 | 9 | local_sum += local_a_[i] * local_b_[i]; | |
| 78 | } | ||
| 79 | |||
| 80 | 6 | double global_sum = 0.0; | |
| 81 | 6 | MPI_Allreduce(&local_sum, &global_sum, 1, MPI_DOUBLE, MPI_SUM, MPI_COMM_WORLD); | |
| 82 | |||
| 83 | 6 | GetOutput() = global_sum; | |
| 84 | 6 | return true; | |
| 85 | } | ||
| 86 | |||
| 87 | 6 | bool BalchunayteZDotProductMPI::PostProcessingImpl() { | |
| 88 | 6 | return true; | |
| 89 | } | ||
| 90 | |||
| 91 | } // namespace balchunayte_z_dot_product | ||
| 92 |