| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include "telnov_strongin_algorithm/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 "telnov_strongin_algorithm/common/include/common.hpp" | ||
| 11 | |||
| 12 | namespace telnov_strongin_algorithm { | ||
| 13 | |||
| 14 | struct MaxData { | ||
| 15 | double value{}; | ||
| 16 | int index{}; | ||
| 17 | }; | ||
| 18 | |||
| 19 | 6 | TelnovStronginAlgorithmMPI::TelnovStronginAlgorithmMPI(const InType &in) { | |
| 20 | SetTypeOfTask(GetStaticTypeOfTask()); | ||
| 21 | 6 | GetInput() = in; | |
| 22 | GetOutput() = 0; | ||
| 23 | 6 | } | |
| 24 | |||
| 25 | 6 | bool TelnovStronginAlgorithmMPI::ValidationImpl() { | |
| 26 | const auto &in = GetInput(); | ||
| 27 |
2/4✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 6 times.
|
6 | return (in.eps > 0.0) && (in.b > in.a); |
| 28 | } | ||
| 29 | |||
| 30 | 6 | bool TelnovStronginAlgorithmMPI::PreProcessingImpl() { | |
| 31 | 6 | return true; | |
| 32 | } | ||
| 33 | |||
| 34 | 6 | bool TelnovStronginAlgorithmMPI::RunImpl() { | |
| 35 | 6 | int rank = 0; | |
| 36 | 6 | int size = 1; | |
| 37 | 6 | MPI_Comm_rank(MPI_COMM_WORLD, &rank); | |
| 38 | 6 | MPI_Comm_size(MPI_COMM_WORLD, &size); | |
| 39 | |||
| 40 | const auto &in = GetInput(); | ||
| 41 | 6 | const double eps = in.eps; | |
| 42 | |||
| 43 | 306 | auto f = [](double x) { return ((x - 1.0) * (x - 1.0)) + 1.0; }; | |
| 44 | |||
| 45 | 6 | std::vector<double> x_vals{in.a, in.b}; | |
| 46 |
1/4✓ Branch 1 taken 6 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
|
6 | std::vector<double> f_vals{f(in.a), f(in.b)}; |
| 47 | |||
| 48 | const double r = 2.0; | ||
| 49 | const int k_max_iters = 100; | ||
| 50 | int iter = 0; | ||
| 51 | |||
| 52 |
3/4✓ Branch 0 taken 606 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 600 times.
✓ Branch 3 taken 6 times.
|
606 | while ((x_vals.back() - x_vals.front()) > eps && iter < k_max_iters) { |
| 53 | 600 | ++iter; | |
| 54 | |||
| 55 | 600 | double m = 0.0; | |
| 56 |
2/2✓ Branch 0 taken 30300 times.
✓ Branch 1 taken 600 times.
|
30900 | for (std::size_t i = 1; i < x_vals.size(); ++i) { |
| 57 | 30300 | const double dx = x_vals[i] - x_vals[i - 1]; | |
| 58 | 30300 | const double df = std::abs(f_vals[i] - f_vals[i - 1]); | |
| 59 | 30300 | m = std::max(m, df / dx); | |
| 60 | } | ||
| 61 | 600 | m = std::max(m, 1.0); | |
| 62 | |||
| 63 | MaxData local_data; | ||
| 64 | 600 | local_data.value = -1e9; | |
| 65 | 600 | local_data.index = 1; | |
| 66 | |||
| 67 |
2/2✓ Branch 0 taken 15150 times.
✓ Branch 1 taken 600 times.
|
15750 | for (std::size_t i = static_cast<std::size_t>(rank) + 1; i < x_vals.size(); i += static_cast<std::size_t>(size)) { |
| 68 | 15150 | const double dx = x_vals[i] - x_vals[i - 1]; | |
| 69 | 15150 | const double df = f_vals[i] - f_vals[i - 1]; | |
| 70 | |||
| 71 | 15150 | const double r_val = (r * dx) + ((df * df) / (r * dx)) - (2.0 * (f_vals[i] + f_vals[i - 1])); | |
| 72 | |||
| 73 |
2/2✓ Branch 0 taken 300 times.
✓ Branch 1 taken 14850 times.
|
15150 | if (r_val > local_data.value) { |
| 74 | 300 | local_data.value = r_val; | |
| 75 | 300 | local_data.index = static_cast<int>(i); | |
| 76 | } | ||
| 77 | } | ||
| 78 | |||
| 79 | 600 | MaxData global_data{}; | |
| 80 |
1/2✓ Branch 1 taken 600 times.
✗ Branch 2 not taken.
|
600 | MPI_Allreduce(&local_data, &global_data, 1, MPI_DOUBLE_INT, MPI_MAXLOC, MPI_COMM_WORLD); |
| 81 | |||
| 82 |
2/2✓ Branch 0 taken 300 times.
✓ Branch 1 taken 300 times.
|
600 | if (rank != 0) { |
| 83 | 300 | int n = 0; | |
| 84 |
1/2✓ Branch 1 taken 300 times.
✗ Branch 2 not taken.
|
300 | MPI_Bcast(&n, 1, MPI_INT, 0, MPI_COMM_WORLD); |
| 85 |
1/2✓ Branch 1 taken 300 times.
✗ Branch 2 not taken.
|
300 | x_vals.resize(static_cast<std::size_t>(n)); |
| 86 |
1/2✓ Branch 1 taken 300 times.
✗ Branch 2 not taken.
|
300 | f_vals.resize(static_cast<std::size_t>(n)); |
| 87 |
1/2✓ Branch 1 taken 300 times.
✗ Branch 2 not taken.
|
300 | MPI_Bcast(x_vals.data(), n, MPI_DOUBLE, 0, MPI_COMM_WORLD); |
| 88 |
1/2✓ Branch 1 taken 300 times.
✗ Branch 2 not taken.
|
300 | MPI_Bcast(f_vals.data(), n, MPI_DOUBLE, 0, MPI_COMM_WORLD); |
| 89 | continue; | ||
| 90 | 300 | } | |
| 91 | |||
| 92 | 300 | const int idx = global_data.index; | |
| 93 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 300 times.
|
300 | const double mid = 0.5 * (x_vals[idx] + x_vals[idx - 1]); |
| 94 | |||
| 95 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 300 times.
|
300 | double new_x = mid - ((f_vals[idx] - f_vals[idx - 1]) / (2.0 * m)); |
| 96 | |||
| 97 |
1/2✓ Branch 1 taken 300 times.
✗ Branch 2 not taken.
|
300 | new_x = std::clamp(new_x, x_vals[idx - 1], x_vals[idx]); |
| 98 | |||
| 99 |
1/2✓ Branch 1 taken 300 times.
✗ Branch 2 not taken.
|
300 | x_vals.insert(x_vals.begin() + idx, new_x); |
| 100 |
2/6✓ Branch 1 taken 300 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 300 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
|
300 | f_vals.insert(f_vals.begin() + idx, f(new_x)); |
| 101 | |||
| 102 | 300 | int n = static_cast<int>(x_vals.size()); | |
| 103 |
1/2✓ Branch 1 taken 300 times.
✗ Branch 2 not taken.
|
300 | MPI_Bcast(&n, 1, MPI_INT, 0, MPI_COMM_WORLD); |
| 104 |
1/2✓ Branch 1 taken 300 times.
✗ Branch 2 not taken.
|
300 | MPI_Bcast(x_vals.data(), n, MPI_DOUBLE, 0, MPI_COMM_WORLD); |
| 105 |
1/2✓ Branch 1 taken 300 times.
✗ Branch 2 not taken.
|
300 | MPI_Bcast(f_vals.data(), n, MPI_DOUBLE, 0, MPI_COMM_WORLD); |
| 106 | } | ||
| 107 | |||
| 108 |
1/2✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
|
6 | GetOutput() = *std::ranges::min_element(f_vals); |
| 109 | 6 | return true; | |
| 110 | } | ||
| 111 | |||
| 112 | 6 | bool TelnovStronginAlgorithmMPI::PostProcessingImpl() { | |
| 113 | 6 | return true; | |
| 114 | } | ||
| 115 | |||
| 116 | } // namespace telnov_strongin_algorithm | ||
| 117 |