| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include "dergachev_a_multistep_2d_parallel/mpi/include/ops_mpi.hpp" | ||
| 2 | |||
| 3 | #include <mpi.h> | ||
| 4 | |||
| 5 | #include <algorithm> | ||
| 6 | #include <cmath> | ||
| 7 | #include <cstddef> | ||
| 8 | #include <limits> | ||
| 9 | #include <utility> | ||
| 10 | #include <vector> | ||
| 11 | |||
| 12 | #include "dergachev_a_multistep_2d_parallel/common/include/common.hpp" | ||
| 13 | |||
| 14 | namespace dergachev_a_multistep_2d_parallel { | ||
| 15 | |||
| 16 | namespace { | ||
| 17 | |||
| 18 | 78 | void ComputeDistribution(int num_intervals, int world_size, std::vector<int> &counts, std::vector<int> &displs) { | |
| 19 | 78 | counts.resize(static_cast<std::size_t>(world_size)); | |
| 20 | 78 | displs.resize(static_cast<std::size_t>(world_size)); | |
| 21 | |||
| 22 | 78 | int base_count = num_intervals / world_size; | |
| 23 | 78 | int remainder = num_intervals % world_size; | |
| 24 | |||
| 25 |
2/2✓ Branch 0 taken 156 times.
✓ Branch 1 taken 78 times.
|
234 | for (int i = 0; i < world_size; ++i) { |
| 26 | 156 | auto idx = static_cast<std::size_t>(i); | |
| 27 |
4/4✓ Branch 0 taken 116 times.
✓ Branch 1 taken 40 times.
✓ Branch 2 taken 78 times.
✓ Branch 3 taken 78 times.
|
272 | counts[idx] = base_count + ((i < remainder) ? 1 : 0); |
| 28 |
2/2✓ Branch 0 taken 78 times.
✓ Branch 1 taken 78 times.
|
156 | displs[idx] = (i == 0) ? 0 : (displs[idx - 1] + counts[idx - 1]); |
| 29 | } | ||
| 30 | 78 | } | |
| 31 | |||
| 32 | 39 | void PrepareIntervalData(const std::vector<double> &t_values, const std::vector<TrialPoint> &trials, int num_intervals, | |
| 33 | std::vector<double> &interval_data) { | ||
| 34 | 39 | interval_data.resize(static_cast<std::size_t>(num_intervals) * 4); | |
| 35 |
2/2✓ Branch 0 taken 276 times.
✓ Branch 1 taken 39 times.
|
315 | for (int i = 0; i < num_intervals; ++i) { |
| 36 | 276 | auto idx = static_cast<std::size_t>(i); | |
| 37 | 276 | interval_data[(idx * 4)] = t_values[idx]; | |
| 38 | 276 | interval_data[(idx * 4) + 1] = t_values[idx + 1]; | |
| 39 | 276 | interval_data[(idx * 4) + 2] = trials[idx].z; | |
| 40 | 276 | interval_data[(idx * 4) + 3] = trials[idx + 1].z; | |
| 41 | } | ||
| 42 | 39 | } | |
| 43 | |||
| 44 | 78 | void ComputeLocalCharacteristics(const std::vector<double> &local_interval_data, int local_count, double m_val, | |
| 45 | std::vector<double> &local_chars) { | ||
| 46 | 78 | local_chars.resize(static_cast<std::size_t>(local_count)); | |
| 47 |
2/2✓ Branch 0 taken 276 times.
✓ Branch 1 taken 78 times.
|
354 | for (int i = 0; i < local_count; ++i) { |
| 48 | 276 | auto idx = static_cast<std::size_t>(i); | |
| 49 | 276 | double t_i = local_interval_data[(idx * 4)]; | |
| 50 | 276 | double t_i1 = local_interval_data[(idx * 4) + 1]; | |
| 51 | 276 | double z_i = local_interval_data[(idx * 4) + 2]; | |
| 52 | 276 | double z_i1 = local_interval_data[(idx * 4) + 3]; | |
| 53 | |||
| 54 | 276 | double delta = t_i1 - t_i; | |
| 55 | 276 | double diff = z_i1 - z_i; | |
| 56 | 276 | local_chars[idx] = (m_val * delta) + ((diff * diff) / (m_val * delta)) - (2.0 * (z_i1 + z_i)); | |
| 57 | } | ||
| 58 | 78 | } | |
| 59 | |||
| 60 | 78 | void GatherResultsToRoot(const std::vector<double> &local_chars, const std::vector<int> &counts, | |
| 61 | const std::vector<int> &displs, int world_rank, int world_size, | ||
| 62 | std::vector<double> &characteristics) { | ||
| 63 |
2/2✓ Branch 0 taken 39 times.
✓ Branch 1 taken 39 times.
|
78 | int local_count = counts[static_cast<std::size_t>(world_rank)]; |
| 64 | |||
| 65 |
2/2✓ Branch 0 taken 39 times.
✓ Branch 1 taken 39 times.
|
78 | if (world_rank == 0) { |
| 66 |
2/2✓ Branch 0 taken 148 times.
✓ Branch 1 taken 39 times.
|
187 | for (int i = 0; i < counts[0]; ++i) { |
| 67 | 148 | characteristics[static_cast<std::size_t>(i)] = local_chars[static_cast<std::size_t>(i)]; | |
| 68 | } | ||
| 69 | |||
| 70 |
2/2✓ Branch 0 taken 39 times.
✓ Branch 1 taken 39 times.
|
78 | for (int proc = 1; proc < world_size; ++proc) { |
| 71 |
2/2✓ Branch 0 taken 36 times.
✓ Branch 1 taken 3 times.
|
39 | auto proc_idx = static_cast<std::size_t>(proc); |
| 72 | 39 | int proc_count = counts[proc_idx]; | |
| 73 |
2/2✓ Branch 0 taken 36 times.
✓ Branch 1 taken 3 times.
|
39 | if (proc_count > 0) { |
| 74 |
1/2✓ Branch 2 taken 36 times.
✗ Branch 3 not taken.
|
36 | std::vector<double> recv_chars(static_cast<std::size_t>(proc_count)); |
| 75 |
1/2✓ Branch 1 taken 36 times.
✗ Branch 2 not taken.
|
36 | MPI_Recv(recv_chars.data(), proc_count, MPI_DOUBLE, proc, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE); |
| 76 | 36 | int disp = displs[proc_idx]; | |
| 77 |
2/2✓ Branch 0 taken 128 times.
✓ Branch 1 taken 36 times.
|
164 | for (int i = 0; i < proc_count; ++i) { |
| 78 | 128 | characteristics[static_cast<std::size_t>(disp) + static_cast<std::size_t>(i)] = | |
| 79 | 128 | recv_chars[static_cast<std::size_t>(i)]; | |
| 80 | } | ||
| 81 | } | ||
| 82 | } | ||
| 83 | } else { | ||
| 84 |
2/2✓ Branch 0 taken 36 times.
✓ Branch 1 taken 3 times.
|
39 | if (local_count > 0) { |
| 85 | 36 | MPI_Send(local_chars.data(), local_count, MPI_DOUBLE, 0, 0, MPI_COMM_WORLD); | |
| 86 | } | ||
| 87 | } | ||
| 88 | 78 | } | |
| 89 | |||
| 90 | } // namespace | ||
| 91 | |||
| 92 |
1/2✓ Branch 1 taken 6 times.
✗ Branch 2 not taken.
|
6 | DergachevAMultistep2dParallelMPI::DergachevAMultistep2dParallelMPI(const InType &in) { |
| 93 | SetTypeOfTask(GetStaticTypeOfTask()); | ||
| 94 | GetInput() = in; | ||
| 95 | 6 | GetOutput() = OutType(); | |
| 96 | |||
| 97 |
1/2✓ Branch 1 taken 6 times.
✗ Branch 2 not taken.
|
6 | MPI_Comm_rank(MPI_COMM_WORLD, &world_rank_); |
| 98 |
1/2✓ Branch 1 taken 6 times.
✗ Branch 2 not taken.
|
6 | MPI_Comm_size(MPI_COMM_WORLD, &world_size_); |
| 99 | 6 | } | |
| 100 | |||
| 101 | 6 | bool DergachevAMultistep2dParallelMPI::ValidationImpl() { | |
| 102 |
2/2✓ Branch 0 taken 3 times.
✓ Branch 1 taken 3 times.
|
6 | if (world_rank_ != 0) { |
| 103 | return true; | ||
| 104 | } | ||
| 105 | |||
| 106 | const auto &input = GetInput(); | ||
| 107 | bool valid = true; | ||
| 108 |
1/2✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
|
3 | valid = valid && (input.func != nullptr); |
| 109 |
1/2✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
|
3 | valid = valid && (input.x_min < input.x_max); |
| 110 |
1/2✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
|
3 | valid = valid && (input.y_min < input.y_max); |
| 111 |
1/2✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
|
3 | valid = valid && (input.epsilon > 0); |
| 112 |
1/2✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
|
3 | valid = valid && (input.r_param > 1.0); |
| 113 |
1/2✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
|
3 | valid = valid && (input.max_iterations > 0); |
| 114 | return valid; | ||
| 115 | } | ||
| 116 | |||
| 117 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
|
6 | bool DergachevAMultistep2dParallelMPI::PreProcessingImpl() { |
| 118 | trials_.clear(); | ||
| 119 | t_values_.clear(); | ||
| 120 | 6 | m_estimate_ = 1.0; | |
| 121 | 6 | return true; | |
| 122 | } | ||
| 123 | |||
| 124 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
|
6 | bool DergachevAMultistep2dParallelMPI::RunImpl() { |
| 125 | const auto &input = GetInput(); | ||
| 126 | auto &output = GetOutput(); | ||
| 127 | |||
| 128 | trials_.clear(); | ||
| 129 | t_values_.clear(); | ||
| 130 | |||
| 131 | 6 | std::vector<double> initial_data(8); | |
| 132 | |||
| 133 |
2/2✓ Branch 0 taken 3 times.
✓ Branch 1 taken 3 times.
|
6 | if (world_rank_ == 0) { |
| 134 | double t0 = 0.0; | ||
| 135 | double t1 = 1.0; | ||
| 136 | |||
| 137 | 3 | double x0 = PeanoToX(t0, input.x_min, input.x_max, input.y_min, input.y_max, peano_level_); | |
| 138 | 3 | double y0 = PeanoToY(t0, input.x_min, input.x_max, input.y_min, input.y_max, peano_level_); | |
| 139 | 3 | double z0 = input.func(x0, y0); | |
| 140 | |||
| 141 | 3 | double x1 = PeanoToX(t1, input.x_min, input.x_max, input.y_min, input.y_max, peano_level_); | |
| 142 | 3 | double y1 = PeanoToY(t1, input.x_min, input.x_max, input.y_min, input.y_max, peano_level_); | |
| 143 | 3 | double z1 = input.func(x1, y1); | |
| 144 | |||
| 145 | 3 | initial_data[0] = t0; | |
| 146 | 3 | initial_data[1] = x0; | |
| 147 | 3 | initial_data[2] = y0; | |
| 148 | 3 | initial_data[3] = z0; | |
| 149 | 3 | initial_data[4] = t1; | |
| 150 | 3 | initial_data[5] = x1; | |
| 151 | 3 | initial_data[6] = y1; | |
| 152 | 3 | initial_data[7] = z1; | |
| 153 | |||
| 154 |
2/2✓ Branch 0 taken 3 times.
✓ Branch 1 taken 3 times.
|
6 | for (int proc = 1; proc < world_size_; ++proc) { |
| 155 |
1/2✓ Branch 1 taken 3 times.
✗ Branch 2 not taken.
|
3 | MPI_Send(initial_data.data(), 8, MPI_DOUBLE, proc, 0, MPI_COMM_WORLD); |
| 156 | } | ||
| 157 | } else { | ||
| 158 |
1/2✓ Branch 1 taken 3 times.
✗ Branch 2 not taken.
|
3 | MPI_Recv(initial_data.data(), 8, MPI_DOUBLE, 0, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE); |
| 159 | } | ||
| 160 | |||
| 161 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
|
6 | t_values_.push_back(initial_data[0]); |
| 162 | t_values_.push_back(initial_data[4]); | ||
| 163 |
1/2✓ Branch 1 taken 6 times.
✗ Branch 2 not taken.
|
6 | trials_.emplace_back(initial_data[1], initial_data[2], initial_data[3]); |
| 164 |
1/2✓ Branch 1 taken 6 times.
✗ Branch 2 not taken.
|
6 | trials_.emplace_back(initial_data[5], initial_data[6], initial_data[7]); |
| 165 | |||
| 166 | 6 | m_estimate_ = 1.0; | |
| 167 | |||
| 168 |
1/2✓ Branch 0 taken 78 times.
✗ Branch 1 not taken.
|
78 | for (int iter = 0; iter < input.max_iterations; ++iter) { |
| 169 |
1/2✓ Branch 1 taken 78 times.
✗ Branch 2 not taken.
|
78 | BroadcastTrialData(); |
| 170 | |||
| 171 |
1/2✓ Branch 1 taken 78 times.
✗ Branch 2 not taken.
|
78 | SortTrialsByT(); |
| 172 | |||
| 173 | 78 | m_estimate_ = ComputeLipschitzEstimate(); | |
| 174 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 78 times.
|
78 | if (m_estimate_ < 1e-10) { |
| 175 | ✗ | m_estimate_ = 1.0; | |
| 176 | } | ||
| 177 | |||
| 178 | 78 | std::vector<double> all_characteristics; | |
| 179 |
1/2✓ Branch 1 taken 78 times.
✗ Branch 2 not taken.
|
78 | ComputeCharacteristicsParallel(input.r_param * m_estimate_, all_characteristics); |
| 180 | |||
| 181 | int best_idx = SelectBestInterval(all_characteristics); | ||
| 182 | |||
| 183 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 78 times.
|
78 | double t_left = t_values_[best_idx]; |
| 184 | 78 | double t_right = t_values_[best_idx + 1]; | |
| 185 | 78 | double z_left = trials_[best_idx].z; | |
| 186 | 78 | double z_right = trials_[best_idx + 1].z; | |
| 187 | |||
| 188 | 78 | double m_val = input.r_param * m_estimate_; | |
| 189 | 78 | double t_new = (0.5 * (t_left + t_right)) - ((z_right - z_left) / (2.0 * m_val)); | |
| 190 | |||
| 191 |
2/4✗ Branch 0 not taken.
✓ Branch 1 taken 78 times.
✓ Branch 2 taken 78 times.
✗ Branch 3 not taken.
|
78 | t_new = std::max(t_left + 1e-12, std::min(t_new, t_right - 1e-12)); |
| 192 | |||
| 193 | 78 | double delta = t_right - t_left; | |
| 194 |
2/2✓ Branch 0 taken 72 times.
✓ Branch 1 taken 6 times.
|
78 | int converged_flag = (delta < input.epsilon) ? 1 : 0; |
| 195 | |||
| 196 |
1/2✓ Branch 1 taken 78 times.
✗ Branch 2 not taken.
|
78 | MPI_Bcast(&converged_flag, 1, MPI_INT, 0, MPI_COMM_WORLD); |
| 197 | |||
| 198 |
2/2✓ Branch 0 taken 6 times.
✓ Branch 1 taken 72 times.
|
78 | if (converged_flag != 0) { |
| 199 | 6 | output.converged = true; | |
| 200 |
1/2✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
|
6 | output.iterations = iter + 1; |
| 201 | break; | ||
| 202 | } | ||
| 203 | |||
| 204 |
2/2✓ Branch 0 taken 36 times.
✓ Branch 1 taken 36 times.
|
72 | if (world_rank_ == 0) { |
| 205 |
1/2✓ Branch 1 taken 36 times.
✗ Branch 2 not taken.
|
36 | double z_new = PerformTrial(t_new); |
| 206 | 36 | double x_new = PeanoToX(t_new, input.x_min, input.x_max, input.y_min, input.y_max, peano_level_); | |
| 207 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 36 times.
|
36 | double y_new = PeanoToY(t_new, input.x_min, input.x_max, input.y_min, input.y_max, peano_level_); |
| 208 | |||
| 209 | t_values_.push_back(t_new); | ||
| 210 |
1/2✓ Branch 1 taken 36 times.
✗ Branch 2 not taken.
|
36 | trials_.emplace_back(x_new, y_new, z_new); |
| 211 | } | ||
| 212 | |||
| 213 |
1/2✓ Branch 0 taken 72 times.
✗ Branch 1 not taken.
|
72 | output.iterations = iter + 1; |
| 214 | } | ||
| 215 | |||
| 216 |
1/2✓ Branch 1 taken 6 times.
✗ Branch 2 not taken.
|
6 | BroadcastTrialData(); |
| 217 | |||
| 218 | 6 | return true; | |
| 219 | } | ||
| 220 | |||
| 221 | 6 | bool DergachevAMultistep2dParallelMPI::PostProcessingImpl() { | |
| 222 | auto &output = GetOutput(); | ||
| 223 | |||
| 224 | double min_z = std::numeric_limits<double>::max(); | ||
| 225 | int min_idx = 0; | ||
| 226 | |||
| 227 |
2/2✓ Branch 0 taken 84 times.
✓ Branch 1 taken 6 times.
|
90 | for (std::size_t i = 0; i < trials_.size(); ++i) { |
| 228 |
2/2✓ Branch 0 taken 26 times.
✓ Branch 1 taken 58 times.
|
84 | if (trials_[i].z < min_z) { |
| 229 | min_z = trials_[i].z; | ||
| 230 | 26 | min_idx = static_cast<int>(i); | |
| 231 | } | ||
| 232 | } | ||
| 233 | |||
| 234 | 6 | output.x_opt = trials_[min_idx].x; | |
| 235 | 6 | output.y_opt = trials_[min_idx].y; | |
| 236 | 6 | output.func_min = min_z; | |
| 237 | |||
| 238 | 6 | return true; | |
| 239 | } | ||
| 240 | |||
| 241 | 78 | void DergachevAMultistep2dParallelMPI::SortTrialsByT() { | |
| 242 | 78 | std::vector<std::size_t> indices(t_values_.size()); | |
| 243 |
2/2✓ Branch 0 taken 630 times.
✓ Branch 1 taken 78 times.
|
708 | for (std::size_t i = 0; i < indices.size(); ++i) { |
| 244 | 630 | indices[i] = i; | |
| 245 | } | ||
| 246 |
3/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 not taken.
✗ Branch 18 not taken.
✓ Branch 19 taken 552 times.
✓ Branch 20 taken 272 times.
✓ Branch 21 taken 552 times.
|
1376 | std::ranges::sort(indices, [this](std::size_t a, std::size_t b) { return t_values_[a] < t_values_[b]; }); |
| 247 | |||
| 248 |
2/6✓ Branch 1 taken 78 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 78 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
|
78 | std::vector<double> sorted_t(t_values_.size()); |
| 249 |
1/4✓ Branch 1 taken 78 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
|
78 | std::vector<TrialPoint> sorted_trials(trials_.size()); |
| 250 |
2/2✓ Branch 0 taken 630 times.
✓ Branch 1 taken 78 times.
|
708 | for (std::size_t i = 0; i < indices.size(); ++i) { |
| 251 | 630 | sorted_t[i] = t_values_[indices[i]]; | |
| 252 | 630 | sorted_trials[i] = trials_[indices[i]]; | |
| 253 | } | ||
| 254 | 78 | t_values_ = std::move(sorted_t); | |
| 255 | 78 | trials_ = std::move(sorted_trials); | |
| 256 | 78 | } | |
| 257 | |||
| 258 | 78 | double DergachevAMultistep2dParallelMPI::ComputeLipschitzEstimate() { | |
| 259 | 78 | double max_slope = 0.0; | |
| 260 | |||
| 261 |
2/2✓ Branch 0 taken 552 times.
✓ Branch 1 taken 78 times.
|
630 | for (std::size_t i = 1; i < t_values_.size(); ++i) { |
| 262 | 552 | double dt = t_values_[i] - t_values_[i - 1]; | |
| 263 |
1/2✓ Branch 0 taken 552 times.
✗ Branch 1 not taken.
|
552 | if (dt > 1e-15) { |
| 264 | 552 | double dz = std::abs(trials_[i].z - trials_[i - 1].z); | |
| 265 | 552 | double slope = dz / dt; | |
| 266 | 552 | max_slope = std::max(slope, max_slope); | |
| 267 | } | ||
| 268 | } | ||
| 269 | |||
| 270 |
2/2✓ Branch 0 taken 72 times.
✓ Branch 1 taken 6 times.
|
78 | return max_slope > 0.0 ? max_slope : 1.0; |
| 271 | } | ||
| 272 | |||
| 273 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 78 times.
|
78 | void DergachevAMultistep2dParallelMPI::ComputeCharacteristicsParallel(double m_val, |
| 274 | std::vector<double> &characteristics) { | ||
| 275 | 78 | int num_intervals = static_cast<int>(t_values_.size()) - 1; | |
| 276 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 78 times.
|
78 | if (num_intervals <= 0) { |
| 277 | characteristics.clear(); | ||
| 278 | ✗ | return; | |
| 279 | } | ||
| 280 | |||
| 281 | 78 | std::vector<int> counts; | |
| 282 | 78 | std::vector<int> displs; | |
| 283 |
1/2✓ Branch 1 taken 78 times.
✗ Branch 2 not taken.
|
78 | ComputeDistribution(num_intervals, world_size_, counts, displs); |
| 284 | |||
| 285 | 78 | std::vector<double> interval_data; | |
| 286 |
2/2✓ Branch 0 taken 39 times.
✓ Branch 1 taken 39 times.
|
78 | if (world_rank_ == 0) { |
| 287 |
1/2✓ Branch 1 taken 39 times.
✗ Branch 2 not taken.
|
39 | PrepareIntervalData(t_values_, trials_, num_intervals, interval_data); |
| 288 | } else { | ||
| 289 |
1/2✓ Branch 1 taken 39 times.
✗ Branch 2 not taken.
|
39 | interval_data.resize(static_cast<std::size_t>(num_intervals) * 4); |
| 290 | } | ||
| 291 | |||
| 292 |
1/4✓ Branch 1 taken 78 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
|
78 | std::vector<int> send_counts(static_cast<std::size_t>(world_size_)); |
| 293 |
1/4✓ Branch 1 taken 78 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
|
78 | std::vector<int> send_displs(static_cast<std::size_t>(world_size_)); |
| 294 |
2/2✓ Branch 0 taken 156 times.
✓ Branch 1 taken 78 times.
|
234 | for (int i = 0; i < world_size_; ++i) { |
| 295 | 156 | auto idx = static_cast<std::size_t>(i); | |
| 296 | 156 | send_counts[idx] = counts[idx] * 4; | |
| 297 | 156 | send_displs[idx] = displs[idx] * 4; | |
| 298 | } | ||
| 299 | |||
| 300 |
1/2✓ Branch 1 taken 78 times.
✗ Branch 2 not taken.
|
78 | int local_count = counts[static_cast<std::size_t>(world_rank_)]; |
| 301 |
1/4✓ Branch 1 taken 78 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
|
78 | std::vector<double> local_interval_data(static_cast<std::size_t>(local_count) * 4); |
| 302 |
1/2✓ Branch 1 taken 78 times.
✗ Branch 2 not taken.
|
78 | MPI_Scatterv(interval_data.data(), send_counts.data(), send_displs.data(), MPI_DOUBLE, local_interval_data.data(), |
| 303 | local_count * 4, MPI_DOUBLE, 0, MPI_COMM_WORLD); | ||
| 304 | |||
| 305 | 78 | std::vector<double> local_chars; | |
| 306 |
1/2✓ Branch 1 taken 78 times.
✗ Branch 2 not taken.
|
78 | ComputeLocalCharacteristics(local_interval_data, local_count, m_val, local_chars); |
| 307 | |||
| 308 |
1/2✓ Branch 1 taken 78 times.
✗ Branch 2 not taken.
|
78 | characteristics.resize(static_cast<std::size_t>(num_intervals)); |
| 309 |
1/2✓ Branch 1 taken 78 times.
✗ Branch 2 not taken.
|
78 | GatherResultsToRoot(local_chars, counts, displs, world_rank_, world_size_, characteristics); |
| 310 | |||
| 311 |
1/2✓ Branch 1 taken 78 times.
✗ Branch 2 not taken.
|
78 | MPI_Bcast(characteristics.data(), num_intervals, MPI_DOUBLE, 0, MPI_COMM_WORLD); |
| 312 | } | ||
| 313 | |||
| 314 | ✗ | int DergachevAMultistep2dParallelMPI::SelectBestInterval(const std::vector<double> &characteristics) { | |
| 315 | double max_char = -std::numeric_limits<double>::max(); | ||
| 316 | int best_idx = 0; | ||
| 317 | |||
| 318 |
2/4✗ Branch 0 not taken.
✗ Branch 1 not taken.
✓ Branch 2 taken 552 times.
✓ Branch 3 taken 78 times.
|
630 | for (std::size_t i = 0; i < characteristics.size(); ++i) { |
| 319 |
2/4✗ Branch 0 not taken.
✗ Branch 1 not taken.
✓ Branch 2 taken 198 times.
✓ Branch 3 taken 354 times.
|
552 | if (characteristics[i] > max_char) { |
| 320 | max_char = characteristics[i]; | ||
| 321 | 198 | best_idx = static_cast<int>(i); | |
| 322 | } | ||
| 323 | } | ||
| 324 | |||
| 325 | ✗ | return best_idx; | |
| 326 | } | ||
| 327 | |||
| 328 | 36 | double DergachevAMultistep2dParallelMPI::PerformTrial(double t) { | |
| 329 | const auto &input = GetInput(); | ||
| 330 | 36 | double x = PeanoToX(t, input.x_min, input.x_max, input.y_min, input.y_max, peano_level_); | |
| 331 | 36 | double y = PeanoToY(t, input.x_min, input.x_max, input.y_min, input.y_max, peano_level_); | |
| 332 | 36 | return input.func(x, y); | |
| 333 | } | ||
| 334 | |||
| 335 | 84 | void DergachevAMultistep2dParallelMPI::BroadcastTrialData() { | |
| 336 | 84 | int size = static_cast<int>(t_values_.size()); | |
| 337 | 84 | MPI_Bcast(&size, 1, MPI_INT, 0, MPI_COMM_WORLD); | |
| 338 | |||
| 339 |
2/2✓ Branch 0 taken 42 times.
✓ Branch 1 taken 42 times.
|
84 | if (world_rank_ != 0) { |
| 340 | 42 | t_values_.resize(static_cast<std::size_t>(size)); | |
| 341 | 42 | trials_.resize(static_cast<std::size_t>(size)); | |
| 342 | } | ||
| 343 | |||
| 344 | 84 | MPI_Bcast(t_values_.data(), size, MPI_DOUBLE, 0, MPI_COMM_WORLD); | |
| 345 | |||
| 346 | 84 | std::vector<double> trial_data(static_cast<std::size_t>(size) * 3); | |
| 347 |
2/2✓ Branch 0 taken 42 times.
✓ Branch 1 taken 42 times.
|
84 | if (world_rank_ == 0) { |
| 348 |
2/2✓ Branch 0 taken 357 times.
✓ Branch 1 taken 42 times.
|
399 | for (int i = 0; i < size; ++i) { |
| 349 | 357 | auto idx = static_cast<std::size_t>(i); | |
| 350 | 357 | trial_data[(idx * 3)] = trials_[idx].x; | |
| 351 | 357 | trial_data[(idx * 3) + 1] = trials_[idx].y; | |
| 352 | 357 | trial_data[(idx * 3) + 2] = trials_[idx].z; | |
| 353 | } | ||
| 354 | } | ||
| 355 | |||
| 356 |
1/2✓ Branch 1 taken 84 times.
✗ Branch 2 not taken.
|
84 | MPI_Bcast(trial_data.data(), size * 3, MPI_DOUBLE, 0, MPI_COMM_WORLD); |
| 357 | |||
| 358 |
2/2✓ Branch 0 taken 42 times.
✓ Branch 1 taken 42 times.
|
84 | if (world_rank_ != 0) { |
| 359 |
2/2✓ Branch 0 taken 357 times.
✓ Branch 1 taken 42 times.
|
399 | for (int i = 0; i < size; ++i) { |
| 360 | 357 | auto idx = static_cast<std::size_t>(i); | |
| 361 | 357 | trials_[idx].x = trial_data[(idx * 3)]; | |
| 362 | 357 | trials_[idx].y = trial_data[(idx * 3) + 1]; | |
| 363 | 357 | trials_[idx].z = trial_data[(idx * 3) + 2]; | |
| 364 | } | ||
| 365 | } | ||
| 366 | 84 | } | |
| 367 | |||
| 368 | } // namespace dergachev_a_multistep_2d_parallel | ||
| 369 |