|
OPALX (Object Oriented Parallel Accelerator Library for Exascal) MINIorX
OPALX
|
A class that bins particles in energy bins and allows for adaptive runtime rebinning. More...
#include <AdaptBins.h>
Public Types | |
| using | value_type = typename BinningSelector::value_type |
| using | particle_position_type = typename BunchType::particle_position_type |
| using | position_view_type = typename particle_position_type::view_type |
| using | size_type = typename BunchType::size_type |
| using | bin_index_type = typename BunchType::bin_index_type |
| using | bin_type = typename ippl::ParticleAttrib<bin_index_type> |
| using | bin_view_type = typename bin_type::view_type |
| using | hash_type = ippl::detail::hash_type<Kokkos::DefaultExecutionSpace::memory_space> |
| using | d_histo_type = Histogram<size_type, bin_index_type, value_type, true> |
| using | dview_type = typename d_histo_type::dview_type |
| using | hview_type = typename d_histo_type::hview_type |
| using | dwidth_view_type = typename d_histo_type::dwidth_view_type |
| using | hindex_transform_type = typename d_histo_type::hindex_transform_type |
| using | dindex_transform_type = typename d_histo_type::dindex_transform_type |
| using | h_histo_type_g = Histogram<size_type, bin_index_type, value_type, false, Kokkos::HostSpace> |
| using | hview_type_g = typename h_histo_type_g::hview_type |
| using | hindex_transform_type_g = typename h_histo_type_g::hindex_transform_type |
Public Member Functions | |
| AdaptBins (std::shared_ptr< BunchType > bunch, BinningSelector var_selector, bin_index_type maxBins, value_type binningAlpha, value_type binningBeta, value_type desiredWidth) | |
| Constructs an AdaptBins object with a specified maximum number of bins and a selector. | |
| void | initTimers () |
| Initializes timers for various operations in the binning process. | |
| bin_view_type | getBinView () |
| Returns a view to the particle bin array. | |
| bin_index_type | getCurrentBinCount () const |
| Returns the current number of bins. | |
| bin_index_type | getMaxBinCount () const |
| Gets the maximum number of bins. Will be used for the fine uniform histogram before merging. | |
| value_type | getBinWidth () const |
| Returns the average binwidth. | |
| void | setCurrentBinCount (bin_index_type nBins) |
| Sets the current number of bins and adjusts the bin width. | |
| hash_type | getHashArray () |
| Returns the index map that sorts the particle container by bin number. | |
| void | initLimits () |
| Initializes the limits for binning based on the particle data. | |
| void | instantiateHistogram (bool setToZero=false) |
| Initializes the histogram view for binning and optionally sets it to zero. | |
| void | assignBinsToParticles () |
| Assigns each particle in the bunch to a bin based on its position. | |
| void | initLocalHisto (HistoReductionMode modePreference=HistoReductionMode::Standard) |
| Initializes a local histogram for particle binning. | |
| void | executeInitLocalHistoReductionTeamFor () |
| Initializes and performs a team-based histogram reduction for particle bins. | |
| template<typename ReducerType> | |
| void | executeInitLocalHistoReduction (ReducerType &to_reduce) |
| Executes a parallel reduction to initialize the local histogram for particle bins. | |
| void | initGlobalHistogram () |
| Retrieves the global histogram across all processes. | |
| void | doFullRebin (bin_index_type nBins, bool recalculateLimits=true, HistoReductionMode modePreference=HistoReductionMode::Standard) |
| Performs a full rebinning operation on the bunch. | |
| void | initHistogram (HistoReductionMode modePreference=HistoReductionMode::Standard) |
| Initializes the local/global histogram based on the Bin attribute. | |
| size_type | getNPartInBin (bin_index_type binIndex, bool global=false) |
| Retrieves the number of particles in a specified bin. | |
| void | sortContainerByBin () |
| Sorts the container of particles by their bin indices. | |
| Kokkos::RangePolicy | getBinIterationPolicy (const bin_index_type &binIndex) |
| Returns the bin iteration policy for a given bin index. | |
| void | genAdaptiveHistogram () |
| Generates an adaptive histogram based on a fine global histogram. | |
| void | print () |
| Prints the current global histogram to the Inform output stream. | |
| void | debug () |
| Outputs debug information related to Kokkos and MPI configurations. | |
| template<typename T, unsigned Dim> | |
| VField_t< T, Dim > & | LTrans (VField_t< T, Dim > &field, const bin_index_type ¤tBin) |
| Applies a Lorentz transformation to a given vector field based on particle velocities. | |
Static Public Member Functions | |
| static KOKKOS_INLINE_FUNCTION bin_index_type | getBin (value_type x, value_type xMin, value_type xMax, value_type binWidthInv, bin_index_type numBins) |
| Calculates the bin index for a given position value in a uniform histogram. | |
Private Attributes | |
| std::shared_ptr< BunchType > | bunch_m |
| Shared pointer to the particle container. | |
| BinningSelector | var_selector_m |
| Variable selector for binning. | |
| const bin_index_type | maxBins_m |
| Maximum number of bins. | |
| bin_index_type | currentBins_m |
| Current number of bins in use. | |
| value_type | xMin_m |
| Minimum value of bin attribute. | |
| value_type | xMax_m |
| Maximum value of bin attribute. | |
| value_type | binWidth_m |
| Width of each bin (assumes a uniform histogram). | |
| value_type | binningAlpha_m |
| Alpha parameter for binning cost function. | |
| value_type | binningBeta_m |
| Beta parameter for binning cost function. | |
| value_type | desiredWidth_m |
| Desired bin width for binning cost function. | |
| d_histo_type | localBinHisto_m |
| Local histogram view for bin counts. | |
| h_histo_type_g | globalBinHisto_m |
| Global histogram view (over ranks reduced local histograms). | |
| hash_type | sortedIndexArr_m |
| Particle index map that sorts the bunch by bin index. | |
| IpplTimings::TimerRef | bInitLimitsT |
| IpplTimings::TimerRef | bAllReduceLimitsT |
| IpplTimings::TimerRef | bAllReduceGlobalHistoT |
| IpplTimings::TimerRef | bAssignUniformBinsT |
| IpplTimings::TimerRef | bExecuteHistoReductionT |
| IpplTimings::TimerRef | bSortContainerByBinT |
| IpplTimings::TimerRef | bVerifySortingT |
A class that bins particles in energy bins and allows for adaptive runtime rebinning.
This class provides functionality to group particles into energy bins, initialize and reinitialize bin structures, and update bin contents using data structures from IPPL and Kokkos. It is optimized for usage on CPU and GPU. It is explained in detail in Alexander Liemen's master thesis and was developed for OPAL-X to improve the performance when solving the electric field in a relativistic particle bunch.
| BunchType | The type of particle bunch (container) used in the binning process. |
| BinningSelector | A function or functor that selects the variable for binning (based on attributes). |
Definition at line 36 of file AdaptBins.h.
| using ParticleBinning::AdaptBins< BunchType, BinningSelector >::bin_index_type = typename BunchType::bin_index_type |
Definition at line 43 of file AdaptBins.h.
| using ParticleBinning::AdaptBins< BunchType, BinningSelector >::bin_type = typename ippl::ParticleAttrib<bin_index_type> |
Definition at line 44 of file AdaptBins.h.
| using ParticleBinning::AdaptBins< BunchType, BinningSelector >::bin_view_type = typename bin_type::view_type |
Definition at line 45 of file AdaptBins.h.
| using ParticleBinning::AdaptBins< BunchType, BinningSelector >::d_histo_type = Histogram<size_type, bin_index_type, value_type, true> |
Definition at line 49 of file AdaptBins.h.
| using ParticleBinning::AdaptBins< BunchType, BinningSelector >::dindex_transform_type = typename d_histo_type::dindex_transform_type |
Definition at line 54 of file AdaptBins.h.
| using ParticleBinning::AdaptBins< BunchType, BinningSelector >::dview_type = typename d_histo_type::dview_type |
Definition at line 50 of file AdaptBins.h.
| using ParticleBinning::AdaptBins< BunchType, BinningSelector >::dwidth_view_type = typename d_histo_type::dwidth_view_type |
Definition at line 52 of file AdaptBins.h.
| using ParticleBinning::AdaptBins< BunchType, BinningSelector >::h_histo_type_g = Histogram<size_type, bin_index_type, value_type, false, Kokkos::HostSpace> |
Definition at line 56 of file AdaptBins.h.
| using ParticleBinning::AdaptBins< BunchType, BinningSelector >::hash_type = ippl::detail::hash_type<Kokkos::DefaultExecutionSpace::memory_space> |
Definition at line 46 of file AdaptBins.h.
| using ParticleBinning::AdaptBins< BunchType, BinningSelector >::hindex_transform_type = typename d_histo_type::hindex_transform_type |
Definition at line 53 of file AdaptBins.h.
| using ParticleBinning::AdaptBins< BunchType, BinningSelector >::hindex_transform_type_g = typename h_histo_type_g::hindex_transform_type |
Definition at line 58 of file AdaptBins.h.
| using ParticleBinning::AdaptBins< BunchType, BinningSelector >::hview_type = typename d_histo_type::hview_type |
Definition at line 51 of file AdaptBins.h.
| using ParticleBinning::AdaptBins< BunchType, BinningSelector >::hview_type_g = typename h_histo_type_g::hview_type |
Definition at line 57 of file AdaptBins.h.
| using ParticleBinning::AdaptBins< BunchType, BinningSelector >::particle_position_type = typename BunchType::particle_position_type |
Definition at line 40 of file AdaptBins.h.
| using ParticleBinning::AdaptBins< BunchType, BinningSelector >::position_view_type = typename particle_position_type::view_type |
Definition at line 41 of file AdaptBins.h.
| using ParticleBinning::AdaptBins< BunchType, BinningSelector >::size_type = typename BunchType::size_type |
Definition at line 42 of file AdaptBins.h.
| using ParticleBinning::AdaptBins< BunchType, BinningSelector >::value_type = typename BinningSelector::value_type |
Definition at line 39 of file AdaptBins.h.
|
inline |
Constructs an AdaptBins object with a specified maximum number of bins and a selector.
| bunch | A shared pointer to the underlying particle container. |
| var_selector | A function or functor that selects the variable for binning (based on attributes). |
| maxBins | The maximum number of bins to initialize with (usually 128). |
| binningAlpha | The alpha parameter for the adaptive binning (merging) algorithm. |
| binningBeta | The beta parameter for the adaptive binning (merging) algorithm. |
| desiredWidth | The desiredWidth for the adaptive binning (merging) algorithm. |
Definition at line 74 of file AdaptBins.h.
References binningAlpha_m, binningBeta_m, bunch_m, currentBins_m, desiredWidth_m, initTimers(), maxBins_m, and var_selector_m.
| void ParticleBinning::AdaptBins< BunchType, BinningSelector >::assignBinsToParticles | ( | ) |
Assigns each particle in the bunch to a bin based on its position.
This function iterates over all particles in the bunch, calculates their bin index using getBin(...), and updates the "Bin" attribute in the container structure accordingly.
Referenced by doFullRebin().
|
inline |
Outputs debug information related to Kokkos and MPI configurations.
This function prints information about the number of threads (in OpenMP) or GPUs (in CUDA) available on the current MPI rank, along with other debug information.
Definition at line 416 of file AdaptBins.h.
References binningAlpha_m, binningBeta_m, and desiredWidth_m.
|
inline |
Performs a full rebinning operation on the bunch.
It does the following steps:
Initializes the limits of the binning attribute.
Sets the current number of bins to nBins (usually 128 for the fine histogram).
Assigns uniform bins to particles based on the new limits and bin count.
(Re-)initializes the local and global histogram based on the new bins.
| nBins | The new number of bins to use for rebinning. |
| recalculateLimits | If true, the limits are recalculated based on the current particle data. |
| modePreference | The preferred mode for histogram reduction (default is Standard and choses the best method itself). |
Definition at line 295 of file AdaptBins.h.
References assignBinsToParticles(), initHistogram(), initLimits(), setCurrentBinCount(), and ParticleBinning::Standard.
| void ParticleBinning::AdaptBins< BunchType, BinningSelector >::executeInitLocalHistoReduction | ( | ReducerType & | to_reduce | ) |
Executes a parallel reduction to initialize the local histogram for particle bins.
This function performs a Kokkos::parallel_reduce over the particles in the bunch, incrementing counts in the reduction array to_reduce based on the bin index for each particle. After the reduction, the results are copied to the final histogram localBinHisto.
| ReducerType | The type of the reduction object, which should support the_array for bin counts. |
| to_reduce | A reduction object that accumulates bin counts for the histogram. |
The function performs the following steps:
Executes a Kokkos parallel reduction loop where each particle increments the bin count corresponding to its bin index.
Executes a parallel loop to copy the reduced bin counts from to_reduce to localBinHisto.
| void ParticleBinning::AdaptBins< BunchType, BinningSelector >::executeInitLocalHistoReductionTeamFor | ( | ) |
Initializes and performs a team-based histogram reduction for particle bins.
This function allocates scratch memory for each team on device, initializes a local histogram for each team in shared memory, updates it based on bin indices of particles assigned to that team and finally reduces the team-local histograms into a global histogram in device memory using pure atomics.
The process consists of the following steps:
Allocating scratch memory for each team's local histogram.
Initializing the local histogram to zero.
Assigning particles to bins in parallel within each team.
Reducing each team's local histogram into a global histogram (atomics).
For GPU architecture, this function does not need to be changed. Should problems with unusal execution time occur (e.g. if this function is supposed to be called on CPU – which it shouldn't), it might make sense to look at the following parameters calculated in the function:
Scratch Memory: Scratch memory is allocated per team for a local histogram, with size binCount.
Concurrency: team_size specifies the number of threads per team, and each team processes a block_size.
| void ParticleBinning::AdaptBins< BunchType, BinningSelector >::genAdaptiveHistogram | ( | ) |
Generates an adaptive histogram based on a fine global histogram.
This function is used to create an adaptive histogram from a fine global histogram. The fine global histogram (usually 128 bins) has to be created beforehand, but does not necessarily have to be uniform (even though this is recommended). The algorithm works perfectly fine as long as the globalBinHisto_m.binWidths_m field is initialized correctly. If not specified otherwise, it will be initialized either uniformly at initialization, or changed by the mergeBins() call or copied from another BinHisto instance.
It performs the following steps:
Merges bins in the global histogram to create an adaptive histogram (see BinHisto::mergeBins()).
Maps old bin indices to new bin indices using the lookup table returned by mergeBins().
Updates the local histogram with the new bin indices and widths.
|
static |
Calculates the bin index for a given position value in a uniform histogram.
This static method calculates which bin a position value falls into based on the bin boundaries and bin width. Uses integer casting to get bin index and clamps it to the biggest bin if out of range (only happens if limits and the particle bunch are out of sync – should not happen since particle push is not done between limit calculation and bin assignment).
| x | The binning parameter value (e.g. z-velocity). |
| xMin | Minimum boundary for the bins. |
| xMax | Maximum boundary for the bins. |
| binWidthInv | Inverse of the bin width for efficiency. |
| numBins | The total number of bins. |
|
inline |
Returns the bin iteration policy for a given bin index.
This function generates a range policy for iterating over the elements within a given bin index. If no DualView is used, it might need to copy some values to host, which might cause overhead. However, this only happens if the localBinHisto_m is implemented as a device only view, which is not its intended type.
| binIndex | The index of the bin for which the iteration policy is to be generated. |
Definition at line 379 of file AdaptBins.h.
References localBinHisto_m.
|
inline |
Returns a view to the particle bin array.
Definition at line 114 of file AdaptBins.h.
References bunch_m.
|
inline |
Returns the average binwidth.
Definition at line 135 of file AdaptBins.h.
References binWidth_m.
|
inline |
Returns the current number of bins.
Definition at line 121 of file AdaptBins.h.
References currentBins_m.
Referenced by getNPartInBin().
|
inline |
Returns the index map that sorts the particle container by bin number.
Definition at line 154 of file AdaptBins.h.
References sortedIndexArr_m.
|
inline |
Gets the maximum number of bins. Will be used for the fine uniform histogram before merging.
Definition at line 128 of file AdaptBins.h.
References maxBins_m.
|
inline |
Retrieves the number of particles in a specified bin.
This function returns the number of particles in the bin specified by the given index. It assumes that the DualView (if used) has been properly synchronized and initialized. If the function is called frequently, it might create some overhead due to the .view_host() call. However, since it is only called on the host (a maximum of nBins times per iteration), the overhead should be minimal. For better efficiency, one can avoid the Kokkos::View "copying-action" by using dualView.h_view(binIndex). Just be careful, since this might change the underlying data structure of the BinHisto class.
| binIndex | The index of the bin for which the number of particles is to be retrieved. |
| global | If true, retrieves from global histogram, otherwise from local histogram. |
Definition at line 332 of file AdaptBins.h.
References bunch_m, getCurrentBinCount(), globalBinHisto_m, and localBinHisto_m.
| void ParticleBinning::AdaptBins< BunchType, BinningSelector >::initGlobalHistogram | ( | ) |
Retrieves the global histogram across all processes.
This function reduces the local histograms across all MPI processes into a single global histogram view.
Referenced by initHistogram().
|
inline |
Initializes the local/global histogram based on the Bin attribute.
| modePreference | The preferred mode for histogram reduction (default is Standard). |
Definition at line 307 of file AdaptBins.h.
References globalBinHisto_m, initGlobalHistogram(), initLocalHisto(), instantiateHistogram(), localBinHisto_m, and ParticleBinning::Standard.
Referenced by doFullRebin().
| void ParticleBinning::AdaptBins< BunchType, BinningSelector >::initLimits | ( | ) |
Initializes the limits for binning based on the particle data.
This function calculates the minimum and maximum limits (xMin and xMax) from the binning attribute (e.g. velocity), which are then used to define bin boundaries.
Referenced by doFullRebin().
| void ParticleBinning::AdaptBins< BunchType, BinningSelector >::initLocalHisto | ( | HistoReductionMode | modePreference = HistoReductionMode::Standard | ) |
Initializes a local histogram for particle binning.
This function initializes the local histogram (instance of BinHisto) after bin indices were assigned using assignBinsToParticles(). It reduces the indices to a histogram using on the the reduction functions in this class. It chooses which function to use based on the HistoReductionMode parameter (defined in ParallelReduceTools.h) or the optimal method based on the current architecture/bin number.
References ParticleBinning::Standard.
Referenced by initHistogram().
|
inline |
Initializes timers for various operations in the binning process.
Definition at line 99 of file AdaptBins.h.
References bAllReduceGlobalHistoT, bAllReduceLimitsT, bAssignUniformBinsT, bExecuteHistoReductionT, bInitLimitsT, bSortContainerByBinT, and bVerifySortingT.
Referenced by AdaptBins().
| void ParticleBinning::AdaptBins< BunchType, BinningSelector >::instantiateHistogram | ( | bool | setToZero = false | ) |
Initializes the histogram view for binning and optionally sets it to zero.
| setToZero | If true, initializes the histogram view to zero. Default is false. The 0 initialization is not needed if data is overwritten anyways. |
Referenced by initHistogram().
| VField_t< T, Dim > & ParticleBinning::AdaptBins< BunchType, BinningSelector >::LTrans | ( | VField_t< T, Dim > & | field, |
| const bin_index_type & | currentBin ) |
Applies a Lorentz transformation to a given vector field based on particle velocities.
| T | The data type of the field components (e.g., double, float). |
| Dim | The dimensionality of the field. |
| field | A reference to the vector field to be transformed. |
Gamma Factor Calculation: The gamma factor is derived from the velocity components of particles. For a given particle velocity (v), the gamma factor is computed as: [ \gamma = \frac{1}{\sqrt{1 - \vec{v} \cdot \vec{v}}} ] where (\vec{v}) is the velocity vector of a particle.
Field Transformation: After computing the gamma factor, each component of the field is multiplied by the corresponding gamma factor.
|
inline |
Prints the current global histogram to the Inform output stream.
This function outputs the global histogram data (bin counts) to the standard output. Note: Only for rank 0 in an MPI environment.
Definition at line 406 of file AdaptBins.h.
References globalBinHisto_m.
|
inline |
Sets the current number of bins and adjusts the bin width.
| nBins | The new number of currently used bins (locally). |
Definition at line 144 of file AdaptBins.h.
References binWidth_m, currentBins_m, maxBins_m, xMax_m, and xMin_m.
Referenced by doFullRebin().
| void ParticleBinning::AdaptBins< BunchType, BinningSelector >::sortContainerByBin | ( | ) |
Sorts the container of particles by their bin indices.
This function assumes that the prefix sum (post-sum) has already been initialized. It rearranges a particle indices array into the correct order based on their Bin attribute and verifies the sorting. The function uses a parallelized bin sort.
Steps:
Retrieves the bin view and initializes the bin offsets using the prefix sum that is saved inside the localBinHisto_m instance (should be of type BinHisto in DualView mode).
Allocates a hash_array to store the sorted indices of particles.
Uses a Kokkos loop to place each particle into its target position based on its bin index.
(In Debug mode) verifies that the sorting was successful.
|
private |
Definition at line 509 of file AdaptBins.h.
Referenced by initTimers().
|
private |
Definition at line 508 of file AdaptBins.h.
Referenced by initTimers().
|
private |
Definition at line 510 of file AdaptBins.h.
Referenced by initTimers().
|
private |
Definition at line 511 of file AdaptBins.h.
Referenced by initTimers().
|
private |
Definition at line 507 of file AdaptBins.h.
Referenced by initTimers().
|
private |
Alpha parameter for binning cost function.
Definition at line 496 of file AdaptBins.h.
Referenced by AdaptBins(), and debug().
|
private |
Beta parameter for binning cost function.
Definition at line 497 of file AdaptBins.h.
Referenced by AdaptBins(), and debug().
|
private |
Width of each bin (assumes a uniform histogram).
Definition at line 493 of file AdaptBins.h.
Referenced by getBinWidth(), and setCurrentBinCount().
|
private |
Definition at line 512 of file AdaptBins.h.
Referenced by initTimers().
|
private |
Shared pointer to the particle container.
Definition at line 487 of file AdaptBins.h.
Referenced by AdaptBins(), getBinView(), and getNPartInBin().
|
private |
Definition at line 513 of file AdaptBins.h.
Referenced by initTimers().
|
private |
Current number of bins in use.
Definition at line 490 of file AdaptBins.h.
Referenced by AdaptBins(), getCurrentBinCount(), and setCurrentBinCount().
|
private |
Desired bin width for binning cost function.
Definition at line 498 of file AdaptBins.h.
Referenced by AdaptBins(), and debug().
|
private |
Global histogram view (over ranks reduced local histograms).
Definition at line 502 of file AdaptBins.h.
Referenced by getNPartInBin(), initHistogram(), and print().
|
private |
Local histogram view for bin counts.
Definition at line 501 of file AdaptBins.h.
Referenced by getBinIterationPolicy(), getNPartInBin(), and initHistogram().
|
private |
Maximum number of bins.
Definition at line 489 of file AdaptBins.h.
Referenced by AdaptBins(), getMaxBinCount(), and setCurrentBinCount().
|
private |
Particle index map that sorts the bunch by bin index.
Definition at line 504 of file AdaptBins.h.
Referenced by getHashArray().
|
private |
Variable selector for binning.
Definition at line 488 of file AdaptBins.h.
Referenced by AdaptBins().
|
private |
Maximum value of bin attribute.
Definition at line 492 of file AdaptBins.h.
Referenced by setCurrentBinCount().
|
private |
Minimum value of bin attribute.
Definition at line 491 of file AdaptBins.h.
Referenced by setCurrentBinCount().