| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include "karpich_i_seidol_method/mpi/include/ops_mpi.hpp" | ||
| 2 | |||
| 3 | #include <mpi.h> | ||
| 4 | |||
| 5 | #include <algorithm> | ||
| 6 | #include <cmath> | ||
| 7 | #include <cstddef> | ||
| 8 | #include <vector> | ||
| 9 | |||
| 10 | #include "karpich_i_seidol_method/common/include/common.hpp" | ||
| 11 | |||
| 12 | namespace karpich_i_seidol_method { | ||
| 13 | |||
| 14 |
1/2✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
|
4 | KarpichISeidolMethodMPI::KarpichISeidolMethodMPI(const InType &in) { |
| 15 | SetTypeOfTask(GetStaticTypeOfTask()); | ||
| 16 | GetInput() = in; | ||
| 17 | GetOutput(); | ||
| 18 | 4 | } | |
| 19 | |||
| 20 | 4 | bool KarpichISeidolMethodMPI::ValidationImpl() { | |
| 21 | 4 | return true; | |
| 22 | } | ||
| 23 | |||
| 24 | 4 | bool KarpichISeidolMethodMPI::PreProcessingImpl() { | |
| 25 | 4 | return true; | |
| 26 | } | ||
| 27 | |||
| 28 | 4 | bool KarpichISeidolMethodMPI::RunImpl() { | |
| 29 | 4 | double eps = std::get<3>(GetInput()); | |
| 30 | |||
| 31 | 4 | int rank = 0; | |
| 32 | 4 | int mpi_size = 0; | |
| 33 | 4 | MPI_Comm_rank(MPI_COMM_WORLD, &rank); | |
| 34 | 4 | MPI_Comm_size(MPI_COMM_WORLD, &mpi_size); | |
| 35 | |||
| 36 | double *a = nullptr; | ||
| 37 | double *b = nullptr; | ||
| 38 | |||
| 39 | 4 | std::size_t n = 0; | |
| 40 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 2 times.
|
4 | if (rank == 0) { |
| 41 | 2 | n = std::get<0>(GetInput()); | |
| 42 | a = std::get<1>(GetInput()).data(); | ||
| 43 | b = std::get<2>(GetInput()).data(); | ||
| 44 | } | ||
| 45 | 4 | MPI_Bcast(&n, 1, MPI_UNSIGNED_LONG, 0, MPI_COMM_WORLD); | |
| 46 | |||
| 47 | 4 | int step = static_cast<int>(n) / mpi_size; | |
| 48 | 4 | int remainder = static_cast<int>(n) % mpi_size; | |
| 49 | |||
| 50 | 4 | std::vector<int> send_counts_a(mpi_size, static_cast<int>(step * n)); | |
| 51 |
1/4✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
|
4 | std::vector<int> send_counts_b(mpi_size, step); |
| 52 |
1/4✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
|
4 | std::vector<int> displ_a(mpi_size, 0); |
| 53 |
1/4✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
|
4 | std::vector<int> displ_b(mpi_size, 0); |
| 54 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
|
4 | for (int i = 0; i < remainder; ++i) { |
| 55 | ✗ | send_counts_b[i]++; | |
| 56 | ✗ | send_counts_a[i] += static_cast<int>(n); | |
| 57 | } | ||
| 58 |
2/2✓ Branch 0 taken 4 times.
✓ Branch 1 taken 4 times.
|
8 | for (int i = 1; i < mpi_size; ++i) { |
| 59 | 4 | displ_b[i] = displ_b[i - 1] + send_counts_b[i - 1]; | |
| 60 | 4 | displ_a[i] = displ_a[i - 1] + send_counts_a[i - 1]; | |
| 61 | } | ||
| 62 |
2/6✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 4 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
|
4 | std::vector<double> lb(send_counts_b[rank]); |
| 63 |
1/2✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
|
4 | MPI_Scatterv(b, send_counts_b.data(), displ_b.data(), MPI_DOUBLE, lb.data(), static_cast<int>(lb.size()), MPI_DOUBLE, |
| 64 | 0, MPI_COMM_WORLD); | ||
| 65 | |||
| 66 |
2/6✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 4 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
|
4 | std::vector<double> la(send_counts_a[rank]); |
| 67 |
1/2✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
|
4 | MPI_Scatterv(a, send_counts_a.data(), displ_a.data(), MPI_DOUBLE, la.data(), static_cast<int>(la.size()), MPI_DOUBLE, |
| 68 | 0, MPI_COMM_WORLD); | ||
| 69 | |||
| 70 |
1/4✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
|
4 | std::vector<double> x(n, 0.0); |
| 71 |
1/4✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
|
4 | std::vector<double> x_new(lb.size(), 0.0); |
| 72 |
1/4✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
|
4 | std::vector<double> epsilons(n, 0.0); |
| 73 |
1/4✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
|
4 | std::vector<double> epsilons_new(lb.size(), 0.0); |
| 74 | bool iter_continue = true; | ||
| 75 |
2/2✓ Branch 0 taken 56 times.
✓ Branch 1 taken 4 times.
|
60 | while (iter_continue) { |
| 76 |
2/2✓ Branch 0 taken 282 times.
✓ Branch 1 taken 56 times.
|
338 | for (std::size_t i = 0; i < lb.size(); i++) { |
| 77 | 282 | std::size_t row = displ_b[rank] + i; | |
| 78 | 282 | double ix = lb[i]; | |
| 79 |
2/2✓ Branch 0 taken 1613 times.
✓ Branch 1 taken 282 times.
|
1895 | for (std::size_t j = 0; j < row; j++) { |
| 80 | 1613 | ix = ix - (la[(i * n) + j] * x[j]); | |
| 81 | } | ||
| 82 |
2/2✓ Branch 0 taken 1613 times.
✓ Branch 1 taken 282 times.
|
1895 | for (std::size_t j = row + 1; j < n; j++) { |
| 83 | 1613 | ix = ix - (la[(i * n) + j] * x[j]); | |
| 84 | } | ||
| 85 | |||
| 86 | 282 | ix = ix / la[(i * n) + row]; | |
| 87 | 282 | epsilons[row] = std::fabs(ix - x[row]); | |
| 88 | 282 | epsilons_new[i] = epsilons[row]; | |
| 89 | 282 | x[row] = ix; | |
| 90 | 282 | x_new[i] = ix; | |
| 91 | } | ||
| 92 |
1/2✓ Branch 1 taken 56 times.
✗ Branch 2 not taken.
|
56 | MPI_Allgatherv(x_new.data(), static_cast<int>(x_new.size()), MPI_DOUBLE, x.data(), send_counts_b.data(), |
| 93 | displ_b.data(), MPI_DOUBLE, MPI_COMM_WORLD); | ||
| 94 | |||
| 95 |
1/2✓ Branch 1 taken 56 times.
✗ Branch 2 not taken.
|
56 | MPI_Allgatherv(epsilons_new.data(), static_cast<int>(epsilons_new.size()), MPI_DOUBLE, epsilons.data(), |
| 96 | send_counts_b.data(), displ_b.data(), MPI_DOUBLE, MPI_COMM_WORLD); | ||
| 97 | |||
| 98 | iter_continue = IterContinue(epsilons, eps); | ||
| 99 | } | ||
| 100 | |||
| 101 |
1/2✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
|
4 | GetOutput() = x; |
| 102 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 2 times.
|
4 | if (rank != 0) { |
| 103 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
|
2 | delete[] a; |
| 104 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
|
2 | delete[] b; |
| 105 | } | ||
| 106 | 4 | return true; | |
| 107 | } | ||
| 108 | |||
| 109 | 4 | bool KarpichISeidolMethodMPI::PostProcessingImpl() { | |
| 110 | 4 | return true; | |
| 111 | } | ||
| 112 | |||
| 113 | ✗ | bool KarpichISeidolMethodMPI::IterContinue(std::vector<double> &iter_eps, double correct_eps) { | |
| 114 | 56 | double max_in_iter = *std::ranges::max_element(iter_eps); | |
| 115 | 56 | return max_in_iter > correct_eps; | |
| 116 | } | ||
| 117 | |||
| 118 | } // namespace karpich_i_seidol_method | ||
| 119 |