| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include "lopatin_a_scalar_mult/mpi/include/ops_mpi.hpp" | ||
| 2 | |||
| 3 | #include <mpi.h> | ||
| 4 | |||
| 5 | #include <cstdint> | ||
| 6 | #include <limits> | ||
| 7 | #include <stdexcept> | ||
| 8 | #include <utility> | ||
| 9 | #include <vector> | ||
| 10 | |||
| 11 | #include "lopatin_a_scalar_mult/common/include/common.hpp" | ||
| 12 | |||
| 13 | namespace lopatin_a_scalar_mult { | ||
| 14 | |||
| 15 |
1/2✓ Branch 1 taken 6 times.
✗ Branch 2 not taken.
|
6 | LopatinAScalarMultMPI::LopatinAScalarMultMPI(const InType &in) { |
| 16 | SetTypeOfTask(GetStaticTypeOfTask()); | ||
| 17 | 6 | int proc_rank = 0; | |
| 18 |
1/2✓ Branch 1 taken 6 times.
✗ Branch 2 not taken.
|
6 | MPI_Comm_rank(MPI_COMM_WORLD, &proc_rank); |
| 19 | |||
| 20 |
2/2✓ Branch 0 taken 3 times.
✓ Branch 1 taken 3 times.
|
6 | if (proc_rank == 0) { |
| 21 | GetInput() = in; | ||
| 22 | } else { | ||
| 23 | 3 | GetInput() = InType{}; | |
| 24 | } | ||
| 25 | |||
| 26 | 6 | GetOutput() = 0.0; | |
| 27 | 6 | } | |
| 28 | |||
| 29 | 6 | bool LopatinAScalarMultMPI::ValidationImpl() { | |
| 30 | 6 | int proc_rank = 0; | |
| 31 | 6 | MPI_Comm_rank(MPI_COMM_WORLD, &proc_rank); | |
| 32 | |||
| 33 |
2/2✓ Branch 0 taken 3 times.
✓ Branch 1 taken 3 times.
|
6 | if (proc_rank == 0) { |
| 34 |
3/6✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 3 times.
✗ Branch 5 not taken.
|
3 | return (!GetInput().first.empty() && !GetInput().second.empty()) && |
| 35 | (GetInput().first.size() == GetInput().second.size()); | ||
| 36 | } | ||
| 37 | return true; | ||
| 38 | } | ||
| 39 | |||
| 40 | 6 | bool LopatinAScalarMultMPI::PreProcessingImpl() { | |
| 41 | 6 | GetOutput() = 0.0; | |
| 42 | 6 | return (GetOutput() == 0.0); | |
| 43 | } | ||
| 44 | |||
| 45 | 6 | bool LopatinAScalarMultMPI::RunImpl() { | |
| 46 | 6 | int proc_num = 0; | |
| 47 | 6 | int proc_rank = 0; | |
| 48 | 6 | MPI_Comm_size(MPI_COMM_WORLD, &proc_num); | |
| 49 | 6 | MPI_Comm_rank(MPI_COMM_WORLD, &proc_rank); | |
| 50 | |||
| 51 | const auto &input = GetInput(); | ||
| 52 | OutType &total_res = GetOutput(); | ||
| 53 | |||
| 54 | 6 | auto n = static_cast<uint64_t>(input.first.size()); | |
| 55 | 6 | MPI_Bcast(&n, 1, MPI_UINT64_T, 0, MPI_COMM_WORLD); | |
| 56 | |||
| 57 | 6 | uint64_t local_n = n / static_cast<uint64_t>(proc_num); | |
| 58 | |||
| 59 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
|
6 | if (local_n > static_cast<uint64_t>(std::numeric_limits<int>::max())) { |
| 60 | ✗ | throw std::runtime_error("Too large input vector size!"); | |
| 61 | } | ||
| 62 | |||
| 63 | 6 | int local_n_int = static_cast<int>(local_n); | |
| 64 | |||
| 65 |
1/2✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
|
6 | if (local_n_int > 0) { |
| 66 |
2/6✓ Branch 2 taken 6 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 6 times.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
|
6 | InType local_data = std::make_pair(std::vector<double>(local_n_int), std::vector<double>(local_n_int)); |
| 67 | |||
| 68 |
1/2✓ Branch 1 taken 6 times.
✗ Branch 2 not taken.
|
6 | MPI_Scatter(input.first.data(), local_n_int, MPI_DOUBLE, local_data.first.data(), local_n_int, MPI_DOUBLE, 0, |
| 69 | MPI_COMM_WORLD); | ||
| 70 |
1/2✓ Branch 1 taken 6 times.
✗ Branch 2 not taken.
|
6 | MPI_Scatter(input.second.data(), local_n_int, MPI_DOUBLE, local_data.second.data(), local_n_int, MPI_DOUBLE, 0, |
| 71 | MPI_COMM_WORLD); | ||
| 72 | |||
| 73 | 6 | OutType proc_res{}; | |
| 74 |
2/2✓ Branch 0 taken 26 times.
✓ Branch 1 taken 6 times.
|
32 | for (int i = 0; i < local_n_int; ++i) { |
| 75 | 26 | proc_res += local_data.first[i] * local_data.second[i]; | |
| 76 | } | ||
| 77 | |||
| 78 |
1/2✓ Branch 1 taken 6 times.
✗ Branch 2 not taken.
|
6 | MPI_Reduce(&proc_res, &total_res, 1, MPI_DOUBLE, MPI_SUM, 0, MPI_COMM_WORLD); |
| 79 | 6 | } | |
| 80 | |||
| 81 |
2/2✓ Branch 0 taken 3 times.
✓ Branch 1 taken 3 times.
|
6 | if (proc_rank == 0) { |
| 82 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 2 times.
|
3 | if (n % proc_num != 0) { |
| 83 | 1 | uint64_t tail_index = n - (n % static_cast<uint64_t>(proc_num)); | |
| 84 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
|
2 | for (uint64_t i = tail_index; i < n; ++i) { |
| 85 | 1 | total_res += input.first[i] * input.second[i]; | |
| 86 | } | ||
| 87 | } | ||
| 88 | } | ||
| 89 | |||
| 90 | 6 | MPI_Bcast(&total_res, 1, MPI_DOUBLE, 0, MPI_COMM_WORLD); | |
| 91 | 6 | MPI_Barrier(MPI_COMM_WORLD); | |
| 92 | |||
| 93 | 6 | return true; | |
| 94 | } | ||
| 95 | |||
| 96 | 6 | bool LopatinAScalarMultMPI::PostProcessingImpl() { | |
| 97 | 6 | return true; | |
| 98 | } | ||
| 99 | |||
| 100 | } // namespace lopatin_a_scalar_mult | ||
| 101 |