| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include "spichek_d_dot_product_of_vectors/mpi/include/ops_mpi.hpp" | ||
| 2 | |||
| 3 | #include <mpi.h> | ||
| 4 | |||
| 5 | #include <algorithm> | ||
| 6 | #include <cstddef> | ||
| 7 | #include <cstdint> | ||
| 8 | #include <vector> | ||
| 9 | |||
| 10 | #include "spichek_d_dot_product_of_vectors/common/include/common.hpp" | ||
| 11 | |||
| 12 | namespace spichek_d_dot_product_of_vectors { | ||
| 13 | |||
| 14 |
1/2✓ Branch 1 taken 40 times.
✗ Branch 2 not taken.
|
40 | SpichekDDotProductOfVectorsMPI::SpichekDDotProductOfVectorsMPI(const InType &in) { |
| 15 | SetTypeOfTask(GetStaticTypeOfTask()); | ||
| 16 | GetInput() = in; | ||
| 17 | 40 | GetOutput() = 0; | |
| 18 | 40 | } | |
| 19 | |||
| 20 | 40 | bool SpichekDDotProductOfVectorsMPI::ValidationImpl() { | |
| 21 | const auto &[vector1, vector2] = GetInput(); | ||
| 22 | 40 | return vector1.size() == vector2.size(); | |
| 23 | } | ||
| 24 | |||
| 25 | 40 | bool SpichekDDotProductOfVectorsMPI::PreProcessingImpl() { | |
| 26 | 40 | GetOutput() = 0; | |
| 27 | 40 | return true; | |
| 28 | } | ||
| 29 | |||
| 30 | 40 | bool SpichekDDotProductOfVectorsMPI::RunImpl() { | |
| 31 | 40 | int rank = 0; | |
| 32 | 40 | int size = 0; | |
| 33 | 40 | MPI_Comm_rank(MPI_COMM_WORLD, &rank); | |
| 34 | 40 | MPI_Comm_size(MPI_COMM_WORLD, &size); | |
| 35 | |||
| 36 | const auto &[v1, v2] = GetInput(); | ||
| 37 | 40 | int n = 0; | |
| 38 |
2/2✓ Branch 0 taken 20 times.
✓ Branch 1 taken 20 times.
|
40 | if (rank == 0) { |
| 39 | 20 | n = static_cast<int>(v1.size()); | |
| 40 | } | ||
| 41 | 40 | MPI_Bcast(&n, 1, MPI_INT, 0, MPI_COMM_WORLD); | |
| 42 | |||
| 43 |
5/6✓ Branch 0 taken 38 times.
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 19 times.
✓ Branch 3 taken 19 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 19 times.
|
40 | if (n == 0 || (rank == 0 && static_cast<size_t>(n) != v2.size())) { |
| 44 | 2 | GetOutput() = 0; | |
| 45 | 2 | return true; | |
| 46 | } | ||
| 47 | |||
| 48 | 38 | std::vector<int> counts(size); | |
| 49 |
1/4✓ Branch 1 taken 38 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
|
38 | std::vector<int> displs(size); |
| 50 | |||
| 51 | 38 | int base = n / size; | |
| 52 | 38 | int rem = n % size; | |
| 53 | |||
| 54 |
2/2✓ Branch 0 taken 76 times.
✓ Branch 1 taken 38 times.
|
114 | for (int i = 0; i < size; ++i) { |
| 55 |
2/2✓ Branch 0 taken 58 times.
✓ Branch 1 taken 18 times.
|
134 | counts[i] = base + (i < rem ? 1 : 0); |
| 56 | // [Исправлено] Добавлены скобки для явного приоритета операций | ||
| 57 | 76 | displs[i] = (i * base) + std::min(i, rem); | |
| 58 | } | ||
| 59 | |||
| 60 |
1/2✓ Branch 1 taken 38 times.
✗ Branch 2 not taken.
|
38 | int local_count = counts[rank]; |
| 61 | |||
| 62 |
1/4✓ Branch 1 taken 38 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
|
38 | std::vector<int> lv1(local_count); |
| 63 |
3/6✓ Branch 1 taken 38 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 19 times.
✓ Branch 4 taken 19 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
|
38 | std::vector<int> lv2(local_count); |
| 64 | |||
| 65 |
3/4✓ Branch 0 taken 19 times.
✓ Branch 1 taken 19 times.
✓ Branch 3 taken 38 times.
✗ Branch 4 not taken.
|
57 | MPI_Scatterv(rank == 0 ? v1.data() : nullptr, counts.data(), displs.data(), MPI_INT, lv1.data(), local_count, MPI_INT, |
| 66 | 0, MPI_COMM_WORLD); | ||
| 67 |
3/4✓ Branch 0 taken 19 times.
✓ Branch 1 taken 19 times.
✓ Branch 3 taken 38 times.
✗ Branch 4 not taken.
|
57 | MPI_Scatterv(rank == 0 ? v2.data() : nullptr, counts.data(), displs.data(), MPI_INT, lv2.data(), local_count, MPI_INT, |
| 68 | 0, MPI_COMM_WORLD); | ||
| 69 | |||
| 70 | 38 | int64_t local_dot = 0; | |
| 71 |
2/2✓ Branch 0 taken 6547 times.
✓ Branch 1 taken 38 times.
|
6585 | for (int i = 0; i < local_count; ++i) { |
| 72 | 6547 | local_dot += static_cast<int64_t>(lv1[i]) * lv2[i]; | |
| 73 | } | ||
| 74 | |||
| 75 | 38 | int64_t global_dot = 0; | |
| 76 |
1/2✓ Branch 1 taken 38 times.
✗ Branch 2 not taken.
|
38 | MPI_Allreduce(&local_dot, &global_dot, 1, MPI_INT64_T, MPI_SUM, MPI_COMM_WORLD); |
| 77 | |||
| 78 |
2/2✓ Branch 0 taken 37 times.
✓ Branch 1 taken 1 times.
|
38 | GetOutput() = static_cast<OutType>(global_dot); |
| 79 | return true; | ||
| 80 | } | ||
| 81 | |||
| 82 | 40 | bool SpichekDDotProductOfVectorsMPI::PostProcessingImpl() { | |
| 83 | 40 | return true; | |
| 84 | } | ||
| 85 | |||
| 86 | } // namespace spichek_d_dot_product_of_vectors | ||
| 87 |