| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include "khruev_a_global_opt/mpi/include/ops_mpi.hpp" | ||
| 2 | |||
| 3 | #include <mpi.h> | ||
| 4 | |||
| 5 | #include <algorithm> | ||
| 6 | #include <cmath> | ||
| 7 | #include <cstddef> | ||
| 8 | #include <functional> | ||
| 9 | #include <utility> | ||
| 10 | #include <vector> | ||
| 11 | |||
| 12 | #include "khruev_a_global_opt/common/include/common.hpp" | ||
| 13 | |||
| 14 | namespace khruev_a_global_opt { | ||
| 15 | |||
| 16 | 8 | KhruevAGlobalOptMPI::KhruevAGlobalOptMPI(const InType &in) { | |
| 17 | SetTypeOfTask(GetStaticTypeOfTask()); | ||
| 18 | 8 | GetInput() = in; | |
| 19 | 8 | } | |
| 20 | |||
| 21 | 8 | bool KhruevAGlobalOptMPI::ValidationImpl() { | |
| 22 |
3/6✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 8 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 8 times.
|
8 | return GetInput().max_iter > 0 && GetInput().epsilon > 0 && GetInput().r > 1.0; |
| 23 | } | ||
| 24 | |||
| 25 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 8 times.
|
8 | bool KhruevAGlobalOptMPI::PreProcessingImpl() { |
| 26 | trials_.clear(); | ||
| 27 | 8 | return true; | |
| 28 | } | ||
| 29 | |||
| 30 | 412 | double KhruevAGlobalOptMPI::CalculateFunction(double t) { | |
| 31 | 412 | double u = 0; | |
| 32 | 412 | double v = 0; | |
| 33 | 412 | D2xy(t, u, v); | |
| 34 | 412 | double real_x = GetInput().ax + (u * (GetInput().bx - GetInput().ax)); | |
| 35 | 412 | double real_y = GetInput().ay + (v * (GetInput().by - GetInput().ay)); | |
| 36 | 412 | return TargetFunction(GetInput().func_id, real_x, real_y); | |
| 37 | } | ||
| 38 | |||
| 39 | ✗ | void KhruevAGlobalOptMPI::AddTrialUnsorted(double t, double z) { | |
| 40 | 808 | Trial tr{}; | |
| 41 | 808 | tr.x = t; | |
| 42 | 808 | tr.z = z; | |
| 43 | 792 | trials_.push_back(tr); | |
| 44 | 8 | } | |
| 45 | |||
| 46 | 400 | double KhruevAGlobalOptMPI::ComputeM() { | |
| 47 | 400 | double max_slope = 0.0; | |
| 48 |
2/2✓ Branch 0 taken 19608 times.
✓ Branch 1 taken 400 times.
|
20008 | for (size_t i = 1; i < trials_.size(); ++i) { |
| 49 | 19608 | double dx = trials_[i].x - trials_[i - 1].x; | |
| 50 |
1/2✓ Branch 0 taken 19608 times.
✗ Branch 1 not taken.
|
19608 | double dz = std::abs(trials_[i].z - trials_[i - 1].z); |
| 51 |
1/2✓ Branch 0 taken 19608 times.
✗ Branch 1 not taken.
|
19608 | if (dx > 1e-12) { |
| 52 | 19608 | max_slope = std::max(max_slope, dz / dx); | |
| 53 | } | ||
| 54 | } | ||
| 55 |
1/2✓ Branch 0 taken 400 times.
✗ Branch 1 not taken.
|
400 | return (max_slope > 0) ? GetInput().r * max_slope : 1.0; |
| 56 | } | ||
| 57 | |||
| 58 | 400 | std::vector<KhruevAGlobalOptMPI::IntervalInfo> KhruevAGlobalOptMPI::ComputeIntervals(double m) const { | |
| 59 |
1/2✓ Branch 1 taken 400 times.
✗ Branch 2 not taken.
|
400 | std::vector<IntervalInfo> intervals; |
| 60 |
1/2✓ Branch 1 taken 400 times.
✗ Branch 2 not taken.
|
400 | intervals.reserve(trials_.size() - 1); |
| 61 |
2/2✓ Branch 0 taken 19608 times.
✓ Branch 1 taken 400 times.
|
20008 | for (size_t i = 1; i < trials_.size(); ++i) { |
| 62 | 19608 | double dx = trials_[i].x - trials_[i - 1].x; | |
| 63 | 19608 | double z_r = trials_[i].z; | |
| 64 | 19608 | double z_l = trials_[i - 1].z; | |
| 65 | |||
| 66 | // Классическая формула Стронгина | ||
| 67 | 19608 | double r = (m * dx) + (((z_r - z_l) * (z_r - z_l)) / (m * dx)) - (2.0 * (z_r + z_l)); | |
| 68 |
1/4✓ Branch 1 taken 19608 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
|
19608 | intervals.push_back({r, static_cast<int>(i)}); |
| 69 | } | ||
| 70 | 400 | return intervals; | |
| 71 | } | ||
| 72 | |||
| 73 | ✗ | bool KhruevAGlobalOptMPI::LocalShouldStop(const std::vector<IntervalInfo> &intervals, int num_to_check) { | |
| 74 | bool local_stop = true; | ||
| 75 |
1/4✓ Branch 0 taken 400 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
400 | for (int i = 0; i < num_to_check; ++i) { |
| 76 |
1/4✗ Branch 0 not taken.
✓ Branch 1 taken 400 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
400 | int idx = intervals[i].index; |
| 77 |
1/4✗ Branch 0 not taken.
✓ Branch 1 taken 400 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
400 | if (trials_[idx].x - trials_[idx - 1].x > GetInput().epsilon) { |
| 78 | local_stop = false; | ||
| 79 | break; | ||
| 80 | } | ||
| 81 | } | ||
| 82 | ✗ | return local_stop; | |
| 83 | } | ||
| 84 | |||
| 85 | 396 | double KhruevAGlobalOptMPI::GenerateNewX(int idx, double m) const { | |
| 86 |
1/2✓ Branch 0 taken 396 times.
✗ Branch 1 not taken.
|
396 | double x_r = trials_[idx].x; |
| 87 | 396 | double x_l = trials_[idx - 1].x; | |
| 88 | 396 | double z_r = trials_[idx].z; | |
| 89 | 396 | double z_l = trials_[idx - 1].z; | |
| 90 | |||
| 91 | // Формула вычисления новой точки испытания | ||
| 92 | 396 | double new_x = (0.5 * (x_r + x_l)) - ((z_r - z_l) / (2.0 * m)); | |
| 93 | |||
| 94 | // Защита от выхода за границы интервала | ||
| 95 |
2/4✓ Branch 0 taken 396 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 396 times.
|
396 | if (new_x <= x_l || new_x >= x_r) { |
| 96 | new_x = 0.5 * (x_r + x_l); | ||
| 97 | } | ||
| 98 | 396 | return new_x; | |
| 99 | } | ||
| 100 | |||
| 101 | 400 | void KhruevAGlobalOptMPI::CollectAndAddPoints(const std::vector<Point> &global_res, int &k) { | |
| 102 |
2/2✓ Branch 0 taken 800 times.
✓ Branch 1 taken 400 times.
|
1200 | for (auto global_re : global_res) { |
| 103 |
2/2✓ Branch 0 taken 792 times.
✓ Branch 1 taken 8 times.
|
800 | if (global_re.x >= 0.0) { |
| 104 | AddTrialUnsorted(global_re.x, global_re.z); | ||
| 105 | 792 | k++; | |
| 106 | } | ||
| 107 | } | ||
| 108 | 400 | } | |
| 109 | |||
| 110 | 8 | bool KhruevAGlobalOptMPI::RunImpl() { | |
| 111 | 8 | int rank = 0; | |
| 112 | 8 | int size = 0; | |
| 113 | 8 | MPI_Comm_rank(MPI_COMM_WORLD, &rank); | |
| 114 | 8 | MPI_Comm_size(MPI_COMM_WORLD, &size); | |
| 115 | |||
| 116 | // Начальные испытания | ||
| 117 |
1/2✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
|
8 | if (trials_.empty()) { |
| 118 | 8 | AddTrialUnsorted(0.0, CalculateFunction(0.0)); | |
| 119 | 8 | AddTrialUnsorted(1.0, CalculateFunction(1.0)); | |
| 120 | } | ||
| 121 |
2/22✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
✗ Branch 14 not taken.
✗ Branch 15 not taken.
✗ Branch 16 not taken.
✓ Branch 17 taken 8 times.
✗ Branch 18 not taken.
✓ Branch 19 taken 8 times.
✗ Branch 20 not taken.
✗ Branch 21 not taken.
|
16 | std::ranges::sort(trials_, [](const Trial &a, const Trial &b) { return a.x < b.x; }); |
| 122 | |||
| 123 | 8 | int k = 2; // Уже провели 2 испытания | |
| 124 | |||
| 125 |
2/2✓ Branch 0 taken 400 times.
✓ Branch 1 taken 8 times.
|
408 | while (k < GetInput().max_iter) { |
| 126 | // 1. Вычисляем M (одинаково на всех процессах) | ||
| 127 | 400 | double m = ComputeM(); | |
| 128 | |||
| 129 | // 2. Вычисляем характеристики R | ||
| 130 | 400 | auto intervals = ComputeIntervals(m); | |
| 131 | |||
| 132 | // 3. Сортируем интервалы по убыванию характеристики | ||
| 133 | std::ranges::sort(intervals, std::greater<>()); | ||
| 134 | |||
| 135 | // 4. Проверка критерия остановки (по самому широкому из лучших интервалов) | ||
| 136 | 400 | int num_to_check = std::min(static_cast<int>(intervals.size()), size); | |
| 137 | bool local_stop = LocalShouldStop(intervals, num_to_check); | ||
| 138 | |||
| 139 | // Синхронная остановка | ||
| 140 | 400 | int stop_signal = 0; | |
| 141 |
1/2✓ Branch 0 taken 400 times.
✗ Branch 1 not taken.
|
400 | int local_stop_signal = local_stop ? 1 : 0; |
| 142 |
1/2✓ Branch 1 taken 400 times.
✗ Branch 2 not taken.
|
400 | MPI_Allreduce(&local_stop_signal, &stop_signal, 1, MPI_INT, MPI_LAND, MPI_COMM_WORLD); |
| 143 |
1/2✓ Branch 0 taken 400 times.
✗ Branch 1 not taken.
|
400 | if (stop_signal != 0) { |
| 144 | break; | ||
| 145 | } | ||
| 146 | |||
| 147 | // 5. Генерация новых точек | ||
| 148 | 400 | Point my_point{.x = -1.0, .z = 0.0}; | |
| 149 | |||
| 150 |
2/2✓ Branch 0 taken 396 times.
✓ Branch 1 taken 4 times.
|
400 | if (std::cmp_less(rank, static_cast<int>(intervals.size()))) { |
| 151 | 396 | int idx = intervals[rank].index; | |
| 152 | 396 | double new_x = GenerateNewX(idx, m); | |
| 153 | |||
| 154 | 396 | my_point.x = new_x; | |
| 155 | 396 | my_point.z = CalculateFunction(new_x); | |
| 156 | } | ||
| 157 | |||
| 158 | // 6. Сбор результатов со всех процессов | ||
| 159 |
2/6✓ Branch 1 taken 400 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 400 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
|
400 | std::vector<Point> global_res(size); |
| 160 |
1/2✓ Branch 1 taken 400 times.
✗ Branch 2 not taken.
|
400 | MPI_Allgather(&my_point, 2, MPI_DOUBLE, global_res.data(), 2, MPI_DOUBLE, MPI_COMM_WORLD); |
| 161 | |||
| 162 | // 7. Обновление списка | ||
| 163 |
1/2✓ Branch 1 taken 400 times.
✗ Branch 2 not taken.
|
400 | CollectAndAddPoints(global_res, k); |
| 164 |
17/22✓ Branch 0 taken 234 times.
✓ Branch 1 taken 2536 times.
✓ Branch 2 taken 3410 times.
✓ Branch 3 taken 3202 times.
✓ Branch 4 taken 14616 times.
✓ Branch 5 taken 5522 times.
✓ Branch 6 taken 71454 times.
✓ Branch 7 taken 5522 times.
✓ Branch 8 taken 2498 times.
✗ Branch 9 not taken.
✓ Branch 10 taken 550 times.
✓ Branch 11 taken 1948 times.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
✗ Branch 14 not taken.
✗ Branch 15 not taken.
✓ Branch 16 taken 528 times.
✓ Branch 17 taken 5080 times.
✓ Branch 18 taken 4892 times.
✓ Branch 19 taken 5080 times.
✓ Branch 20 taken 16548 times.
✓ Branch 21 taken 14792 times.
|
155914 | std::ranges::sort(trials_, [](const Trial &a, const Trial &b) { return a.x < b.x; }); |
| 165 | } | ||
| 166 | |||
| 167 | // Поиск финального минимума среди всех испытаний | ||
| 168 | auto it = std::ranges::min_element(trials_, {}, &Trial::z); | ||
| 169 | |||
| 170 | 8 | double u = 0; | |
| 171 | 8 | double v = 0; | |
| 172 | 8 | D2xy(it->x, u, v); | |
| 173 | 8 | result_.x = GetInput().ax + (u * (GetInput().bx - GetInput().ax)); | |
| 174 | 8 | result_.y = GetInput().ay + (v * (GetInput().by - GetInput().ay)); | |
| 175 | 8 | result_.value = it->z; | |
| 176 | 8 | result_.iter_count = k; | |
| 177 | |||
| 178 | 8 | GetOutput() = result_; | |
| 179 | 8 | return true; | |
| 180 | } | ||
| 181 | |||
| 182 | 8 | bool KhruevAGlobalOptMPI::PostProcessingImpl() { | |
| 183 | 8 | return true; | |
| 184 | } | ||
| 185 | |||
| 186 | } // namespace khruev_a_global_opt | ||
| 187 |