Skip to content

Replace linear search in ContinuousTabular with binary search - #4098

Open
realmisch wants to merge 2 commits into
openmc-dev:developfrom
realmisch:fix_energy_sampling
Open

Replace linear search in ContinuousTabular with binary search#4098
realmisch wants to merge 2 commits into
openmc-dev:developfrom
realmisch:fix_energy_sampling

Conversation

@realmisch

Copy link
Copy Markdown
Contributor

Description

Currently, the sampling algorithm in ContinuousTabular::sample relies on two linear searches to locate the corresponding point in the CDF. Since the CDF is monotonically increasing, a binary search can be implemented for $\mathcal{O}(\log\ n)$ complexity instead of the $\mathcal{O}(n)$ complexity of the linear search.

Effect

Performance improvements up to 10% have been found with simple cases and identical results for a fixed seed.
Example case is the Jezebel benchmark from ICSBEP running openmc -s 4 with 500 batches and 100,000 particles. The runtimes are averaged over 5 runs for each search algorithm.

Search Type Linear Binary Difference
Total Runtime (s) 43.139±0.439 39.938±0.938 3.201±1.036
Transport Runtime (s) 27.402±0.280 23.586±0.554 3.816±0.608

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)

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