Skip to content

Performance optimizations for shared secondary bank#4011

Open
paulromano wants to merge 4 commits into
openmc-dev:developfrom
paulromano:shared-sec-perf-opt2
Open

Performance optimizations for shared secondary bank#4011
paulromano wants to merge 4 commits into
openmc-dev:developfrom
paulromano:shared-sec-perf-opt2

Conversation

@paulromano

Copy link
Copy Markdown
Contributor

Description

This PR is a follow-on to #3995 with further optimizations I've made for when the shared secondary bank is turned on. The optimizations are focused on how thread-local banks are collected into the shared secondary bank. Currently, this involves a critical section to drain the thread-local banks into a single SharedArray followed by a sort on the array to preserve reproducibility. I've made the following changes:

  • First, I avoided the critical section on the drain of thread-local banks by pre-allocating the full bank, which then allows us to copy data into the appropriate offset from each thread using an exclusive scan on the progeny per particle.
  • Once I did that, I noted that we can precompute the index into the sorted array using the parent_id, progeny_id, and the progeny offsets, which allows us to entirely avoid the sort on the shared secondary bank.
  • After a generation's read bank has been used, its storage is no longer needed which means we can reuse that storage as the next write bank. This avoids extra memory allocations when the next secondary generation is no larger than the current one.

Performance Profiling

As I mentioned in #3995, these optimizations arose when I was observing poor performance on a D1S simulation using weight windows. To share some profiles as I was going through these optimizations. Here is the starting point (before #3995):
Before optimizations

You can see a huge block from the memory allocation of the particle cross section caches (addressed in #3995) but also some other inefficiences due to the allocation/sorting of the shared secondary bank itself (addressed here). After the optimizations from #3995 and this PR, this is what the profile looks like now:
After optimizaitons

At this point, the major "overhead" from the shared secondary mode is computing random number seeds (future_seed_coefficients) and I don't think there's much we can do about that.

Overall for this problem, the performance with the shared secondary bank improved by about 2.3× with both sets of optimizations, and whereas before it was about 37% of the performance with a local secondary bank, now it reaches 77% of the performance with a local secondary bank (turns out for this problem the load balancing between threads is not bad enough for shared secondary to give better performance than local).

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)

@paulromano paulromano requested a review from jtramm July 11, 2026 15:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant