| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include "vasiliev_m_vec_signs/mpi/include/ops_mpi.hpp" | ||
| 2 | |||
| 3 | #include <mpi.h> | ||
| 4 | |||
| 5 | #include <cstddef> | ||
| 6 | #include <vector> | ||
| 7 | |||
| 8 | #include "vasiliev_m_vec_signs/common/include/common.hpp" | ||
| 9 | |||
| 10 | namespace vasiliev_m_vec_signs { | ||
| 11 | |||
| 12 |
1/2✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
|
10 | VasilievMVecSignsMPI::VasilievMVecSignsMPI(const InType &in) { |
| 13 | SetTypeOfTask(GetStaticTypeOfTask()); | ||
| 14 |
1/2✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
|
10 | GetInput() = in; |
| 15 | 10 | GetOutput() = OutType{}; | |
| 16 | 10 | } | |
| 17 | |||
| 18 | 10 | bool VasilievMVecSignsMPI::ValidationImpl() { | |
| 19 | 10 | return !GetInput().empty(); | |
| 20 | } | ||
| 21 | |||
| 22 | 10 | bool VasilievMVecSignsMPI::PreProcessingImpl() { | |
| 23 | 10 | GetOutput() = 0; | |
| 24 | 10 | return true; | |
| 25 | } | ||
| 26 | |||
| 27 | 10 | bool VasilievMVecSignsMPI::RunImpl() { | |
| 28 | 10 | int rank = 0; | |
| 29 | 10 | int size = 1; | |
| 30 | 10 | MPI_Comm_rank(MPI_COMM_WORLD, &rank); | |
| 31 | 10 | MPI_Comm_size(MPI_COMM_WORLD, &size); | |
| 32 | |||
| 33 | auto &vec = GetInput(); | ||
| 34 | 10 | int n = static_cast<int>(vec.size()); | |
| 35 | |||
| 36 | 10 | std::vector<int> counts(size); | |
| 37 |
1/4✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
|
10 | std::vector<int> displs(size); |
| 38 | |||
| 39 |
2/2✓ Branch 0 taken 5 times.
✓ Branch 1 taken 5 times.
|
10 | if (rank == 0) { |
| 40 | 5 | CalcCountsAndDispls(n, size, counts, displs); | |
| 41 | } | ||
| 42 | |||
| 43 |
1/2✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
|
10 | MPI_Bcast(counts.data(), size, MPI_INT, 0, MPI_COMM_WORLD); |
| 44 |
1/2✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
|
10 | MPI_Bcast(displs.data(), size, MPI_INT, 0, MPI_COMM_WORLD); |
| 45 | |||
| 46 |
1/4✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
|
10 | std::vector<int> local_data(counts[rank]); |
| 47 |
1/2✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
|
10 | MPI_Scatterv(vec.data(), counts.data(), displs.data(), MPI_INT, local_data.data(), counts[rank], MPI_INT, 0, |
| 48 | MPI_COMM_WORLD); | ||
| 49 | |||
| 50 |
1/2✓ Branch 0 taken 10 times.
✗ Branch 1 not taken.
|
10 | int local_count = 0; |
| 51 | |||
| 52 |
1/2✓ Branch 0 taken 10 times.
✗ Branch 1 not taken.
|
10 | if (!local_data.empty()) { |
| 53 |
2/2✓ Branch 0 taken 112 times.
✓ Branch 1 taken 10 times.
|
122 | for (size_t i = 0; i < local_data.size() - 1; i++) { |
| 54 |
2/2✓ Branch 0 taken 87 times.
✓ Branch 1 taken 25 times.
|
112 | if (SignChangeCheck(local_data[i], local_data[i + 1])) { |
| 55 | 47 | local_count++; | |
| 56 | } | ||
| 57 | } | ||
| 58 | } | ||
| 59 | |||
| 60 |
1/2✓ Branch 0 taken 10 times.
✗ Branch 1 not taken.
|
10 | int first_elem = local_data.empty() ? 0 : local_data.front(); |
| 61 |
1/2✓ Branch 0 taken 10 times.
✗ Branch 1 not taken.
|
10 | int last_elem = local_data.empty() ? 0 : local_data.back(); |
| 62 | |||
| 63 | 10 | int prev_last = 0; | |
| 64 |
2/2✓ Branch 0 taken 5 times.
✓ Branch 1 taken 5 times.
|
10 | if (rank > 0) { |
| 65 |
1/2✓ Branch 1 taken 5 times.
✗ Branch 2 not taken.
|
5 | MPI_Recv(&prev_last, 1, MPI_INT, rank - 1, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE); |
| 66 |
1/2✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
|
5 | if (!local_data.empty() && SignChangeCheck(prev_last, first_elem)) { |
| 67 | 4 | local_count++; | |
| 68 | } | ||
| 69 | } | ||
| 70 | |||
| 71 |
2/2✓ Branch 0 taken 5 times.
✓ Branch 1 taken 5 times.
|
10 | if (rank < size - 1) { |
| 72 |
1/2✓ Branch 1 taken 5 times.
✗ Branch 2 not taken.
|
5 | MPI_Send(&last_elem, 1, MPI_INT, rank + 1, 0, MPI_COMM_WORLD); |
| 73 | } | ||
| 74 | |||
| 75 | 10 | int global_count = 0; | |
| 76 |
1/2✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
|
10 | MPI_Allreduce(&local_count, &global_count, 1, MPI_INT, MPI_SUM, MPI_COMM_WORLD); |
| 77 | |||
| 78 |
1/2✓ Branch 0 taken 10 times.
✗ Branch 1 not taken.
|
10 | GetOutput() = global_count; |
| 79 | |||
| 80 | 10 | return true; | |
| 81 | } | ||
| 82 | |||
| 83 | 5 | void VasilievMVecSignsMPI::CalcCountsAndDispls(int n, int size, std::vector<int> &counts, std::vector<int> &displs) { | |
| 84 | 5 | int chunk = n / size; | |
| 85 | 5 | int remain = n % size; | |
| 86 | |||
| 87 |
2/2✓ Branch 0 taken 10 times.
✓ Branch 1 taken 5 times.
|
15 | for (int i = 0; i < size; i++) { |
| 88 |
2/2✓ Branch 0 taken 8 times.
✓ Branch 1 taken 2 times.
|
18 | counts[i] = chunk + (i < remain ? 1 : 0); |
| 89 | } | ||
| 90 | |||
| 91 | 5 | displs[0] = 0; | |
| 92 |
2/2✓ Branch 0 taken 5 times.
✓ Branch 1 taken 5 times.
|
10 | for (int i = 1; i < size; i++) { |
| 93 | 5 | displs[i] = displs[i - 1] + counts[i - 1]; | |
| 94 | } | ||
| 95 | 5 | } | |
| 96 | |||
| 97 | ✗ | bool VasilievMVecSignsMPI::SignChangeCheck(int a, int b) { | |
| 98 |
8/10✗ Branch 0 not taken.
✗ Branch 1 not taken.
✓ Branch 2 taken 87 times.
✓ Branch 3 taken 25 times.
✓ Branch 4 taken 22 times.
✓ Branch 5 taken 65 times.
✓ Branch 6 taken 4 times.
✓ Branch 7 taken 1 times.
✓ Branch 8 taken 3 times.
✓ Branch 9 taken 1 times.
|
117 | return (a > 0 && b < 0) || (a < 0 && b > 0); |
| 99 | } | ||
| 100 | |||
| 101 | 10 | bool VasilievMVecSignsMPI::PostProcessingImpl() { | |
| 102 | 10 | return GetOutput() >= 0; | |
| 103 | } | ||
| 104 | |||
| 105 | } // namespace vasiliev_m_vec_signs | ||
| 106 |