• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

openmc-dev / openmc / 44562703135

19 Nov 2026 04:08PM UTC coverage: 82.274% (-0.08%) from 82.354%
44562703135

Pull #4246

github

web-flow
Merge 658124864 into b3443897d
Pull Request #4246: Random Ray Domain Decomposed Load Balancing MPI Optimization

19948 of 28028 branches covered (71.17%)

Branch coverage included in aggregate %.

84 of 86 new or added lines in 1 file covered. (97.67%)

351 existing lines in 30 files now uncovered.

63228 of 73068 relevant lines covered (86.53%)

47191136.83 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

92.89
/src/random_ray/decomposition_map.cpp
1
#include "openmc/random_ray/decomposition_map.h"
2

3
#include "openmc/cell.h"
4
#include "openmc/constants.h"
5
#include "openmc/message_passing.h"
6
#include "openmc/mgxs_interface.h"
7
#include "openmc/output.h"
8
#include "openmc/random_lcg.h"
9
#include "openmc/random_ray/flat_source_domain.h"
10
#include "openmc/random_ray/random_ray.h"
11
#include "openmc/simulation.h"
12
#include "openmc/timer.h"
13
#include "openmc/vector.h"
14
#include <algorithm>
15
#include <cstring>
16
#include <numeric>
17

18
#ifdef OPENMC_MPI
19

20
namespace openmc {
21

22
namespace mpi {
23
DecompositionMap decomp_map;
24
}
25

26
// Constructor
27
DecompositionMap::DecompositionMap() {}
1,802 ✔
28

29
void DecompositionMap::initialize()
328 ✔
30
{
31
  negroups_ = data::mg.num_energy_groups_;
328 ✔
32
  estimated_rank_load_fractions_.resize(mpi::n_procs, 0.0);
328 ✔
33
  measured_rank_load_fractions_.resize(mpi::n_procs, 0.0);
328 ✔
34
  estimated_rank_load_totals_.resize(mpi::n_procs, 0.0);
328 ✔
35
  target_load_ = 1.0 / mpi::n_procs;
328 ✔
36
  rank_weights_.resize(mpi::n_procs, 1.0);
328 ✔
37

38
  auto* independent_source =
328 !
39
    dynamic_cast<IndependentSource*>(RandomRay::ray_source_.get());
328 !
40
  spatial_box_ = independent_source
656 ✔
41
                   ? dynamic_cast<SpatialBox*>(independent_source->space())
328 !
42
                   : nullptr;
43
  if (!spatial_box_) {
328 !
44
    fatal_error("Domain decomposition of the random ray solver requires the "
×
45
                "ray source to be an independent source with a box spatial "
46
                "distribution.");
47
  }
48

49
  double x_length =
328 ✔
50
    spatial_box_->upper_right().x - spatial_box_->lower_left().x;
328 ✔
51
  double y_length =
328 ✔
52
    spatial_box_->upper_right().y - spatial_box_->lower_left().y;
328 ✔
53
  double z_length =
328 ✔
54
    spatial_box_->upper_right().z - spatial_box_->lower_left().z;
328 ✔
55

56
  max_domain_length_ =
656 ✔
57
    sqrt(x_length * x_length + y_length * y_length + z_length * z_length);
328 ✔
58

59
  is_linear_ = RandomRay::source_shape_ != RandomRaySourceShape::FLAT;
328 ✔
60
  is_adaptive_ =
328 ✔
61
    is_adaptive_family(FlatSourceDomain::resolved_volume_estimator_);
328 ✔
62

63
  // Size in bytes of the record that carries one source region between ranks.
64
  // It depends only on the settings above and on the run mode, so it is
65
  // measured once per solve on a freshly constructed region.
66
  SourceRegion sr(negroups_, is_linear_);
328 ✔
67
  sr_record_bytes_ = transfer_sr_fields(sr, nullptr, SRTransfer::SIZE);
328 ✔
68

69
  // Count the number of source regions in the model
70
  n_base_sr_ = 0;
328 ✔
71
  for (const auto& c : model::cells) {
2,472 ✔
72
    if (c->type_ == Fill::MATERIAL) {
2,144 ✔
73
      n_base_sr_ += c->n_instances();
1,212 ✔
74
    }
75
  }
76

77
  num_base_source_region_RT_.resize(n_base_sr_, 0);
328 ✔
78
  num_mesh_bin_RT_.resize(n_base_sr_, 0);
328 ✔
79
  ray_tracing_cost_.resize(n_base_sr_);
328 ✔
80
  volume_base_sr_.resize(n_base_sr_, 0.0);
328 ✔
81
}
328 ✔
82

83
void DecompositionMap::generate_rank_centers()
288 ✔
84
{
85

86
  // Calculate grid points that are used for Voronoi cells
87
  int grid_points_total = grid_points_per_rank_ * mpi::n_procs;
288 ✔
88
  write_message(6, "Calculating {} grid points for Voronoi tessellation...",
288 ✔
89
    grid_points_total);
90
  calculate_grid_points(grid_points_total);
288 ✔
91

92
  // Initialize points with random positions
93
  initialize_voronoi_centers();
288 ✔
94

95
  double err = 1.0;
288 ✔
96
  double precision = 1e-3; // corresponding to 0.001 cm position change
288 ✔
97
  int it = 0;
288 ✔
98
  int max_iterations = 100;
288 ✔
99

100
  // Lloyd's algorithm to move Voronoi centers to centroids of their cells
101
  // https://en.wikipedia.org/wiki/Lloyd%27s_algorithm
102
  while (err > precision && it < max_iterations) {
3,080 !
103
    // Reset error to determine maximum movement below
104
    err = 0.0;
2,792 ✔
105

106
    vector<Position> position_sum_per_rank(mpi::n_procs, Position(0, 0, 0));
2,792 ✔
107
    vector<int> num_points_per_rank(mpi::n_procs, 0);
2,792 ✔
108

109
    // Compute Voronoi cells by summing up all position values of mesh grid
110
    // points that are closest to a Voronoi center
111
    calculate_voronoi(position_sum_per_rank, num_points_per_rank);
2,792 ✔
112

113
    for (int rank = 0; rank < mpi::n_procs; rank++) {
8,376 ✔
114

115
      // Calculate centroid of the cell
116
      Position centroid = calculate_centroids(
5,584 ✔
117
        position_sum_per_rank[rank], num_points_per_rank[rank], rank);
5,584 ✔
118

119
      // Calculate movement for convergence check
120
      double movement = (centroid - rank_centers_[rank]).norm();
5,584 ✔
121

122
      // Move rank center to centroid
123
      rank_centers_[rank] = centroid;
5,584 ✔
124

125
      // Record maximum movement
126
      if (movement > err) {
5,584 ✔
127
        err = movement;
3,577 ✔
128
      }
129
    }
130

131
    it++;
2,792 ✔
132
  }
2,792 ✔
133

134
  // The centers must be bitwise identical on every rank: find_closest_rank()
135
  // is evaluated independently on different ranks for the same physical
136
  // point and they have to agree, or ownership diverges and a ray can
137
  // ping-pong forever. The Lloyd accumulation above uses omp atomic on
138
  // doubles, whose summation order is not reproducible, so broadcast rank
139
  // 0's result rather than relying on every rank landing on the same bits.
140
  MPI_Bcast(rank_centers_.data(), rank_centers_.size() * sizeof(Position),
288 ✔
141
    MPI_BYTE, 0, mpi::intracomm);
142

143
  if (mpi::master) {
288 ✔
144
    if (it == max_iterations) {
144 !
145
      warning("Lloyd's algorithm did not converge within the maximum number of "
×
146
              "iterations.");
147
    } else {
148
      write_message(6, "Lloyd's algorithm converged in {} iterations.", it);
288 ✔
149
    }
150
  }
151
}
288 ✔
152

153
// Calculate grid points needed for calculating Voronoi cells
154
void DecompositionMap::calculate_grid_points(int grid_points_total)
288 ✔
155
{
156

157
  // Calculate length along each dimension
158
  vector<double> domain_length(3);
288 ✔
159
  domain_length[0] =
288 ✔
160
    spatial_box_->upper_right().x - spatial_box_->lower_left().x;
288 ✔
161
  domain_length[1] =
288 ✔
162
    spatial_box_->upper_right().y - spatial_box_->lower_left().y;
288 ✔
163
  domain_length[2] =
288 ✔
164
    spatial_box_->upper_right().z - spatial_box_->lower_left().z;
288 ✔
165

166
  double volume = domain_length[0] * domain_length[1] * domain_length[2];
288 ✔
167

168
  // For each dimension, determine grid points along that direction based on
169
  // aspect ratio: domain_length / volume^(1/3) = grid_points_dimension /
170
  // grid_points_total^(1/3). Check if any dimension is so distorted that it
171
  // would only receive minimum of 1 grid point and flag that direction to
172
  // correct total number of grid points.
173
  int excluded_dimension = -1;
288 ✔
174
  vector<int> grid_points_per_dimension(3);
288 ✔
175
  for (int i = 0; i < 3; i++) {
1,152 ✔
176
    double grid_points_estimate =
864 ✔
177
      ((domain_length[i] / cbrt(volume)) * cbrt(grid_points_total));
864 ✔
178

179
    if (grid_points_estimate > 1) {
864 !
180
      grid_points_per_dimension[i] = round(grid_points_estimate);
864 ✔
181
    } else {
182
      excluded_dimension = i;
×
183
      grid_points_per_dimension[i] = 1;
×
184
    }
185
  }
186

187
  // If problem is 2D, exclude z direction
188
  if (RandomRay::geom_dim_ == RandomRayGeomDim::TWO_DIM) {
288 ✔
189
    excluded_dimension = 2;
116 ✔
190
    grid_points_per_dimension[2] = 1;
116 ✔
191
  }
192

193
  // If one dimension is excluded, recalculate grid points in other two
194
  // dimensions based on area.
195
  if (excluded_dimension != -1) {
288 !
196
    double area = 1.0;
197

198
    for (int i = 0; i < 3; i++) {
464 ✔
199
      if (i == excluded_dimension)
348 ✔
200
        continue;
116 ✔
201
      area *= domain_length[i];
232 ✔
202
    }
203

204
    for (int i = 0; i < 3; i++) {
464 ✔
205
      if (i == excluded_dimension)
348 ✔
206
        continue;
116 ✔
207
      grid_points_per_dimension[i] =
464 ✔
208
        round((domain_length[i] / sqrt(area)) * sqrt(grid_points_total));
232 ✔
209
    }
210
  }
211

212
  // Adjust grid points in each dimension to match actual total number of grid
213
  // points to the number of grid points requested
214
  double new_total = grid_points_per_dimension[0] *
288 ✔
215
                     grid_points_per_dimension[1] *
288 ✔
216
                     grid_points_per_dimension[2];
288 ✔
217
  double adjustment;
288 ✔
218
  if (excluded_dimension != -1) {
288 ✔
219
    // When one dimension is excluded, use square root for the other two
220
    // dimensions
221
    adjustment = sqrt(grid_points_total / new_total);
116 ✔
222
  } else {
223
    // When all dimensions are used, use cubic root
224
    adjustment = cbrt(grid_points_total / new_total);
172 ✔
225
  }
226

227
  // Multiply adjustment factor
228
  for (int i = 0; i < 3; i++) {
1,152 ✔
229
    if (i == excluded_dimension)
864 ✔
230
      continue;
116 ✔
231
    grid_points_per_dimension[i] =
748 ✔
232
      round(grid_points_per_dimension[i] * adjustment);
748 ✔
233
  }
234

235
  // Calculate spacing between grid points in each dimension
236
  vector<double> delta_value(3, 0.0);
288 ✔
237

238
  for (int i = 0; i < 3; i++) {
1,152 ✔
239
    if (grid_points_per_dimension[i] > 1) {
864 ✔
240
      delta_value[i] =
748 ✔
241
        (domain_length[i] - 2 * TINY_BIT) / (grid_points_per_dimension[i] - 1);
748 ✔
242
    }
243
  }
244

245
  // Initialize point at center of domain (in case of only 1 grid point in a
246
  // dimension)
247
  double x = spatial_box_->lower_left().x + domain_length[0] * 0.5;
288 ✔
248
  double y = spatial_box_->lower_left().y + domain_length[1] * 0.5;
288 ✔
249
  double z = spatial_box_->lower_left().z + domain_length[2] * 0.5;
288 ✔
250

251
  // Generate all grid points
252
  for (int i = 0; i < grid_points_per_dimension[0]; i++) {
3,240 ✔
253
    if (grid_points_per_dimension[0] > 1) {
2,952 !
254
      x = spatial_box_->lower_left().x + TINY_BIT + i * delta_value[0];
2,952 ✔
255
    }
256
    for (int j = 0; j < grid_points_per_dimension[1]; j++) {
38,840 ✔
257
      if (grid_points_per_dimension[1] > 1) {
35,888 !
258
        y = spatial_box_->lower_left().y + TINY_BIT + j * delta_value[1];
35,888 ✔
259
      }
260
      for (int k = 0; k < grid_points_per_dimension[2]; k++) {
102,736 ✔
261
        if (grid_points_per_dimension[2] > 1) {
66,848 ✔
262
          z = spatial_box_->lower_left().z + TINY_BIT + k * delta_value[2];
37,152 ✔
263
        }
264
        // Add grid point
265
        grid_points_.push_back({x, y, z});
66,848 ✔
266
      }
267
    }
268
  }
269

270
  // Check if mesh grid points are inside spatial domain, which can be different
271
  // from a box. If not, erase them.
272
  for (int i = grid_points_.size() - 1; i >= 0; i--) {
67,136 ✔
273
    Position xi = grid_points_[i];
66,848 ✔
274

275
    bool is_inside_domain =
66,848 ✔
276
      RandomRay::ray_source_->satisfies_spatial_constraints(xi);
66,848 ✔
277

278
    if (!is_inside_domain) {
66,848 !
279
      grid_points_.erase(grid_points_.begin() + i);
×
280
    }
281
  }
282

283
  if (mpi::master && grid_points_.size() < grid_points_total) {
288 ✔
284
    warning(fmt::format("Spatial constraints reduced grid points for Voronoi "
86 ✔
285
                        "tesselation from {} to {}.",
286
      grid_points_total, grid_points_.size()));
172 ✔
287
  }
288
}
288 ✔
289

290
// Places random points in the spatial domain.
291
// Each point corresponds to the initial center of a rank.
292
void DecompositionMap::initialize_voronoi_centers()
288 ✔
293
{
294
  rank_centers_.resize(mpi::n_procs);
288 ✔
295

296
  uint64_t seed = openmc_get_seed();
288 ✔
297
  int rank_cnt = 0;
288 ✔
298

299
  // Sample random positions to start with
300
  while (rank_cnt < mpi::n_procs) {
1,152 ✔
301

302
    double x = prn(&seed);
576 ✔
303
    double y = prn(&seed);
576 ✔
304
    double z = 0.0;
576 ✔
305
    if (RandomRay::geom_dim_ == RandomRayGeomDim::THREE_DIM) {
576 ✔
306
      z = prn(&seed);
344 ✔
307
    } else {
308
      z = 0.5; // Mid-plane in z direction
309
    }
310

311
    Position xi {x, y, z};
576 ✔
312

313
    // Make a small shift in position to avoid geometry floating point issues at
314
    // boundaries
315
    Position shift {FP_COINCIDENT, FP_COINCIDENT, FP_COINCIDENT};
576 ✔
316
    xi = (spatial_box_->lower_left() + shift) +
1,152 ✔
317
         xi * ((spatial_box_->upper_right() - shift) -
576 ✔
318
                (spatial_box_->lower_left() + shift));
576 ✔
319

320
    bool is_inside_domain =
576 ✔
321
      RandomRay::ray_source_->satisfies_spatial_constraints(xi);
576 ✔
322

323
    if (is_inside_domain) {
576 !
324
      rank_centers_[rank_cnt] = xi;
576 ✔
325
      rank_cnt++;
576 ✔
326
    }
327
  }
328
}
288 ✔
329

330
// Determine the distance of each mesh grid point to all rank centers.
331
// Sum up the positions of all mesh grid points that are closest to a given rank
332
// center for computation of centroid later. Record number of grid points per
333
// rank center.
334
void DecompositionMap::calculate_voronoi(
2,792 ✔
335
  vector<Position>& position_sum_per_rank, vector<int>& num_points_per_rank)
336
{
337

338
// Assign each point to the closest rank center
339
#pragma omp parallel for schedule(static)
1,396 ✔
340
  for (int p = 0; p < grid_points_.size(); p++) {
327,812 ✔
341
    Position point = grid_points_[p];
326,416 ✔
342
    int closest_rank = C_NONE;
326,416 ✔
343
    double min_distance = INFTY;
326,416 ✔
344

345
    // Find closest rank center
346
    for (int rank = 0; rank < mpi::n_procs; rank++) {
979,248 ✔
347
      double dist = (point - rank_centers_[rank]).norm();
652,832 ✔
348
      // Power Voronoi diagram uses squared distances
349
      dist = dist * dist - rank_weights_[rank];
652,832 ✔
350

351
      if (dist < min_distance) {
652,832 ✔
352
        min_distance = dist;
488,180 ✔
353
        closest_rank = rank;
488,180 ✔
354
      }
355
    }
356

357
    if (closest_rank == C_NONE) {
326,416 !
UNCOV
358
      fatal_error("Could not find closest rank for Voronoi cell point " +
×
UNCOV
359
                  std::to_string(p) + ".");
×
360
    }
361

362
// Accumulate point coordinates for the closest rank
363
#pragma omp atomic
364
    position_sum_per_rank[closest_rank].x += point.x;
326,416 ✔
365
#pragma omp atomic
366
    position_sum_per_rank[closest_rank].y += point.y;
326,416 ✔
367
#pragma omp atomic
368
    position_sum_per_rank[closest_rank].z += point.z;
326,416 ✔
369

370
// Record number of mesh grid points for closest rank
371
#pragma omp atomic
372
    num_points_per_rank[closest_rank]++;
326,416 ✔
373
  }
374
}
2,792 ✔
375

376
Position DecompositionMap::calculate_centroids(
5,584 ✔
377
  const Position position_sum, const int num_points, int rank)
378
{
379

380
  // check if any points have been recorded in rank
381
  if (num_points == 0) {
5,584 !
382
    fatal_error("Rank " + std::to_string(rank) +
×
383
                " has no Voronoi cell points. This indicates that the number "
384
                "of grid points for the Voronoi tesselation is too coarse. "
385
                "Requires source code modificaiton to fix.");
386
  }
387

388
  Position centroid = position_sum;
5,584 ✔
389

390
  // Divide by number of points
391
  double n = static_cast<double>(num_points);
5,584 ✔
392
  centroid.x /= n;
5,584 ✔
393
  centroid.y /= n;
5,584 ✔
394
  centroid.z /= n;
5,584 ✔
395

396
  return centroid;
5,584 ✔
397
}
398

399
bool DecompositionMap::any_discovered_source_regions(
8,256 ✔
400
  ParallelMap<SourceRegionKey, SourceRegion, SourceRegionKey::HashFunctor>&
401
    discovered_source_regions)
402
{
403

404
  simulation::time_decomposition_handling.start();
8,256 ✔
405

406
  int flag = 0;
8,256 ✔
407
  if (discovered_source_regions.begin() != discovered_source_regions.end()) {
8,256 ✔
408
    flag = 1;
725 ✔
409
  }
410

411
  MPI_Allreduce(MPI_IN_PLACE, &flag, 1, MPI_INT, MPI_MAX, mpi::intracomm);
8,256 ✔
412

413
  simulation::time_decomposition_handling.stop();
8,256 ✔
414

415
  return flag > 0;
8,256 ✔
416
}
417

418
void DecompositionMap::exchange_sr_info(
776 ✔
419
  ParallelMap<SourceRegionKey, SourceRegion, SourceRegionKey::HashFunctor>&
420
    discovered_source_regions)
421
{
422

423
  // Communicate maps
424
  for (int rank = 0; rank < mpi::n_procs; rank++) {
2,328 ✔
425

426
    // Send size
427
    uint64_t bcast_size = 0;
1,552 ✔
428
    if (rank == mpi::rank) {
1,552 ✔
429
      for (const auto& pair : discovered_source_regions) {
991,362 ✔
430
        // Only broadcast source regions that have non-zero volume, i.e. regions
431
        // discovered during active phase of ray
432
        if (pair.second.scalars_.volume_ > 0.0) {
494,905 ✔
433
          bcast_size++;
473,007 ✔
434
        }
435
      }
436
    }
437

438
    MPI_Bcast(&bcast_size, 1, MPI_UINT64_T, rank, mpi::intracomm);
1,552 ✔
439

440
    if (bcast_size > 0) {
1,552 ✔
441

442
      vector<int64_t> local_base_ids(bcast_size);
1,382 ✔
443
      vector<int64_t> local_mesh_bins(bcast_size);
1,382 ✔
444

445
      if (rank == mpi::rank) {
1,382 ✔
446
        // fill in vectors to be sent
447
        int i = 0;
691 ✔
448
        for (const auto& pair : discovered_source_regions) {
991,096 ✔
449
          if (pair.second.scalars_.volume_ > 0.0) {
494,857 ✔
450
            SourceRegionKey sr_key = pair.first;
473,007 ✔
451
            local_base_ids[i] = sr_key.base_source_region_id;
473,007 ✔
452
            local_mesh_bins[i] = sr_key.mesh_bin;
473,007 ✔
453
            i++;
473,007 ✔
454
          }
455
        }
456
      }
457

458
      // Broadcast all data
459
      MPI_Bcast(
1,382 ✔
460
        local_base_ids.data(), bcast_size, MPI_INT64_T, rank, mpi::intracomm);
1,382 ✔
461
      MPI_Bcast(
1,382 ✔
462
        local_mesh_bins.data(), bcast_size, MPI_INT64_T, rank, mpi::intracomm);
1,382 ✔
463

464
      // Update subdomain map
465
      for (int j = 0; j < bcast_size; j++) {
947,396 ✔
466

467
        SourceRegionKey sr_key(local_base_ids[j], local_mesh_bins[j]);
946,014 ✔
468

469
        // check if already in map or not, i.e. has someone else discovered that
470
        // region already?
471
        if (subdomain_map_.find(sr_key) == subdomain_map_.end()) {
946,014 ✔
472
          subdomain_map_[sr_key] = rank;
944,988 ✔
473
        } else {
474
          int resident_rank = subdomain_map_[sr_key]; // current owner
1,026 ✔
475
          int challenger_rank = rank; // current broadcasting rank
1,026 ✔
476

477
          double ratio_resident = calculate_load_ratio(resident_rank);
1,026 ✔
478
          double ratio_challenger = calculate_load_ratio(challenger_rank);
1,026 ✔
479
          double resident_rank_load =
1,026 ✔
480
            ratio_resident * estimated_rank_load_totals_[resident_rank];
1,026 ✔
481
          double challenger_rank_load =
1,026 ✔
482
            ratio_challenger * estimated_rank_load_totals_[challenger_rank];
1,026 ✔
483

484
          // If load of challenger rank is lower, assign source region to that
485
          // rank, otherwise resident keeps it
486
          int sender;
1,026 ✔
487
          int receiver;
1,026 ✔
488
          if (challenger_rank_load < resident_rank_load) {
1,026 ✔
489
            subdomain_map_[sr_key] = challenger_rank;
662 ✔
490
            sender = resident_rank;
662 ✔
491
            receiver = challenger_rank;
662 ✔
492
          } else {
493
            // resident keeps it
494
            sender = challenger_rank;
495
            receiver = resident_rank;
496
          }
497

498
          // Broadcast load of exchanged source region such that each rank can
499
          // update load balance
500
          double bcast_load = 0;
1,026 ✔
501
          if (mpi::rank == sender) {
1,026 ✔
502
            SourceRegion& contested_sr = discovered_source_regions[sr_key];
513 ✔
503
            double volume_sr = contested_sr.scalars_.volume_;
513 ✔
504
            bcast_load =
513 ✔
505
              C1_ *
513 ✔
506
                (static_cast<double>(contested_sr.scalars_.n_hits_) /
513 ✔
507
                  simulation::current_batch) *
513 ✔
508
                negroups_ +
513 ✔
509
              volume_sr * ray_tracing_cost_[sr_key.base_source_region_id];
513 ✔
510
          }
511
          MPI_Bcast(&bcast_load, 1, MPI_DOUBLE, sender, mpi::intracomm);
1,026 ✔
512

513
          double load_change_fraction =
1,026 ✔
514
            bcast_load / estimated_load_sum_; // load fraction update
1,026 ✔
515
          estimated_rank_load_fractions_[sender] -= load_change_fraction;
1,026 ✔
516
          estimated_rank_load_fractions_[receiver] += load_change_fraction;
1,026 ✔
517
          double load_change_total = bcast_load; // load total update
1,026 ✔
518
          estimated_rank_load_totals_[sender] -= load_change_total;
1,026 ✔
519
          estimated_rank_load_totals_[receiver] += load_change_total;
1,026 ✔
520

521
          // Communicate source region data and merge on receiver side
522
          if (mpi::rank == sender) {
1,026 ✔
523
            SourceRegion& sr = discovered_source_regions[sr_key];
513 ✔
524
            send_sr_data(receiver, sr);
513 ✔
525

526
            // clear old source region data from discovered regions map
527
            discovered_source_regions.erase(sr_key);
513 ✔
528
          }
529
          if (mpi::rank == receiver) {
1,026 ✔
530
            SourceRegion& sr = discovered_source_regions[sr_key];
513 ✔
531
            SourceRegion sr_recv(negroups_, is_linear_);
513 ✔
532
            receive_sr_data(sender, sr_recv);
513 ✔
533

534
            sr.merge(sr_recv, is_linear_);
513 ✔
535
          }
513 ✔
536
        }
537
      }
538
    }
1,382 ✔
539
  }
540
}
776 ✔
541

542
//==============================================================================
543
// Source region transfer between ranks
544
//
545
// A source region is moved as one fixed-size record of raw bytes (the scalar
546
// fields, then the group-wise arrays in a fixed order). The record layout and
547
// size depend only on run-wide settings (energy groups, linear source, source
548
// gradient limiter, adaptive volume estimator, fixed source mode), so sender
549
// and receiver agree on it without any metadata. When adding a per-region
550
// field, add it to ScalarSourceRegionFields (carried automatically) or to
551
// transfer_sr_fields() (and allocate it in the SourceRegion constructors).
552
//==============================================================================
553

554
// Start receiving `bytes` bytes from rank `sender` into `buf`, as messages of
555
// at most MAX_MPI_MESSAGE_BYTES. The sender splits its buffer the same way
556
// (start_chunked_send), and MPI delivers the pieces in the order they were
557
// sent.
558
void start_chunked_receive(
857 ✔
559
  char* buf, size_t bytes, int sender, vector<MPI_Request>& requests)
560
{
561
  for (size_t off = 0; off < bytes; off += MAX_MPI_MESSAGE_BYTES) {
1,714 ✔
562
    int n = static_cast<int>(std::min(MAX_MPI_MESSAGE_BYTES, bytes - off));
857 !
563
    requests.emplace_back();
857 ✔
564
    MPI_Irecv(
857 ✔
565
      buf + off, n, MPI_BYTE, sender, 1, mpi::intracomm, &requests.back());
857 ✔
566
  }
567
}
857 ✔
568

569
// Start sending `bytes` bytes of `buf` to rank `receiver`, as messages of at
570
// most MAX_MPI_MESSAGE_BYTES
571
void start_chunked_send(
857 ✔
572
  char* buf, size_t bytes, int receiver, vector<MPI_Request>& requests)
573
{
574
  for (size_t off = 0; off < bytes; off += MAX_MPI_MESSAGE_BYTES) {
1,714 ✔
575
    int n = static_cast<int>(std::min(MAX_MPI_MESSAGE_BYTES, bytes - off));
857 !
576
    requests.emplace_back();
857 ✔
577
    MPI_Isend(
857 ✔
578
      buf + off, n, MPI_BYTE, receiver, 1, mpi::intracomm, &requests.back());
857 ✔
579
  }
580
}
857 ✔
581

582
// Copy the `n` bytes of one field into the record at `offset` (PACK) or out of
583
// it (UNPACK), or only count them (SIZE), and advance the offset
584
void transfer_bytes(
721,220 ✔
585
  void* field, size_t n, char* buf, size_t& offset, SRTransfer mode)
586
{
587
  if (mode == SRTransfer::PACK) {
721,220 ✔
588
    std::memcpy(buf + offset, field, n);
359,334 ✔
589
  } else if (mode == SRTransfer::UNPACK) {
361,886 ✔
590
    std::memcpy(field, buf + offset, n);
359,334 ✔
591
  }
592
  offset += n;
721,220 ✔
593
}
721,220 ✔
594

595
// The same for one group-wise array, after checking that it has the expected
596
// number of entries (a field that is transferred but not allocated by the
597
// SourceRegion constructors would otherwise be read or written out of bounds)
598
template<typename T>
599
void transfer_array(
626,544 !
600
  vector<T>& v, size_t n, char* buf, size_t& offset, SRTransfer mode)
601
{
602
  if (v.size() != n) {
626,544 !
UNCOV
603
    fatal_error(fmt::format(
×
604
      "A transferred source region array has {} entries instead of {}. Check "
605
      "that the SourceRegion constructors allocate every field listed in "
606
      "DecompositionMap::transfer_sr_fields().",
NEW
607
      v.size(), n));
×
608
  }
609
  transfer_bytes(v.data(), n * sizeof(T), buf, offset, mode);
626,544 ✔
610
}
626,544 ✔
611

612
size_t DecompositionMap::transfer_sr_fields(
94,400 ✔
613
  SourceRegion& sr, char* buf, SRTransfer mode) const
614
{
615
  // The one list of what moves with a source region between ranks, in record
616
  // order. Scalars ride along in ScalarSourceRegionFields.
617
  size_t offset = 0;
94,400 ✔
618
  size_t ng = negroups_;
94,400 ✔
619
  transfer_bytes(
94,400 ✔
620
    &sr.scalars_, sizeof(ScalarSourceRegionFields), buf, offset, mode);
94,400 ✔
621
  transfer_array(sr.scalar_flux_old_, ng, buf, offset, mode);
94,400 ✔
622
  transfer_array(sr.scalar_flux_new_, ng, buf, offset, mode);
94,400 ✔
623
  transfer_array(sr.source_, ng, buf, offset, mode);
94,400 ✔
624
  if (settings::run_mode == RunMode::FIXED_SOURCE)
94,400 ✔
625
    transfer_array(sr.external_source_, ng, buf, offset, mode);
90,294 ✔
626
  transfer_array(sr.scalar_flux_final_, ng, buf, offset, mode);
94,400 ✔
627
  if (is_linear_) {
94,400 ✔
628
    transfer_array(sr.source_gradients_, ng, buf, offset, mode);
30,598 ✔
629
    transfer_array(sr.flux_moments_old_, ng, buf, offset, mode);
30,598 ✔
630
    transfer_array(sr.flux_moments_new_, ng, buf, offset, mode);
30,598 ✔
631
    transfer_array(sr.flux_moments_t_, ng, buf, offset, mode);
30,598 ✔
632
  }
633
  if (is_linear_ && FlatSourceDomain::source_gradient_limiter_)
94,400 ✔
634
    transfer_bytes(&sr.extent_, sizeof(BoundingBox), buf, offset, mode);
276 ✔
635
  if (is_adaptive_)
94,400 ✔
636
    transfer_array(sr.scalar_flux_t_, ng, buf, offset, mode);
36,258 ✔
637

638
  // The size of the record in bytes
639
  return offset;
94,400 ✔
640
}
641

642
void DecompositionMap::send_sr_data(int receiver, SourceRegion& sr_send)
513 ✔
643
{
644
  vector<char> buf(sr_record_bytes_);
513 ✔
645
  transfer_sr_fields(sr_send, buf.data(), SRTransfer::PACK);
513 ✔
646
  MPI_Send(buf.data(), static_cast<int>(buf.size()), MPI_BYTE, receiver, 1,
513 ✔
647
    mpi::intracomm);
648
}
513 ✔
649

650
void DecompositionMap::receive_sr_data(int sender, SourceRegion& sr_recv)
513 ✔
651
{
652
  vector<char> buf(sr_record_bytes_);
513 ✔
653
  MPI_Recv(buf.data(), static_cast<int>(buf.size()), MPI_BYTE, sender, 1,
513 ✔
654
    mpi::intracomm, MPI_STATUS_IGNORE);
655
  transfer_sr_fields(sr_recv, buf.data(), SRTransfer::UNPACK);
513 ✔
656
}
513 ✔
657

658
int DecompositionMap::find_owner(SourceRegionKey sr_key, Position r,
278,361,141 ✔
659
  ParallelMap<SourceRegionKey, SourceRegion, SourceRegionKey::HashFunctor>&
660
    discovered_source_regions)
661
{
662

663
  // Check if source region key is in subdomain map
664
  auto it = subdomain_map_.find(sr_key);
278,361,141 ✔
665
  if (it != subdomain_map_.end()) {
278,361,141 ✔
666
    return it->second;
268,399,269 ✔
667
  }
668

669
  // Check if already recorded in newly discovered source regions
670
  discovered_source_regions.lock(sr_key);
9,961,872 ✔
671
  bool sr_key_discovered = discovered_source_regions.contains(sr_key);
9,961,872 ✔
672
  discovered_source_regions.unlock(sr_key);
9,961,872 ✔
673
  if (sr_key_discovered) {
9,961,872 ✔
674
    return mpi::rank;
9,103,702 ✔
675
  }
676

677
  // If not found in either map, check which rank owns source
678
  // region beased on location
679
  int closest_rank = find_closest_rank(r, true);
858,170 ✔
680
  return closest_rank;
681
}
682

683
int DecompositionMap::find_closest_rank(Position r, bool test_all_ranks)
18,108,568 ✔
684
{
685

686
  int closest_rank = C_NONE;
18,108,568 ✔
687
  double min_distance = INFTY;
18,108,568 ✔
688

689
  // Distance function corresponding to weighted power Voronoi diagram. This
690
  // runs inside the transport loop (via find_owner) and inside the load
691
  // balancing iteration, so it deliberately allocates nothing.
692
  auto test_rank = [&](int rank) {
36,217,136 ✔
693
    double dist = (r - rank_centers_[rank]).norm();
36,217,136 ✔
694
    dist = dist * dist - rank_weights_[rank];
36,217,136 ✔
695
    if (dist < min_distance) {
36,217,136 ✔
696
      min_distance = dist;
32,168,648 ✔
697
      closest_rank = rank;
32,168,648 ✔
698
    }
699
  };
54,325,704 ✔
700

701
  if (test_all_ranks) {
18,108,568 ✔
702
    for (int rank = 0; rank < mpi::n_procs; rank++) {
22,382,949 ✔
703
      test_rank(rank);
14,921,966 ✔
704
    }
705
  } else {
706
    // Only the recorded neighbors, plus this rank itself
707
    for (int rank : mpi::decomp_map.my_neighbors_) {
21,295,170 ✔
708
      test_rank(rank);
10,647,585 ✔
709
    }
710
    test_rank(mpi::rank);
10,647,585 ✔
711
  }
712

713
  if (closest_rank == C_NONE) {
18,108,568 !
714
    fatal_error(
×
715
      "Could not find closest rank for new source region at position (" +
×
716
      std::to_string(r.x) + ", " + std::to_string(r.y) + ", " +
×
717
      std::to_string(r.z) + ").");
×
718
  }
719

720
  return closest_rank;
18,108,568 ✔
721
}
722

723
void DecompositionMap::calculate_rank_load(
1,640 ✔
724
  FlatSourceDomain* domain, double batch_transport_time)
725
{
726

727
  // Reset local volumes of base source regions, which might change when source
728
  // regions change rank ownership
729
  std::fill(volume_base_sr_.begin(), volume_base_sr_.end(), 0.0);
1,640 ✔
730

731
  // Add volumes of newly discovered source regions
732
  for (const auto& [sr_key, sr] : domain->discovered_source_regions_) {
497,473 ✔
733
    volume_base_sr_[sr_key.base_source_region_id] += sr.scalars_.volume_;
494,193 ✔
734
  }
735

736
  // Add volumes of known source regions
737
  for (int64_t sr = 0; sr < domain->n_source_regions(); sr++) {
2,200,910 ✔
738
    SourceRegionKey sr_key = domain->source_regions_.key(sr);
2,199,270 ✔
739
    uint64_t base_sr = sr_key.base_source_region_id;
2,199,270 ✔
740
    volume_base_sr_[base_sr] += domain->source_regions_.volume_t(sr);
2,199,270 ✔
741
    volume_base_sr_[base_sr] += domain->source_regions_.volume(sr);
2,199,270 ✔
742
  }
743

744
// Calculate ray tracing cost per base source region
745
#pragma omp parallel for
820 ✔
746
  for (uint64_t bsr = 0; bsr < n_base_sr_; bsr++) {
820,450 ✔
747
    if (volume_base_sr_[bsr] > 0.0) {
819,630 ✔
748
      ray_tracing_cost_[bsr] =
405,014 ✔
749
        (C2_ * static_cast<double>(num_base_source_region_RT_[bsr]) +
405,014 ✔
750
          C3_ * static_cast<double>(num_mesh_bin_RT_[bsr])) /
405,014 ✔
751
        volume_base_sr_[bsr];
405,014 ✔
752
    } else {
753
      ray_tracing_cost_[bsr] = 0.0;
414,616 ✔
754
    }
755
  }
756

757
  // Accumulate load of known source regions
758
  double local_estimated_load = 0.0;
1,640 ✔
759
#pragma omp parallel for reduction(+ : local_estimated_load)
820 ✔
760
  for (int64_t sr = 0; sr < domain->n_source_regions(); sr++) {
1,100,455 ✔
761
    SourceRegionKey sr_key = domain->source_regions_.key(sr);
1,099,635 ✔
762
    uint64_t base_sr = sr_key.base_source_region_id;
1,099,635 ✔
763

764
    // Calculate volume of unique source region to weight volume-dependent ray
765
    // tracing cost
766
    double volume_sr =
1,099,635 ✔
767
      domain->source_regions_.volume_t(sr) + domain->source_regions_.volume(sr);
1,099,635 ✔
768

769
    // Calculate load of source region
770
    double load_sr =
1,099,635 ✔
771
      C1_ *
1,099,635 ✔
772
        (static_cast<double>(domain->source_regions_.n_hits(sr)) /
1,099,635 ✔
773
          simulation::current_batch) *
1,099,635 ✔
774
        negroups_ +
1,099,635 ✔
775
      volume_sr * ray_tracing_cost_[base_sr];
1,099,635 ✔
776

777
    // Accumulate to local estimated load
778
    local_estimated_load += load_sr;
1,099,635 ✔
779
  }
780

781
  // Accumulate load of newly discovered source regions
782
  for (const auto& [sr_key, sr] : domain->discovered_source_regions_) {
497,473 ✔
783
    uint64_t base_sr = sr_key.base_source_region_id;
494,193 ✔
784
    double volume_sr = sr.scalars_.volume_;
494,193 ✔
785
    double load_sr =
494,193 ✔
786
      C1_ *
494,193 ✔
787
        (static_cast<double>(sr.scalars_.n_hits_) / simulation::current_batch) *
494,193 ✔
788
        negroups_ +
494,193 ✔
789
      volume_sr * ray_tracing_cost_[base_sr];
494,193 ✔
790
    local_estimated_load += load_sr;
494,193 ✔
791
  }
792

793
  // Communicate estimated load across ranks
794
  MPI_Allgather(&local_estimated_load, 1, MPI_DOUBLE,
1,640 ✔
795
    estimated_rank_load_totals_.data(), 1, MPI_DOUBLE, mpi::intracomm);
1,640 ✔
796
  estimated_load_sum_ = std::accumulate(estimated_rank_load_totals_.begin(),
1,640 ✔
797
    estimated_rank_load_totals_.end(), 0.0);
798

799
  // Communicate measured load across ranks
800
  MPI_Allgather(&batch_transport_time, 1, MPI_DOUBLE,
1,640 ✔
801
    measured_rank_load_fractions_.data(), 1, MPI_DOUBLE, mpi::intracomm);
1,640 ✔
802
  double measured_load_sum =
1,640 ✔
803
    std::accumulate(measured_rank_load_fractions_.begin(),
1,640 ✔
804
      measured_rank_load_fractions_.end(), 0.0);
805

806
  // Calculate fractions
807
  for (int rank = 0; rank < mpi::n_procs; rank++) {
4,920 ✔
808
    estimated_rank_load_fractions_[rank] =
3,280 ✔
809
      estimated_rank_load_totals_[rank] / estimated_load_sum_;
3,280 ✔
810
    measured_rank_load_fractions_[rank] =
3,280 ✔
811
      measured_rank_load_fractions_[rank] / measured_load_sum;
3,280 ✔
812
  }
813

814
  // Reset ray trace counters
815
  fill(num_base_source_region_RT_.begin(), num_base_source_region_RT_.end(), 0);
1,640 ✔
816
  fill(num_mesh_bin_RT_.begin(), num_mesh_bin_RT_.end(), 0);
1,640 ✔
817
}
1,640 ✔
818

819
void DecompositionMap::balance_load(FlatSourceDomain* domain)
1,640 ✔
820
{
821

822
  // Optimization parameters
823
  int max_iterations = 200;
1,640 ✔
824
  int it_outer = 0;
1,640 ✔
825
  double adaptation_factor = 1;
1,640 ✔
826
  double min_adaptation_factor = 0.01;
1,640 ✔
827
  double max_adaptation_factor = 2;
1,640 ✔
828
  double avg_rank_distance =
1,640 ✔
829
    max_domain_length_ /
1,640 ✔
830
    cbrt(mpi::n_procs); // rough estimate of average distance between ranks
1,640 ✔
831
  double weight_scale =
1,640 ✔
832
    avg_rank_distance * avg_rank_distance * optimization_history_factor_;
1,640 ✔
833
  double beta = 0.6; // momentum damping
1,640 ✔
834
  bool check_all_ranks = true;
1,640 ✔
835

836
  vector<double> weight_change(mpi::n_procs, 0.0);
1,640 ✔
837
  vector<double> combined_rank_load(mpi::n_procs, 0.0);
1,640 ✔
838
  vector<double> load_ratio(mpi::n_procs, 0.0);
1,640 ✔
839

840
  // Combine estimated load with measured load ratios
841
  for (int rank = 0; rank < mpi::n_procs; rank++) {
4,920 ✔
842
    load_ratio[rank] = calculate_load_ratio(rank);
3,280 ✔
843
    combined_rank_load[rank] =
3,280 ✔
844
      load_ratio[rank] * estimated_rank_load_totals_[rank];
3,280 ✔
845
  }
846

847
  double combined_load_sum =
1,640 ✔
848
    std::accumulate(combined_rank_load.begin(), combined_rank_load.end(), 0.0);
1,640 ✔
849

850
  for (int rank = 0; rank < mpi::n_procs; rank++) {
4,920 ✔
851
    combined_rank_load[rank] = (combined_rank_load[rank] / combined_load_sum);
3,280 ✔
852
  }
853
  double max_load =
1,640 !
854
    *std::max_element(combined_rank_load.begin(), combined_rank_load.end());
1,640 !
855
  double max_imbalance = (max_load - target_load_) / target_load_;
1,640 ✔
856
  double prev_imbalance = max_imbalance;
1,640 ✔
857

858
  // History tracking
859
  vector<double> imbalance_history;
1,640 ✔
860
  vector<vector<double>> weight_history;
1,640 ✔
861
  imbalance_history.push_back(max_imbalance);
1,640 ✔
862
  weight_history.push_back(rank_weights_);
1,640 ✔
863

864
  // Change weights to equalize load based on combined load estimates
865
  while (max_imbalance > imbalance_tolerance_ && it_outer < max_iterations) {
43,128 ✔
866

867
    it_outer++;
41,488 ✔
868

869
    for (int rank = 0; rank < mpi::n_procs; rank++) {
124,464 ✔
870
      double corr = ((combined_rank_load[rank] - target_load_) / target_load_) *
82,976 !
871
                    weight_scale;
82,976 ✔
872
      weight_change[rank] =
82,976 ✔
873
        beta * weight_change[rank] +
82,976 ✔
874
        (1.0 - beta) * corr; // keep some inertia from previous changes to
82,976 ✔
875
                             // prevent oscillations
876
      double damping = std::clamp(max_imbalance / prev_imbalance, 0.1,
165,952 ✔
877
        1.0); // dampening factor based on whether we are getting closer to
82,976 !
878
              // convergence or not, prevents big jumps, if too big a change,
879
              // more dampening is applied
880
      rank_weights_[rank] -= adaptation_factor * damping * weight_change[rank];
82,976 ✔
881
    }
882

883
    if (simulation::current_batch > 1) {
41,488 ✔
884
      // Check distances to all ranks only in first batch, otherwise only
885
      // recorded neighbors
886
      check_all_ranks = false;
31,718 ✔
887
    }
888

889
    // Calculate new load after weight update
890
    update_load(domain, check_all_ranks, combined_rank_load, load_ratio);
41,488 ✔
891
    max_load =
41,488 !
892
      *std::max_element(combined_rank_load.begin(), combined_rank_load.end());
41,488 !
893
    max_imbalance = (max_load - target_load_) / target_load_;
41,488 ✔
894

895
    // Store imbalance history
896
    imbalance_history.push_back(max_imbalance);
41,488 ✔
897
    weight_history.push_back(rank_weights_);
41,488 ✔
898

899
    // Adaptive factor
900
    if (max_imbalance > prev_imbalance)
41,488 ✔
901
      adaptation_factor =
18,000 ✔
902
        std::max(adaptation_factor * 0.5, min_adaptation_factor);
13,234 ✔
903
    else
904
      adaptation_factor =
64,976 ✔
905
        std::min(adaptation_factor * 1.05, max_adaptation_factor);
32,830 ✔
906

907
    prev_imbalance = max_imbalance;
908
  }
909

910
  // Check convergence and adjust history optimization factor depending on
911
  // failure mode to enable better convergence in the following batch
912
  if (it_outer == max_iterations) {
1,640 ✔
913
    if (mpi::master) {
152 ✔
914
      warning("MPI load balancing has not converged after " +
228 ✔
915
              std::to_string(max_iterations) + " iterations.");
304 ✔
916
    }
917

918
    // Check if oscillating or simply slow convergence
919
    int direction_changes = 0;
920

921
    for (int i = 1; i < imbalance_history.size(); i++) {
30,552 ✔
922
      // Calculate change
923
      double change = imbalance_history[i] - imbalance_history[i - 1];
30,400 ✔
924

925
      // Count direction changes
926
      if (i > 1) {
30,400 ✔
927
        double prev_change =
30,248 ✔
928
          imbalance_history[i - 1] - imbalance_history[i - 2];
30,248 ✔
929
        if (change * prev_change < 0) {
30,248 ✔
930
          direction_changes++;
5,634 ✔
931
        }
932
      }
933
    }
934

935
    // Check for oscillations
936
    double oscillation_ratio =
152 ✔
937
      (double)direction_changes / (imbalance_history.size() - 2);
152 ✔
938

939
    if (oscillation_ratio > 0.4) {
152 ✔
940
      // decrease weight for next batch if oscillating
941
      optimization_history_factor_ =
8 ✔
942
        std::max(optimization_history_factor_ * 0.5, min_adaptation_factor);
4 !
943
    } else {
944
      // increase weight for faster convergence if too slow
945
      optimization_history_factor_ =
296 ✔
946
        std::min(optimization_history_factor_ * 1.2, max_adaptation_factor);
156 ✔
947
    }
948

949
    // Check which iteration had the best imbalance and revert to those weights
950
    auto min_it =
152 !
951
      std::min_element(imbalance_history.begin(), imbalance_history.end());
152 !
952
    int best_index = std::distance(imbalance_history.begin(), min_it);
152 ✔
953
    double best_imbalance = *min_it;
152 ✔
954
    rank_weights_ = weight_history[best_index];
152 ✔
955

956
    write_message(6,
152 ✔
957
      "Best imbalance during optimization was {:.2f}% at iteration {}.",
958
      best_imbalance * 100.0, best_index);
152 ✔
959

960
    if (best_index == 0) {
152 ✔
961
      // if no improvement at all, just keep current decomposition and return
962
      // without redistributing
963
      return;
56 ✔
964
    }
965
  } else {
966
    optimization_history_factor_ = 1.0; // reset history factor if converged
1,488 ✔
967
    write_message(6,
1,488 ✔
968
      "MPI load balancing converged after {} iterations. Max. imbalance: "
969
      "{:.2f}%",
970
      it_outer, max_imbalance * 100.0);
2,976 ✔
971
  }
972

973
  // Redistribute source regions according to new weights determined in
974
  // optimization
975
  redistribute_source_regions(domain);
1,584 ✔
976
}
1,640 ✔
977

978
void DecompositionMap::update_load(FlatSourceDomain* domain,
41,488 ✔
979
  bool check_all_ranks, vector<double>& combined_rank_load,
980
  vector<double>& load_ratio)
981
{
982

983
  vector<double> load(mpi::n_procs, 0);
41,488 ✔
984

985
// Add up source region hits to respective rank depending of position of
986
// centroid of each source region
987
#pragma omp parallel
28,996 ✔
988
  {
12,492 ✔
989
    // number of hits per thread
990
    vector<double> thread_load(mpi::n_procs, 0);
12,492 ✔
991

992
#pragma omp for
993
    for (int64_t sr = 0; sr < domain->n_source_regions(); sr++) {
6,701,906 ✔
994
      Position centroid = domain->source_regions_.centroid(sr);
6,689,414 ✔
995
      int owner = find_closest_rank(centroid, check_all_ranks);
6,689,414 ✔
996
      double volume_sr = domain->source_regions_.volume_t(sr);
6,689,414 ✔
997
      thread_load[owner] +=
6,689,414 ✔
998
        load_ratio[owner] *
6,689,414 ✔
999
        (C1_ *
6,689,414 ✔
1000
            (static_cast<double>(domain->source_regions_.n_hits(sr)) /
6,689,414 ✔
1001
              simulation::current_batch) *
6,689,414 ✔
1002
            negroups_ +
6,689,414 ✔
1003
          volume_sr * ray_tracing_cost_[domain->source_regions_.key(sr)
6,689,414 ✔
1004
                                          .base_source_region_id]);
6,689,414 ✔
1005
    }
1006

1007
// Combine results from different threads
1008
#pragma omp critical(combining_loads)
1009
    {
1010
      for (int i = 0; i < mpi::n_procs; i++) {
37,476 ✔
1011
        load[i] += thread_load[i];
24,984 ✔
1012
      }
1013
    }
UNCOV
1014
  }
1015

1016
  // Communicate new load estimates across ranks
1017
  MPI_Allreduce(MPI_IN_PLACE, load.data(), mpi::n_procs, MPI_DOUBLE, MPI_SUM,
41,488 ✔
1018
    mpi::intracomm);
1019
  double load_sum = std::accumulate(load.begin(), load.end(), 0.0);
41,488 ✔
1020

1021
  // Update new combined rank load fractions
1022
  for (int rank = 0; rank < mpi::n_procs; rank++) {
124,464 ✔
1023
    combined_rank_load[rank] = load[rank] / load_sum;
82,976 ✔
1024
  }
1025
}
41,488 ✔
1026

1027
void DecompositionMap::redistribute_source_regions(FlatSourceDomain* domain)
1,584 ✔
1028
{
1029

1030
  // Map of source regions to be sent to other ranks
1031
  std::unordered_map<int, vector<int>> sr_send_list;
1,584 ✔
1032
  // Number of source regions to be received from other ranks
1033
  vector<int> num_sr_receiving(mpi::n_procs, 0);
1,584 ✔
1034

1035
  // Local source region container that contains updated list
1036
  SourceRegionContainer source_regions_new =
1,584 ✔
1037
    domain->source_regions_.empty_like();
1,584 ✔
1038

1039
  // Each rank identifies source regions that need to be transferred to new
1040
  // owner and updates subdomain map accordingly
1041
  for (int64_t sr = 0; sr < domain->n_source_regions(); sr++) {
2,672,110 ✔
1042
    Position centroid = domain->source_regions_.centroid(sr);
2,670,526 ✔
1043
    int owner = find_closest_rank(centroid, true);
2,670,526 ✔
1044

1045
    // If owner changed, write source region to list of outbound source regions
1046
    if (owner != mpi::rank) {
2,670,526 ✔
1047
      sr_send_list[owner].push_back(sr);
46,523 ✔
1048
    }
1049
    // If owner did not change, add source region to new local container
1050
    else {
1051
      source_regions_new.push_back(
2,624,003 ✔
1052
        domain->source_regions_.get_source_region_handle(sr));
5,248,006 ✔
1053
    }
1054
  }
1055

1056
  // Each rank informs other ranks about ownership changes to update subdomain
1057
  // map
1058
  for (int rank = 0; rank < mpi::n_procs; rank++) {
4,752 ✔
1059

1060
    // Send size
1061
    int bcast_size = 0;
3,168 ✔
1062
    if (rank == mpi::rank) {
3,168 ✔
1063
      for (const auto& pair : sr_send_list) {
2,441 ✔
1064
        bcast_size += pair.second.size();
857 ✔
1065
      }
1066
    }
1067
    MPI_Bcast(&bcast_size, 1, MPI_INT, rank, mpi::intracomm);
3,168 ✔
1068

1069
    if (bcast_size > 0) {
3,168 ✔
1070
      vector<int> rank_ids(bcast_size);
1,714 ✔
1071
      vector<int64_t> base_ids(bcast_size);
1,714 ✔
1072
      vector<int64_t> mesh_bins(bcast_size);
1,714 ✔
1073

1074
      // Current owner prepares communication and fills vectors to be sent
1075
      if (rank == mpi::rank) {
1,714 ✔
1076
        int i = 0;
857 ✔
1077
        for (auto& pair : sr_send_list) {
1,714 ✔
1078

1079
          int receiver = pair.first; // new owner
857 ✔
1080
          vector<int>& sr_indices =
857 ✔
1081
            pair.second; // vector of source region indices
1082

1083
          // Iterate through all source regions for this key
1084
          for (int sr_idx : sr_indices) {
47,380 ✔
1085
            SourceRegionKey sr_key = domain->source_regions_.key(sr_idx);
46,523 ✔
1086
            rank_ids[i] = receiver;
46,523 ✔
1087
            base_ids[i] = sr_key.base_source_region_id;
46,523 ✔
1088
            mesh_bins[i] = sr_key.mesh_bin;
46,523 ✔
1089
            i++;
46,523 ✔
1090
          }
1091
        }
1092
      }
1093

1094
      // Broadcast source region data
1095
      MPI_Bcast(rank_ids.data(), bcast_size, MPI_INT, rank, mpi::intracomm);
1,714 ✔
1096
      MPI_Bcast(base_ids.data(), bcast_size, MPI_INT64_T, rank, mpi::intracomm);
1,714 ✔
1097
      MPI_Bcast(
1,714 ✔
1098
        mesh_bins.data(), bcast_size, MPI_INT64_T, rank, mpi::intracomm);
1,714 ✔
1099

1100
      // Every rank updates subdomain map
1101
      for (int j = 0; j < bcast_size; j++) {
94,760 ✔
1102

1103
        // Re-assemble source region key and extract new owner
1104
        SourceRegionKey sr_key(base_ids[j], mesh_bins[j]);
93,046 ✔
1105
        int rank_new = rank_ids[j];
93,046 ✔
1106

1107
        // Update subdomain_map with new owner
1108
        subdomain_map_[sr_key] = rank_new;
93,046 ✔
1109

1110
        // If calling rank is new owner, increase count of source regions coming
1111
        // from sending rank (current broadcaster)
1112
        if (mpi::rank == rank_new) {
93,046 ✔
1113
          num_sr_receiving[rank] += 1;
46,523 ✔
1114
        }
1115
      }
1116
    }
1,714 ✔
1117
  }
1118

1119
  // Clear source_region_map_
1120
  domain->source_region_map_.clear();
1,584 ✔
1121

1122
  // Move the source region data to the new owners: one packed buffer per
1123
  // pair of ranks (fixed-size records, see transfer_sr_fields()), sent in
1124
  // messages of at most MAX_MPI_MESSAGE_BYTES, instead of one message per
1125
  // field and region. Every rank already knows how many regions it receives
1126
  // from each sender (num_sr_receiving, from the broadcasts above), so all
1127
  // receives are posted first, then all sends, then one Waitall: no ordering
1128
  // between ranks is needed. The messages are few and large, which keeps MPI
1129
  // resource use small (posting tens of thousands of small messages made Open
1130
  // MPI's memory grow without bound).
1131
  if (num_sr_receiving[mpi::rank] > 0) {
1,584 !
NEW
1132
    fatal_error("Rank sends source regions to itself. Rank should not "
×
1133
                "receive source regions from itself.");
1134
  }
1135
  const size_t record = sr_record_bytes_;
1,584 ✔
1136
  vector<MPI_Request> requests;
1,584 ✔
1137

1138
  // Post receives
1139
  vector<vector<char>> recv_buffers(mpi::n_procs);
1,584 ✔
1140
  for (int sender = 0; sender < mpi::n_procs; sender++) {
4,752 ✔
1141
    if (num_sr_receiving[sender] == 0)
3,168 ✔
1142
      continue;
2,311 ✔
1143
    recv_buffers[sender].resize(num_sr_receiving[sender] * record);
857 ✔
1144
    start_chunked_receive(recv_buffers[sender].data(),
857 ✔
1145
      recv_buffers[sender].size(), sender, requests);
857 ✔
1146
  }
1147

1148
  // Pack and post sends
1149
  vector<vector<char>> send_buffers;
1,584 ✔
1150
  send_buffers.reserve(sr_send_list.size());
1,584 ✔
1151
  for (auto& pair : sr_send_list) {
2,441 ✔
1152
    int receiver = pair.first;             // destination rank
857 ✔
1153
    vector<int>& sr_indices = pair.second; // vector of source region indices
857 ✔
1154
    send_buffers.emplace_back(sr_indices.size() * record);
857 ✔
1155
    char* p = send_buffers.back().data();
857 ✔
1156
    for (int sr_idx : sr_indices) {
47,380 ✔
1157
      SourceRegion sr(domain->source_regions_.get_source_region_handle(sr_idx));
46,523 ✔
1158
      transfer_sr_fields(sr, p, SRTransfer::PACK);
46,523 ✔
1159
      p += record;
46,523 ✔
1160
    }
46,523 ✔
1161
    start_chunked_send(send_buffers.back().data(), send_buffers.back().size(),
857 ✔
1162
      receiver, requests);
1163
  }
1164

1165
  if (!requests.empty()) {
1,584 ✔
1166
    MPI_Waitall(
1,280 ✔
1167
      static_cast<int>(requests.size()), requests.data(), MPI_STATUSES_IGNORE);
1,280 ✔
1168
  }
1169
  send_buffers.clear();
1,584 ✔
1170

1171
  // Record starting source region ID for tally reinitialization later
1172
  int64_t start_sr_id = source_regions_new.n_source_regions();
1,584 ✔
1173

1174
  // Unpack, in order of sending rank (as the region order before)
1175
  for (int sender = 0; sender < mpi::n_procs; sender++) {
4,752 ✔
1176
    char* p = recv_buffers[sender].data();
3,168 ✔
1177
    for (int i = 0; i < num_sr_receiving[sender]; ++i) {
49,691 ✔
1178
      SourceRegion sr_recv(negroups_, is_linear_);
46,523 ✔
1179
      transfer_sr_fields(sr_recv, p, SRTransfer::UNPACK);
46,523 ✔
1180
      p += record;
46,523 ✔
1181
      source_regions_new.push_back(sr_recv);
46,523 ✔
1182
    }
46,523 ✔
1183
    vector<char>().swap(recv_buffers[sender]);
3,168 ✔
1184
  }
1185

1186
  // Update source regions in domain to new container
1187
  domain->source_regions_ = source_regions_new;
1,584 ✔
1188

1189
  // Update source region map
1190
  for (int64_t sr = 0; sr < domain->n_source_regions(); sr++) {
2,672,110 ✔
1191
    SourceRegionKey key = domain->source_regions_.key(sr);
2,670,526 ✔
1192
    domain->source_region_map_[key] = sr;
2,670,526 ✔
1193
  }
1194

1195
  // Reinitialise tallies
1196
  domain->convert_source_regions_to_tallies(start_sr_id);
1,584 ✔
1197
}
3,168 ✔
1198

1199
double DecompositionMap::calculate_load_ratio(int rank)
5,332 ✔
1200
{
1201
  if (estimated_rank_load_fractions_[rank] > 0.0) {
5,332 !
1202
    return measured_rank_load_fractions_[rank] /
5,332 ✔
1203
           estimated_rank_load_fractions_[rank];
5,332 ✔
1204
  } else {
1205
    return 1.0;
1206
  }
1207
}
1208

1209
} // namespace openmc
1210
#endif // OPENMC_MPI
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc