| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include "zavyalov_a_scalar_product/mpi/include/ops_mpi.hpp" | ||
| 2 | |||
| 3 | #include <mpi.h> | ||
| 4 | |||
| 5 | #include <vector> | ||
| 6 | |||
| 7 | #include "zavyalov_a_scalar_product/common/include/common.hpp" | ||
| 8 | |||
| 9 | namespace zavyalov_a_scalar_product { | ||
| 10 | |||
| 11 |
1/2✓ Branch 1 taken 20 times.
✗ Branch 2 not taken.
|
20 | ZavyalovAScalarProductMPI::ZavyalovAScalarProductMPI(const InType &in) { |
| 12 | SetTypeOfTask(GetStaticTypeOfTask()); | ||
| 13 | 20 | int rank = 0; | |
| 14 |
1/2✓ Branch 1 taken 20 times.
✗ Branch 2 not taken.
|
20 | MPI_Comm_rank(MPI_COMM_WORLD, &rank); |
| 15 |
2/2✓ Branch 0 taken 10 times.
✓ Branch 1 taken 10 times.
|
20 | if (rank == 0) { |
| 16 | GetInput() = in; | ||
| 17 | } | ||
| 18 | 20 | GetOutput() = 0.0; | |
| 19 | 20 | } | |
| 20 | |||
| 21 | 20 | bool ZavyalovAScalarProductMPI::ValidationImpl() { | |
| 22 | 20 | int rank = 0; | |
| 23 | 20 | MPI_Comm_rank(MPI_COMM_WORLD, &rank); | |
| 24 |
2/2✓ Branch 0 taken 10 times.
✓ Branch 1 taken 10 times.
|
20 | if (rank != 0) { |
| 25 | return true; | ||
| 26 | } | ||
| 27 |
2/4✓ Branch 0 taken 10 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 10 times.
✗ Branch 3 not taken.
|
10 | return (!std::get<0>(GetInput()).empty()) && (std::get<0>(GetInput()).size() == std::get<1>(GetInput()).size()); |
| 28 | } | ||
| 29 | |||
| 30 | 20 | bool ZavyalovAScalarProductMPI::PreProcessingImpl() { | |
| 31 | 20 | return true; | |
| 32 | } | ||
| 33 | 20 | bool ZavyalovAScalarProductMPI::RunImpl() { | |
| 34 | const double *left_data = nullptr; | ||
| 35 | const double *right_data = nullptr; | ||
| 36 | |||
| 37 | 20 | int world_size = 0; | |
| 38 | 20 | MPI_Comm_size(MPI_COMM_WORLD, &world_size); | |
| 39 | 20 | int rank = 0; | |
| 40 | 20 | MPI_Comm_rank(MPI_COMM_WORLD, &rank); | |
| 41 | |||
| 42 | 20 | int vector_size = 0; | |
| 43 | |||
| 44 |
2/2✓ Branch 0 taken 10 times.
✓ Branch 1 taken 10 times.
|
20 | if (rank == 0) { |
| 45 |
1/2✓ Branch 0 taken 10 times.
✗ Branch 1 not taken.
|
10 | GetOutput() = 0.0; |
| 46 | const auto &input = GetInput(); | ||
| 47 |
1/2✓ Branch 0 taken 10 times.
✗ Branch 1 not taken.
|
10 | if (!std::get<0>(input).empty()) { // it does not compile in ubuntu without this line |
| 48 | left_data = std::get<0>(input).data(); | ||
| 49 | } | ||
| 50 |
1/2✓ Branch 0 taken 10 times.
✗ Branch 1 not taken.
|
10 | if (!std::get<1>(input).empty()) { // it does not compile in ubuntu without this line |
| 51 | right_data = std::get<1>(input).data(); | ||
| 52 | } | ||
| 53 | 10 | vector_size = static_cast<int>(std::get<0>(input).size()); | |
| 54 | } | ||
| 55 | |||
| 56 | 20 | MPI_Bcast(&vector_size, 1, MPI_INT, 0, MPI_COMM_WORLD); | |
| 57 | |||
| 58 | 20 | std::vector<int> sendcounts(world_size); | |
| 59 |
1/4✓ Branch 1 taken 20 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
|
20 | std::vector<int> displs(world_size); |
| 60 | |||
| 61 | 20 | int blocksize = vector_size / world_size; | |
| 62 | int elements_left = vector_size - (world_size * blocksize); | ||
| 63 | int elements_processed = 0; | ||
| 64 | |||
| 65 |
2/2✓ Branch 0 taken 40 times.
✓ Branch 1 taken 20 times.
|
60 | for (int i = 0; i < world_size; i++) { |
| 66 |
2/2✓ Branch 0 taken 30 times.
✓ Branch 1 taken 10 times.
|
70 | sendcounts[i] = blocksize + (i < elements_left ? 1 : 0); |
| 67 | 40 | displs[i] = elements_processed; | |
| 68 | 40 | elements_processed += sendcounts[i]; | |
| 69 | } | ||
| 70 | |||
| 71 |
1/2✓ Branch 1 taken 20 times.
✗ Branch 2 not taken.
|
20 | int elements_count = sendcounts[rank]; |
| 72 |
1/4✓ Branch 1 taken 20 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
|
20 | std::vector<double> local_left(elements_count); |
| 73 |
2/6✓ Branch 1 taken 20 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 20 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
|
20 | std::vector<double> local_right(elements_count); |
| 74 | |||
| 75 |
1/2✓ Branch 1 taken 20 times.
✗ Branch 2 not taken.
|
20 | MPI_Scatterv(left_data, sendcounts.data(), displs.data(), MPI_DOUBLE, local_left.data(), elements_count, MPI_DOUBLE, |
| 76 | 0, MPI_COMM_WORLD); | ||
| 77 |
1/2✓ Branch 1 taken 20 times.
✗ Branch 2 not taken.
|
20 | MPI_Scatterv(right_data, sendcounts.data(), displs.data(), MPI_DOUBLE, local_right.data(), elements_count, MPI_DOUBLE, |
| 78 | 0, MPI_COMM_WORLD); | ||
| 79 | |||
| 80 | 20 | double cur_res = 0.0; | |
| 81 |
2/2✓ Branch 0 taken 55 times.
✓ Branch 1 taken 20 times.
|
75 | for (int i = 0; i < elements_count; i++) { |
| 82 | 55 | cur_res += local_left[i] * local_right[i]; | |
| 83 | } | ||
| 84 | |||
| 85 | 20 | double glob_res = 0.0; | |
| 86 |
1/2✓ Branch 1 taken 20 times.
✗ Branch 2 not taken.
|
20 | MPI_Allreduce(&cur_res, &glob_res, 1, MPI_DOUBLE, MPI_SUM, MPI_COMM_WORLD); |
| 87 | |||
| 88 |
2/2✓ Branch 0 taken 19 times.
✓ Branch 1 taken 1 times.
|
20 | GetOutput() = glob_res; |
| 89 | |||
| 90 | 20 | return true; | |
| 91 | } | ||
| 92 | |||
| 93 | 20 | bool ZavyalovAScalarProductMPI::PostProcessingImpl() { | |
| 94 | 20 | return true; | |
| 95 | } | ||
| 96 | |||
| 97 | } // namespace zavyalov_a_scalar_product | ||
| 98 |