Skip to content

Random Ray Domain Decomposed Load Balancing MPI Optimization - #4246

Merged
paulromano merged 11 commits into
openmc-dev:developfrom
jtramm:rr-dd-packed-redistribution
Nov 27, 2026
Merged

paulromano merged 11 commits into
openmc-dev:developfrom
jtramm:rr-dd-packed-redistribution

Conversation

@jtramm

@jtramm jtramm commented Nov 19, 2026 •

Copy link
Copy Markdown
Contributor

Problem

The domain decomposed random ray solver (added in #4026) features a dynamic load balancing algorithm. After reach batch, if a load imbalance between ranks is detected, a source region exchange takes place wherein ownership of sources regions (and their accompanying data structures) can be transferred from one rank to another.

During this source region exchange phase, redistribute_source_regions sent each source region as ~10 separate small messages (one message for the scalar fields and a message for each vector field), all posted at once. On some models this can results in millions of messages in flight during the first bulk synchronous exchange when heavy load rebalancing occurs, even with just a few MPI ranks. In some cases this can cause the MPI implementation to do a lot of additional and highly inefficient buffering, causing slow performance and extreme memory usage.

This issue was first observed on an assembly problem by @nuclearkevin. He reported that the issue was so bad that the code appeared to hang. In my testing on that problem on my laptop, the code ran out of memory and was killed.

Fix

Source regions are now fully serialized into a combined, contiguous buffer before transmission. The redistribution sends the buffer per pair of ranks rather than 10 messages per source region x number of source regions. In theory we could have implemented this as a single message between each pair of ranks. However, in some cases extremely large numbers of SRs may be moved, which if sent as one giant message (potentially 10's of GB) might overwhelm some MPI implementations. As such, I've actually introduced a chunking send abstraction with that chops the buffer up into reasonable chunks to cap the message size. I've used a 32 MB chunk limit, which in my experience should work well for most MPI implementations.

Performance Results

17x17 assembly reproducer (~600k source regions) from @nuclearkevin, 20 batches, 1 thread per rank, as run on my laptop:

Ranks develop this PR
2 killed (out of memory) 152 s, 2.6 GB
8 killed (out of memory) 71 s, 4.0 GB
10 killed (out of memory) 107 s, 4.8 GB

We're not getting fantastic scaling here still but this is measuring just the first handful of iterations where communications and load balancing are most expensive. Further optimizations to the random ray domain decomposition implementation are in the works for later PRs.

AI Assistance

  • Harness: Claude Code
  • Model: Claude Opus 5.5 and Fable 5.2
  • Reasoning effort: max

Checklist

  • I have performed a self-review of my own code
  • I have run clang-format (version 18) on any C++ source files (if applicable)
  • I have followed the style guidelines for Python source files (if applicable)
  • I have made corresponding changes to the documentation (if applicable)
  • I have added tests that prove my fix is effective or that my feature works (if applicable)

Summary by CodeRabbit

  • New Features
    • Source-region data transfers are now handled in packed records. Large transfers are split into smaller messages, allowing redistribution to handle more data reliably.
  • Bug Fixes
    • Received source-region data is applied only after transfers are complete, and in sender order, preventing incomplete or out-of-order updates.

John Tramm added 9 commits November 18, 2026 15:03
… rank pair

redistribute_source_regions sent every source region as ~10 separate small
MPI_Isend messages (one per field), all posted at once, received field by
field with blocking MPI_Recv. A load balancing redistribution of 1e4-1e5
regions put 1e5-1e6 small messages in flight, which made Open MPI's memory
grow without bound (and the transfer slow) on large models.

A source region is now one fixed-size record of raw bytes. The fields that
move are listed once, in for_each_transfer_field(); the record size
(sr_record_bytes), pack_sr_data and unpack_sr_data all follow that list, and
each group-wise array is checked for its expected length when it is visited.
The redistribution pre-posts one receive per sending rank (counts are known
from the ownership broadcasts), sends one packed buffer per destination rank
and waits once; buffers above 1 GiB are split. The contested-region exchange
sends one message per region instead of up to 12. Received regions keep their
previous order, so results are unchanged.
The size was cached lazily on first use and never reset, so a later solve in
the same process with different settings (groups, source shape, estimator, run
mode) would have used a stale value. initialize() runs at the start of every
solve.
@coderabbitai

coderabbitai Bot commented Nov 19, 2026 •

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

Random-ray source-region transfers now use fixed-size packed records and chunked MPI communication. Initialization measures the configured record size. Redistribution posts receives before sends, waits for requests, and unpacks records in sender order.

Changes: Source-region transfer

Layer / File(s) Summary
Source-region record contract and layout
include/openmc/random_ray/decomposition_map.h, include/openmc/random_ray/source_region.h, src/random_ray/decomposition_map.cpp
SRProcessMode defines sizing, packing, and unpacking modes. Initialization measures the configured record size. The shared field processor handles scalar fields and applicable arrays. The field documentation now refers to process_sr_fields().
Packed and chunked MPI transfer
include/openmc/random_ray/flat_source_domain.h, include/openmc/random_ray/decomposition_map.h, src/random_ray/decomposition_map.cpp
Source-region fields are packed into byte records instead of separate field messages. MPI transfers use chunks capped at 32 MiB. Array transfers validate their expected lengths.
Redistribution receive, send, and unpack
src/random_ray/decomposition_map.cpp
Redistribution prepares per-destination buffers, posts receives before sends, and waits for all requests. It then unpacks records in sender order and rejects receive counts indicating that the rank receives regions from itself.

Estimated code review effort: 3 (Moderate) | ~25 minutes

Change: Refactor

Sequence Diagram(s)

sequenceDiagram
  participant SenderMap as sender DecompositionMap
  participant ReceiverMap as receiver DecompositionMap
  participant MPI
  ReceiverMap->>MPI: post chunked receives
  SenderMap->>SenderMap: pack destination source regions
  SenderMap->>MPI: send packed buffers in chunks
  MPI-->>ReceiverMap: complete posted requests
  ReceiverMap->>ReceiverMap: unpack records in sender order
Loading

Suggested reviewers: suark94

Merge Risk: Minimal · up to 36421

The change replaces per-field MPI messages with packed, chunked transfers for source-region redistribution. No actionable merge-blocking risk was found in this review.

Pre-merge checks: Passed 4

Check name Status Explanation
Title check Passed The title names the main change: load balancing for the domain-decomposed random ray solver is optimized through MPI changes during source-region redistribution. The PR serializes source regions into …
Description check Passed The description covers the problem, the fix, and performance results with a reproducer table. It includes the AI assistance disclosure and a completed checklist, matching the template structure. It ha…
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Full details: Title check

Explanation

The title names the main change: load balancing for the domain-decomposed random ray solver is optimized through MPI changes during source-region redistribution. The PR serializes source regions into contiguous buffers and sends them in chunks, which reduces MPI message count. The title is specific and matches the changeset and objectives, though it does not name the serialization or chunking mechanism.

Full details: Description check

Explanation

The description covers the problem, the fix, and performance results with a reproducer table. It includes the AI assistance disclosure and a completed checklist, matching the template structure. It has no 'Fixes #' issue reference, and the template's Description section heading is replaced by Problem and Fix subsections. These gaps are minor. The checklist marks tests as added, but the diff summary contains no test files, so that claim cannot be verified from the provided context.

Resolution

Consider adding a 'Fixes #' line or stating that no issue is linked. Confirm that the tests the checklist mentions are included in the PR, since the provided summaries list no test changes. Consider using the template's '# Description' heading.

Finishing Touches

Generate unit tests (beta)

  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 50d655b4-5f31-4e4f-8475-34217816e625
Commits

Reviewing files that changed from the base of the PR and between 6c6ca91 and 3642156.

Files selected for processing: 1

  • src/random_ray/decomposition_map.cpp

Included review availability: This review used your included allowance. Your plan provides up to 4 included reviews per hour; 3 remain after this review.

@Suark94 Suark94 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a neat way to make the exchange of source regions for load balancing more efficient! Thanks @jtramm !

Comment thread src/random_ray/decomposition_map.cpp Resolved
Comment thread src/random_ray/decomposition_map.cpp Resolved
…the transfer helpers private members of DecompositionMap

@Suark94 Suark94 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for addressing the comments!

Copy assignment duplicates every rebuilt array and keeps both containers
live during tally remapping. Move the rebuilt container into the domain
so remapping uses its original storage.

@paulromano paulromano left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great! I made a one-line change to take advantage of std::move on a temporary but otherwise no changes needed

@paulromano
paulromano enabled auto-merge (squash) November 27, 2026 18:12
@paulromano
paulromano merged commit 936c884 into openmc-dev:develop Nov 27, 2026
16 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants