Skip to content

Random Ray Domain Decomposed Ray Handoff Improvement - #4249

Open
jtramm wants to merge 4 commits into
openmc-dev:developfrom
jtramm:rr-dd-lossless-handoff
Open

jtramm wants to merge 4 commits into
openmc-dev:developfrom
jtramm:rr-dd-lossless-handoff

Conversation

@jtramm

@jtramm jtramm commented Nov 20, 2026 •

Copy link
Copy Markdown
Contributor

Summary

This PR makes a small change to how rays are passed across subdomain boundaries in the random ray domain decomposed MPI solver. The current develop branch results in inconsistent floating point rounding which I've noticed occasionally causes one of the CI tests to fail. This PR ensures much stronger floating point consistency (by ensuring the exact same ray tracing operations are performed regardless of where subdomain boundaries are) so the CI test should pass cleanly now.

The Problem

In random ray domain decomposition, a ray that leaves a rank's subdomain between two bins of a source region mesh is moved to that point, and the receiving rank starts a new ray trace from inside the cell. That trace is not the one made when the ray entered the cell (its first segment is TINY_BIT shorter), so results depend on where the subdomain boundaries are. Load balancing sets those from measured wall time, so results change from run to run, which makes tests with a source region mesh potentially subject to different roundoffs under MPI.

The Fix

The ray is no longer moved. It is buffered in the state it had on entry to the cell, with the number of segments it has been attenuated over since. The receiving rank traces the cell again from that state and skips those segments, so every segment is the same as without domain decomposition. This replaces the partial-length bookkeeping and the decomposed-only rule that skipped segments shorter than TINY_BIT. Results without MPI are unchanged.

I also considered just trying to correct for the differences with TINY_BIT etc, but I've found it is generally just not a great idea to "interfere" with OpenMC's default cell-to-cell ray tracer (as we are currently doing in develop) by stopping the ray part way between cell surfaces and then doing another shorter length ray trace, as this will inevitably result in different numbers and rounding compared to the serial case with no subdomain boundaries.

Testing

weightwindows_fw_cadis_mesh model on 2 to 8 ranks, largest relative difference of the weight windows compared to a serial run without MPI:

develop this PR
flat source 6e-7 to 2e-5 1e-13
linear source 1e-6 to 5e-6 3e-11

As you can see above, the floating point difference vs serial is reduced by many orders of magnitude, which makes CI testing much easier and reliable. Random ray and FW-CADIS regression tests give the same results as develop without MPI and on 2 ranks.

Note that on 4 ranks develop can also abort on this model with a lost ray. That is a separate issue already fixed in #4247, so it is not a motivation for this PR.

Potential for Runtime Cost Increases

There is going to be some amount of repeated ray tracing work that gets performed here. While OpenMC top level ray tracing (from one material filled cell instance surface to the next) isn't going to ever be redone, cell-under-voxel ray tracing operations inside of a material filled cell instance will be performed again. Typically this is not very expensive (especially if a structured mesh is used), but we might imagine a worst case where a very large cell (maybe a shield wall) is subdivided finely by an unstructured mesh into millions of regions. In this case, retracing hundreds or thousands of mesh cell crossings might be quite expensive to repeat.

I don't see a great way of guaranteeing both full reproducibility and guaranteeing that no ray tracing work is performed twice. As CI reproducibility is immediately quite valuable to us, I think we should go the route this PR takes. If someone down the line hits a real world use case where this choice becomes very expensive for performance, we could always add in a user option to enable "fast non-reproducible cell under voxel ray tracing" or something like that.

AI Assistance

  • Harness: Claude Code
  • Model: Claude 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

  • Bug Fixes
    • Improved continuity of random-ray transport across distributed simulation boundaries, including when rays are handed off partway through a cell. Handoffs preserve cell-entry state so receiving partitions can continue transport consistently.
    • Improved tracking of completed ray segments during handoff, including for inactive rays and rays that have traveled no positive distance. This helps prevent repeated segment processing and incorrect neighbor tracking.

John Tramm added 2 commits November 20, 2026 09:09
A ray that left a subdomain between two bins of a source region mesh was
moved to that point, and the receiving rank started a new ray trace from
inside the cell. That trace is not the one made when the ray entered the
cell: the first segment lost TINY_BIT of length, so results depended on
where the subdomain boundaries were (which load balancing sets from
measured wall time), and the restart point could be invalid for the
geometry (lost rays).

The ray is now buffered in the state it had on entry to the cell, with
the number of segments it has been attenuated over since. The receiving
rank traces the cell again from that state and skips those segments, so
every segment is the same as without domain decomposition. This replaces
the partial-length bookkeeping and the decomposed-only rule that skipped
segments shorter than TINY_BIT.
@coderabbitai

coderabbitai Bot commented Nov 20, 2026 •

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

Random-ray transport now records cell-entry state and completed segments during MPI handoff. The receiving rank restores the ray at cell entry and retraces the cell while skipping segments already attenuated.

Changes: Random-ray MPI handoff

Layer / File(s) Summary
Cell-entry and exchange state
include/openmc/random_ray/random_ray.h, src/random_ray/random_ray.cpp
Ray and exchange structures track completed segments and whether initial flux needs initialization. RandomRay stores cell-entry distance and activity. Buffer packing records the supplied distance and activity.
Segment attenuation and handoff
src/random_ray/random_ray.cpp
Traversal counts completed segments and skips attenuation for segments already processed on another rank. When a ray exits a subdomain, it is buffered with its saved cell-entry state. The segment-skip count is cleared after local cell traversal.
Exchange and ray restoration
src/random_ray/random_ray.cpp, src/random_ray/ray_bank.cpp
Ray exchange transmits the completed-segment count and initial-flux flag. The receiving rank restores the ray at cell entry and uses the flag to restore angular flux or initialize it from the source region.

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

Change: Bug fix

Sequence Diagram(s)

sequenceDiagram
  participant SourceRankRandomRay
  participant RayBank
  participant DestinationRankRandomRay
  SourceRankRandomRay->>RayBank: Pack cell-entry state and completed-segment count
  RayBank->>DestinationRankRandomRay: Exchange ray data
  DestinationRankRandomRay->>DestinationRankRandomRay: Restore cell-entry state and skip completed segments
Loading

Possibly related PRs

Suggested reviewers: suark94, pshriwise, paulromano

Merge Risk: Minimal · up to 8a118

This change improves how random-ray MPI handoffs resume tracing across subdomain boundaries, which should make results less sensitive to load-balanced boundary placement. Inspection found no concrete defect in the handoff accounting, so merge risk is minimal.

Pre-merge checks: Passed 4

Check name Status Explanation
Title check Passed The title names the main change: the random ray domain decomposed MPI ray handoff. The raw summary and PR objectives confirm that the change replaces partial-cell buffering with buffering of the entry…
Description check Passed The description covers the problem, the fix, testing results, and a runtime-cost discussion. It also states that non-MPI results are unchanged. The AI assistance section lists the harness, model, and …
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: the random ray domain decomposed MPI ray handoff. The raw summary and PR objectives confirm that the change replaces partial-cell buffering with buffering of the entry state and segment count, and the receiving rank now retraces the cell and skips completed segments. The title is short, specific, and matches the changeset.

Full details: Description check

Explanation

The description covers the problem, the fix, testing results, and a runtime-cost discussion. It also states that non-MPI results are unchanged. The AI assistance section lists the harness, model, and reasoning effort, and the checklist is filled in. The template's 'Fixes # (issue)' line is left unfilled, and the description does not link an issue or PR. That gap is minor, so the description is mostly complete.

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: 22e5a38c-ff32-477a-ad21-493080ff4ba7
Commits

Reviewing files that changed from the base of the PR and between ec6339e and 8a11891.

Files selected for processing: 3

  • include/openmc/random_ray/random_ray.h
  • src/random_ray/random_ray.cpp
  • src/random_ray/ray_bank.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.

Avoiding having to mess around with the TINY_BITs seems like a good way to ensure consistency of results regardless of where the subdomain boundaries fall, even though this means some ray tracing work has to be repeated. Thanks @jtramm !

Comment thread src/random_ray/random_ray.cpp Resolved
A ray with zero inactive length starts active before its angular flux
has been initialized. Mark sampling handoffs explicitly so the receiver
initializes flux from the source region and the sender records neighbors
from transport handoffs only.

@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.

Thanks @jtramm! The changes look fine to me. I also had GPT-6.2-Sol max review and it spotted one subtle (pre-existing) bug: in an MPI job where the inactive distance is set to zero, a newly sampled ray that was sent to another rank would mistake the zero-filled buffer for a valid flux. The commit I pushed should address this by adding a flag indicating whether to initialized a sampled ray's angular flux from its starting region. Let me know if that looks OK to you and if so, I'll go ahead and merge.

This branch has not been deployed

No deployments
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