Celeritas 0.7.0-dev.127+develop.e63889793
Loading...
Searching...
No Matches
Public Member Functions | List of all members
celeritas::Selector< F, T > Class Template Reference

On-the-fly selection of a weighted discrete distribution. More...

#include <Selector.hh>

Public Types

Type aliases
using arg_type = T
 
using real_type = typename std::invoke_result< F, arg_type >::type
 

Public Member Functions

CELER_FUNCTION Selector (F &&eval, arg_type size, real_type total, SelectorNormalization)
 Construct with function, size, and accumulated value.
 
template<class Engine >
CELER_FUNCTION T operator() (Engine &rng) const
 Sample from the distribution.
 

Detailed Description

template<class F, class T>
class celeritas::Selector< F, T >

On-the-fly selection of a weighted discrete distribution.

This algorithm encapsulates the loop for sampling from distributions of a function f(index) -> real (usually with the function prototype real_type(*)(size_type) ). The index can be an integer, an enumeration, or an OpaqueId . The Selector is constructed with the size of the distribution (but using the indexing type).

Edge cases are thoroughly tested (it will never iterate off the end if normalized, even for incorrect values of the "total" probability/xs), and it uses one fewer register than the typical accumulation algorithm. When building with debug checking and normalization, the constructor asserts that the provided "total" value is consistent.

The given function must return a consistent value for the same given argument.

auto select_el = make_selector(
[](ElementId i) { return xs[i.get()]; },
ElementId{num_elements()},
tot_xs);
ElementId el = select_el(rng);
CELER_FUNCTION Selector< F, T > make_selector(F &&func, T size, real_type total=1)
Create a normalized on-the-fly discrete PDF sampler.
Definition Selector.hh:158
size_type get() const
Get the ID's value.
Definition OpaqueId.hh:146

or

auto select_val = make_selector([](size_type i) { return pdf[i]; },
pdf.size());
size_type idx = select_val(rng);
unsigned int size_type
Standard type for container sizes, optimized for GPU use.
Definition corecel/Types.hh:27

Create a normalized selector from a function and total accumulated value.

See also
celeritas::make_selector , celeritas::make_unnormalized_selector
Template Parameters
Ffunction type to sample from
Tindex type passed to the function

Constructor & Destructor Documentation

◆ Selector()

template<class F , class T >
CELER_FUNCTION celeritas::Selector< F, T >::Selector ( F &&  eval,
arg_type  size,
real_type  total,
SelectorNormalization  norm 
)
inline

Construct with function, size, and accumulated value.

The SelectorNormalization argument determines whether to allow an "off the end" value. If unnormalized, the sum of all evaluated arguments can be nontrivially less than the given total.


The documentation for this class was generated from the following file: