|
OPALX (Object Oriented Parallel Accelerator Library for Exascal) MINIorX
OPALX
|
Template class providing adaptive particle histogram binning with support for Kokkos Views and DualViews. More...
#include <BinHisto.h>
Public Types | |
| using | view_type |
| using | dview_type = typename DeviceViewTraits<UseDualView, view_type>::d_type |
| using | hview_type = typename DeviceViewTraits<UseDualView, view_type>::h_type |
| using | width_view_type |
| using | hwidth_view_type = typename DeviceViewTraits<UseDualView, width_view_type>::h_type |
| using | dwidth_view_type = typename DeviceViewTraits<UseDualView, width_view_type>::d_type |
| template<class... Args> | |
| using | index_transform_type = Kokkos::View<bin_index_type*, Args...> |
| using | dindex_transform_type = index_transform_type<Kokkos::DefaultExecutionSpace> |
| using | hindex_transform_type = index_transform_type<Kokkos::HostSpace> |
Public Member Functions | |
| Histogram ()=default | |
| Default constructor for the Histogram class. | |
| Histogram (std::string debug_name, bin_index_type numBins, value_type totalBinWidth, value_type binningAlpha, value_type binningBeta, value_type desiredWidth) | |
| Constructor for the Histogram class with a given name, number of bins, and total bin width. | |
| Histogram (const Histogram &other) | |
| Default destructor for the Histogram class. | |
| Histogram & | operator= (const Histogram &other) |
| Assignment operator for copying the fields from another Histogram object. | |
| size_type | getNPartInBin (bin_index_type binIndex) |
| Retrieves the number of particles in a specified bin. | |
| size_type | getCurrentBinCount () const |
| Returns the current number of bins in the histogram. | |
| view_type | getHistogram () |
| Returns the Kokkos View containing the histogram bin counts. | |
| view_type | getPostSum () |
| Returns the Kokkos View containing the post-sum of bin counts. | |
| width_view_type | getBinWidths () const |
| Returns the Kokkos View of bin widths in the current histogram configuration. It will be an array of constant values if the histogram is uniform. | |
| template<typename Histogram_t> | |
| void | copyBinWidths (const Histogram_t &other) |
| Sets the bin widths by copying them from a different Histogram instance (usually neccessary after merging the global histogram to copy the new non-uniform widths to the local histogram). | |
| void | init () |
| Synchronizes the histogram view and initializes the bin widths and post-sum. | |
| void | initConstBinWidths (const value_type constBinWidth) |
| Initializes the bin widths with a constant value. | |
| void | initPostSum () |
| Initializes and computes the post-sum for the histogram. | |
| Kokkos::RangePolicy | getBinIterationPolicy (const bin_index_type &binIndex1, const bin_index_type numBins=1) |
| Returns a Kokkos::RangePolicy for iterating over the elements in a specified bin. | |
| void | sync () |
| Synchronizes the histogram data between host and device. | |
| void | modify_device () |
| If a DualView is used, it sets the flag on the view that the device has been modified. | |
| void | modify_host () |
| If a DualView is used, it sets the flag on the view that the host has been modified. | |
| hindex_transform_type | mergeBins () |
| Merges bins in a histogram to reduce the number of bins while minimizing a cost function. | |
| void | printHistogram (std::ostream &os=std::cout) |
| Prints a nicely formatted table of bin indices, counts, and widths. | |
| void | printPythonArrays () const |
Static Public Member Functions | |
| template<typename return_type, typename HistogramType> | |
| static constexpr return_type | getDeviceView (HistogramType histo) |
| Retrieves the device view of the histogram. | |
| template<typename return_type, typename HistogramType> | |
| static constexpr return_type | getHostView (HistogramType histo) |
| Retrieves a host view of the given histogram. | |
Private Member Functions | |
| value_type | adaptiveBinningCostFunction (const size_type &sumCount, const value_type &sumWidth, const size_type &totalNumParticles) |
| Computes the cost function for adaptive binning in a histogram. | |
| void | initTimers () |
| Initializes timers for various operations in the binning process. | |
| void | instantiateHistograms () |
| Instantiates the histogram, bin widths, and post-sum views (Possibly DualView). | |
| void | copyFields (const Histogram &other) |
| Copies the fields from another Histogram object. | |
Private Attributes | |
| std::string | debug_name_m |
| bin_index_type | numBins_m |
| Debug name for identifying the histogram instance. | |
| value_type | totalBinWidth_m |
| Number of bins in the histogram. | |
| value_type | binningAlpha_m |
| Total width of all bins combined. | |
| value_type | binningBeta_m |
| Alpha parameter for the adaptive binning (merging) cost function. | |
| value_type | desiredWidth_m |
| Beta parameter for the adaptive binning (merging) cost function. | |
| view_type | histogram_m |
| Desired width for the adaptive binning (merging) cost function. | |
| width_view_type | binWidths_m |
| View storing the particle counts in each bin. | |
| view_type | postSum_m |
| View storing the widths of the bins. | |
| IpplTimings::TimerRef | bDeviceSyncronizationT |
| View storing the cumulative sum of bin counts (used in sorting, generating range policies). | |
| IpplTimings::TimerRef | bHistogramInitT |
| IpplTimings::TimerRef | bMergeBinsT |
Template class providing adaptive particle histogram binning with support for Kokkos Views and DualViews.
The Histogram class implements a flexible histogram for particle binning. It manages bin counts, bin widths, and post-sums, supporting both uniform and adaptive binning (merging). All operations can be performed on the host or device, as required. The class can be initialized as a Kokkos::DualView or a simple Kokkos::View, should it be necessary to use the host/device views separately. In that case, one needs to make sure that the sync() and modify_... functions are called correctly to ensure that the data is synchronized. The synchronization is a wrapper around the Kokkos sync functions. More can be found in the Kokkos::DualView documentation.
| size_type | Type used for counting (e.g., int, size_t). |
| bin_index_type | Type used for bin indices. |
| value_type | Type representing binning parameter. |
| UseDualView | If true, uses Kokkos::DualView for host/device sync; else, Kokkos::View. |
| Properties | Additional properties for Kokkos Views/DualViews (e.g. explicit memory spaces). |
Definition at line 59 of file BinHisto.h.
| using ParticleBinning::Histogram< size_type, bin_index_type, value_type, UseDualView, Properties >::dindex_transform_type = index_transform_type<Kokkos::DefaultExecutionSpace> |
Definition at line 79 of file BinHisto.h.
| using ParticleBinning::Histogram< size_type, bin_index_type, value_type, UseDualView, Properties >::dview_type = typename DeviceViewTraits<UseDualView, view_type>::d_type |
Definition at line 66 of file BinHisto.h.
| using ParticleBinning::Histogram< size_type, bin_index_type, value_type, UseDualView, Properties >::dwidth_view_type = typename DeviceViewTraits<UseDualView, width_view_type>::d_type |
Definition at line 74 of file BinHisto.h.
| using ParticleBinning::Histogram< size_type, bin_index_type, value_type, UseDualView, Properties >::hindex_transform_type = index_transform_type<Kokkos::HostSpace> |
Definition at line 80 of file BinHisto.h.
| using ParticleBinning::Histogram< size_type, bin_index_type, value_type, UseDualView, Properties >::hview_type = typename DeviceViewTraits<UseDualView, view_type>::h_type |
Definition at line 67 of file BinHisto.h.
| using ParticleBinning::Histogram< size_type, bin_index_type, value_type, UseDualView, Properties >::hwidth_view_type = typename DeviceViewTraits<UseDualView, width_view_type>::h_type |
Definition at line 73 of file BinHisto.h.
| using ParticleBinning::Histogram< size_type, bin_index_type, value_type, UseDualView, Properties >::index_transform_type = Kokkos::View<bin_index_type*, Args...> |
Definition at line 78 of file BinHisto.h.
| using ParticleBinning::Histogram< size_type, bin_index_type, value_type, UseDualView, Properties >::view_type |
Definition at line 63 of file BinHisto.h.
| using ParticleBinning::Histogram< size_type, bin_index_type, value_type, UseDualView, Properties >::width_view_type |
Definition at line 70 of file BinHisto.h.
|
default |
Default constructor for the Histogram class.
|
inline |
Constructor for the Histogram class with a given name, number of bins, and total bin width.
| debug_name | The name of the histogram for debugging purposes. Is passed as a name for the Kokkos::...View. |
| numBins | The number of bins in the histogram. Might change once the merging algorithm is used. |
| totalBinWidth | The total width of the value range covered by the particles, so $x_\mathrm{max} - x_\mathrm{min}$. |
| binningAlpha | The alpha parameter for the adaptive binning (merging) cost function. |
| binningBeta | The beta parameter for the adaptive binning (merging) cost function. |
| desiredWidth | The desired width for the adaptive binning (merging) cost function. |
Definition at line 98 of file BinHisto.h.
|
inline |
Default destructor for the Histogram class.
Copy constructor for copying the fields from another Histogram object.
Definition at line 123 of file BinHisto.h.
|
private |
Computes the cost function for adaptive binning in a histogram.
This function calculates a cost value based on several factors, including Shannon entropy, bin width penalties, and sparsity penalties. It is used to optimize the binning process by balancing the distribution of particles across bins while considering desired bin width and sparsity constraints.
| sumCount | The total count of particles in the current to-be-merged bin. |
| sumWidth | The total width of the current to-be-merged bin. |
| totalNumParticles | The total number of particles across all bins. |
The cost function is composed of the following terms:
Shannon Entropy: Encourages a balanced distribution of particles.
Wide Bin Penalty: Penalizes bins that are too wide or too narrow, based on the binningAlpha parameter.
Bin Size Bias: Biases the bin size towards desiredWidth, controlled by the binningBeta parameter.
Sparse Penalty: Penalizes bins with too few particles, particularly in the distribution tails, normalized by the desired width.
The cost function is defined as:
\[c[i, j] \overset{!}{=} \underbrace{N_\mathrm{tot}\, \log\left(N_\mathrm{tot}\right)\, \Delta v_\mathrm{tot}}_{\text{Shannon Entropy}} + \underbrace{\alpha \, \Delta v_\mathrm{tot}^2}_{\text{Width Bias}} + \underbrace{\beta \, \left(\Delta v_\mathrm{tot} - \Delta v_\mathrm{bias}\right)^2}_{\text{Deviation Penalty}} + \underbrace{\frac{\Delta v_\mathrm{bias}}{N_\mathrm{tot}} \Theta\left(\Delta v_\mathrm{bias}-N_\mathrm{tot}\right)}_{\text{Sparse Penalty}} \]
Where:
( N_\mathrm{tot} ): Total number of particles in the bin.
( \Delta v_\mathrm{tot} ): Total width of the bin.
( \Delta v_\mathrm{bias} ): Desired bin width.
( \alpha ): Weight for the width bias term.
( \beta ): Weight for the deviation penalty term.
( \Theta(x) ): Heaviside step function.
The function is designed to judge how exactly an "optimally merged histogram" looks like. For a more detailed explanation, see Alexander Liemen's master thesis ("Adaptive Energy Binning in OPAL-X").
|
inline |
Sets the bin widths by copying them from a different Histogram instance (usually neccessary after merging the global histogram to copy the new non-uniform widths to the local histogram).
Definition at line 195 of file BinHisto.h.
|
private |
Copies the fields from another Histogram object.
This function copies the internal fields from the provided Histogram object to the current object. The fields are copied using Kokkos' shallow copy.
| other | The Histogram object from which to copy the fields. |
Referenced by ParticleBinning::Histogram< size_type, bin_index_type, value_type, true >::Histogram(), and ParticleBinning::Histogram< size_type, bin_index_type, value_type, true >::operator=().
|
inline |
Returns a Kokkos::RangePolicy for iterating over the elements in a specified bin.
This function generates a range policy for iterating over the elements within a given bin index range.
| bin_index_type | The type of the bin index. |
| binIndex1 | The index of the bin for which the iteration policy is to be generated. |
| numBins | The number of bins to iterate over (default is 1) starting at binIndex1. |
Definition at line 286 of file BinHisto.h.
|
inline |
Returns the Kokkos View of bin widths in the current histogram configuration. It will be an array of constant values if the histogram is uniform.
Definition at line 184 of file BinHisto.h.
|
inline |
Returns the current number of bins in the histogram.
Definition at line 168 of file BinHisto.h.
|
inlinestaticconstexpr |
Retrieves the device view of the histogram.
This function returns the device view of the histogram if the UseDualView flag is set to true. Otherwise, it returns the histogram itself (which might not be on device!).
| HistogramType | The type of the histogram. |
| histo | Reference to the histogram. |
Definition at line 359 of file BinHisto.h.
Referenced by ParticleBinning::Histogram< size_type, bin_index_type, value_type, true >::initConstBinWidths().
|
inline |
Returns the Kokkos View containing the histogram bin counts.
Definition at line 173 of file BinHisto.h.
|
inlinestaticconstexpr |
Retrieves a host view of the given histogram.
This function returns a host view of the provided histogram object. If a DualView is used, it calls the view_host(), otherwise it returns the normal view.
| HistogramType | The type of the histogram object. |
| histo | The histogram object from which to retrieve the host view. |
Definition at line 379 of file BinHisto.h.
Referenced by ParticleBinning::Histogram< size_type, bin_index_type, value_type, true >::printHistogram(), and ParticleBinning::Histogram< size_type, bin_index_type, value_type, true >::printPythonArrays().
|
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 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).
| UseDualView | A boolean template parameter indicating whether DualView is used. |
| binIndex | The index of the bin for which the number of particles is to be retrieved. |
Definition at line 152 of file BinHisto.h.
|
inline |
Returns the Kokkos View containing the post-sum of bin counts.
Definition at line 178 of file BinHisto.h.
|
inline |
Synchronizes the histogram view and initializes the bin widths and post-sum.
Definition at line 214 of file BinHisto.h.
|
inline |
Initializes the bin widths with a constant value.
| constBinWidth | The constant value to set for all bin widths. |
Definition at line 229 of file BinHisto.h.
Referenced by ParticleBinning::Histogram< size_type, bin_index_type, value_type, true >::init().
|
inline |
Initializes and computes the post-sum for the histogram.
This function initializes the post-sum by computing the fixed sum on the device view of the post-sum member. If the UseDualView constant is true, it modifies the device view and synchronizes it with the host.
Definition at line 258 of file BinHisto.h.
Referenced by ParticleBinning::Histogram< size_type, bin_index_type, value_type, true >::init().
|
inlineprivate |
Initializes timers for various operations in the binning process.
Timers initialized:
bDeviceSyncronizationT: Measures the time taken for device synchronization.
bHistogramInitT: Measures the time taken to initialize histograms.
bMergeBinsT: Measures the time taken to merge bins.
Definition at line 484 of file BinHisto.h.
Referenced by ParticleBinning::Histogram< size_type, bin_index_type, value_type, true >::Histogram().
|
inlineprivate |
Instantiates the histogram, bin widths, and post-sum views (Possibly DualView).
Definition at line 494 of file BinHisto.h.
Referenced by ParticleBinning::Histogram< size_type, bin_index_type, value_type, true >::Histogram().
| hindex_transform_type ParticleBinning::Histogram< size_type, bin_index_type, value_type, UseDualView, Properties >::mergeBins | ( | ) |
Merges bins in a histogram to reduce the number of bins while minimizing a cost function.
This function performs adaptive binning by merging adjacent bins in a histogram. The merging process is guided by a cost function that evaluates the deviation introduced by merging bins. The goal is to minimize the total cost while reducing the number of bins.
The function performs the following steps:
Ensures the histogram is available on the host (if using DualView).
Computes prefix sums for bin counts and widths on the host.
Uses dynamic programming to find the optimal bin merging strategy that minimizes the total cost.
Reconstructs the boundaries of the merged bins based on the dynamic programming results.
Builds new arrays for the merged bins, including counts and widths.
Generates a lookup table mapping old bin indices to new bin indices.
Overwrites the old histogram arrays with the new merged ones.
If using DualView, marks the host data as modified and synchronizes with the device.
Recomputes the post-sum for the new merged histogram.
The cost function used for merging is defined by adaptiveBinningCostFunction(), which evaluates the deviation introduced by merging bins based on their counts and widths.
|
inline |
If a DualView is used, it sets the flag on the view that the device has been modified.
Definition at line 337 of file BinHisto.h.
|
inline |
If a DualView is used, it sets the flag on the view that the host has been modified.
Definition at line 345 of file BinHisto.h.
|
inline |
Assignment operator for copying the fields from another Histogram object.
Definition at line 132 of file BinHisto.h.
|
inline |
Prints a nicely formatted table of bin indices, counts, and widths.
| os | The output stream to write to (defaults to std::cout). |
Definition at line 538 of file BinHisto.h.
|
inline |
Definition at line 570 of file BinHisto.h.
|
inline |
Synchronizes the histogram data between host and device.
This function checks if the histogram data needs to be synchronized between the host and the device. If both the host and device have modifications, it issues a warning and overwrites the changes on the host. It then performs the necessary synchronization based on where the modifications occurred.
Definition at line 316 of file BinHisto.h.
Referenced by ParticleBinning::Histogram< size_type, bin_index_type, value_type, true >::init().
|
private |
View storing the cumulative sum of bin counts (used in sorting, generating range policies).
Definition at line 524 of file BinHisto.h.
|
private |
Definition at line 525 of file BinHisto.h.
|
private |
Total width of all bins combined.
Definition at line 516 of file BinHisto.h.
|
private |
Alpha parameter for the adaptive binning (merging) cost function.
Definition at line 517 of file BinHisto.h.
|
private |
View storing the particle counts in each bin.
Definition at line 521 of file BinHisto.h.
|
private |
Definition at line 526 of file BinHisto.h.
|
private |
Definition at line 512 of file BinHisto.h.
|
private |
Beta parameter for the adaptive binning (merging) cost function.
Definition at line 518 of file BinHisto.h.
|
private |
Desired width for the adaptive binning (merging) cost function.
Definition at line 520 of file BinHisto.h.
Referenced by ParticleBinning::Histogram< size_type, bin_index_type, value_type, true >::getNPartInBin().
|
private |
Debug name for identifying the histogram instance.
Definition at line 513 of file BinHisto.h.
|
private |
View storing the widths of the bins.
Definition at line 522 of file BinHisto.h.
|
private |
Number of bins in the histogram.
Definition at line 514 of file BinHisto.h.