Skip to content

Optimize Mapping of Random Ray Source Regions to Tallies - #3465

Merged
jtramm merged 5 commits into
openmc-dev:developfrom
jtramm:fw_cadis_opt
Jun 25, 2025
Merged

jtramm merged 5 commits into
openmc-dev:developfrom
jtramm:fw_cadis_opt

Conversation

@jtramm

@jtramm jtramm commented Jun 24, 2025

Copy link
Copy Markdown
Contributor

Description

Currently in OpenMC, at the end of a batch, if a new source region is discovered during transport then a search of all known source regions is done to relate them to any spatial tally filters. Source regions that have already been mapped to tallies will exit this process early, saving time. However, the exit process happens after a Particle object is allocated and searched for with exhaustive_find_cell(), making this mapping test become quite expensive.

The present PR changes the behavior such that source regions are only mapped to tallies once. When mesh overlay is being used, this process occurs at the time when newly discovered source regions are moved out of the dynamic thread safe source region container into the permanent known source region container.

I had also thought about just adding in another data variable to the source region itself to use as a flag to indicate if it has been mapped to tallies yet or not. This would greatly speed up the exit process and save a lot of time. However, on large problems like JET we can easily have over 100 million source regions, so it's not a great design pattern to loop over all 100 million just to find one that needs mapping. Thus, the optimization proposed in this PR seems like the better long term route.

Note that this PR should not change any results from the code -- the same tallies will still be scored, but we are just reducing wasted effort.

Impact

I tested this new optimization out on a short run of the JET model on a large CPU node. The impacts are significant. Without this PR, tallying takes 55.78 seconds during the adjoint phase of the simulation. With this PR, tallying only takes 0.98 seconds (a 57x speedup for this section of the code).

Checklist

  • I have performed a self-review of my own code
  • I have run clang-format (version 15) 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)

Copilot AI 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.

Pull Request Overview

This PR optimizes the mapping of random ray source regions to tallies to significantly reduce tallying time, particularly for large-scale simulations. The key changes include:

  • Removing redundant resets of task containers during adjoint resets.
  • Modifying the simulation to conditionally map source regions based on a mesh subdivision flag.
  • Updating the source region to tally conversion logic to support a starting index, which aids in processing only new source regions.
  • Adjusting the iteration method for tally mapping and updating the function signature and documentation accordingly.

Reviewed Changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 1 comment.

File Description
src/random_ray/source_region.cpp Removed clearing loops for task containers during adjoint reset.
src/random_ray/random_ray_simulation.cpp Added a condition to check mesh subdivision before mapping source regions and passing the starting index.
src/random_ray/flat_source_domain.cpp Changed convert_source_regions_to_tallies to accept a starting index and iterated over all tallies using an index.
include/openmc/random_ray/flat_source_domain.h Updated the function signature of convert_source_regions_to_tallies to require a starting index.
Comments suppressed due to low confidence (4)

src/random_ray/source_region.cpp:261

  • Please verify that the removal of the task_set clearing does not lead to stale or accumulated tasks in subsequent iterations.
    MomentMatrix {0.0, 0.0, 0.0, 0.0, 0.0, 0.0});

src/random_ray/flat_source_domain.cpp:476

  • Ensure that iterating over all tallies from model::tallies instead of model::active_tallies is the intended change and does not inadvertently include tallies that should be filtered out.
      for (int i_tally = 0; i_tally < model::tallies.size(); i_tally++) {

include/openmc/random_ray/flat_source_domain.h:37

  • Ensure the accompanying documentation clearly explains the purpose and expected value of the 'start_sr_id' parameter.
  void convert_source_regions_to_tallies(int64_t start_sr_id);

src/random_ray/random_ray_simulation.cpp:512

  • Confirm that adding the mesh_subdivision_enabled_ condition aligns with the intended behavior for handling mesh subdivisions and that mapping for these cases is managed separately.
            !RandomRay::mesh_subdivision_enabled_) {

Comment thread src/random_ray/flat_source_domain.cpp
@jtramm jtramm changed the title Optimize of Random Ray Source Regions to Tallies Optimize Mapping of Random Ray Source Regions to Tallies Jun 24, 2025
@jtramm jtramm mentioned this pull request Jun 24, 2025
5 tasks done

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

Nice optimization: simple but effective!

@jtramm
jtramm merged commit a6db05a into openmc-dev:develop Jun 25, 2025
ahnaf-tahmid-chowdhury pushed a commit to ahnaf-tahmid-chowdhury/OpenMC that referenced this pull request Jul 7, 2025
yardasol pushed a commit to yardasol/openmc that referenced this pull request Dec 25, 2025
apingegno pushed a commit to apingegno/openmc that referenced this pull request May 7, 2026
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