15#include <Kokkos_DualView.hpp>
26 template <
typename BunchType>
29 using value_type =
typename BunchType::Layout_t::value_type;
32 using hash_type = ippl::detail::hash_type<Kokkos::DefaultExecutionSpace::memory_space>;
59 std::vector<size_type>& binCounts, std::vector<value_type>& binWidths)
const = 0;
78 template <
typename BunchType,
typename BinningSelector>
87 using bin_type =
typename ippl::ParticleAttrib<bin_index_type>;
89 using hash_type = ippl::detail::hash_type<Kokkos::DefaultExecutionSpace::memory_space>;
122 BunchType& bunch, BinningSelector var_selector,
bin_index_type maxBins,
124 const std::string& binningCmdName)
137 Inform msg(
"AdaptBins");
138 msg << level4 <<
"AdaptBins initialized with maxBins = " <<
maxBins_m
235 KOKKOS_INLINE_FUNCTION
342 template <
typename ReducerType>
498 std::vector<size_type>& binCounts,
499 std::vector<value_type>& binWidths)
const override;
516 Inform msg(
"KOKKOS DEBUG");
518 int rank = ippl::Comm->rank();
519 msg << level2 <<
"=====================================" << endl;
520 msg << level2 <<
" Kokkos Debug Information (Rank " << rank <<
")" << endl;
521 msg << level2 <<
"=====================================" << endl;
524#ifdef KOKKOS_ENABLE_OPENMP
525 int num_threads = Kokkos::OpenMP().concurrency();
526 msg << level2 <<
"CPU Threads (OpenMP): " << num_threads << endl;
527#elif defined(KOKKOS_ENABLE_THREADS)
528 int num_threads = Kokkos::Threads().concurrency();
529 msg << level2 <<
"CPU Threads (Kokkos::Threads): " << num_threads << endl;
531 msg << level2 <<
"CPU Threads: No multi-threaded CPU execution space enabled." << endl;
535#if defined(KOKKOS_ENABLE_CUDA)
536 int num_gpus = Kokkos::num_devices();
537 msg << level2 <<
"CUDA Enabled: Rank " << rank <<
" sees " << num_gpus
538 <<
" GPU(s) available." << endl;
539 Kokkos::Cuda cuda_instance;
540 std::stringstream ss;
541 cuda_instance.print_configuration(ss);
542 msg << level2 << ss.str();
543#elif defined(KOKKOS_ENABLE_HIP)
544 int num_gpus = Kokkos::num_devices();
545 msg << level2 <<
"HIP Enabled: Rank " << rank <<
" sees " << num_gpus
546 <<
" GPU(s) available." << endl;
547 Kokkos::Experimental::HIP hip_instance;
548 std::stringstream ss;
549 hip_instance.print_configuration(ss);
550 msg << level2 << ss.str();
552 msg << level2 <<
"CUDA/HIP: GPU support disabled.\n";
556 int default_concurrency = Kokkos::DefaultExecutionSpace().concurrency();
557 msg << level2 <<
"Default Execution Space Concurrency: " << default_concurrency << endl;
558 msg << level2 <<
"Binning cost function parameters: alpha = " <<
binningAlpha_m
561 msg << level2 <<
"=====================================" << endl;
598#include "AdaptBins.tpp"
typename BunchType::Layout_t::value_type value_type
virtual hash_type getHashArray()=0
virtual bin_index_type getCurrentBinCount() const =0
ippl::detail::hash_type< Kokkos::DefaultExecutionSpace::memory_space > hash_type
virtual bin_index_type getMaxBinCount() const =0
virtual void initHistogram(HistoReductionMode modePreference=HistoReductionMode::Standard)=0
virtual value_type getXMin() const =0
virtual Kokkos::RangePolicy getBinIterationPolicy(const bin_index_type &binIndex)=0
virtual const std::string & getBinningCmdName() const =0
virtual size_type getNPartInBin(bin_index_type binIndex, bool global=false)=0
virtual void genAdaptiveHistogram()=0
virtual void doFullRebin(bin_index_type nBins, bool recalculateLimits=true, HistoReductionMode modePreference=HistoReductionMode::Standard)=0
typename BunchType::size_type size_type
virtual void initLocalHisto(HistoReductionMode modePreference=HistoReductionMode::Standard)=0
virtual void instantiateHistogram(bool setToZero=false)=0
virtual ~AdaptBinsBase()=default
virtual void initGlobalHistogram()=0
virtual value_type getBinConfigHost(std::vector< size_type > &binCounts, std::vector< value_type > &binWidths) const =0
virtual void sortContainerByBin()=0
virtual void initLimits()=0
virtual void assignBinsToParticles()=0
virtual value_type getBinWidth() const =0
virtual void executeInitLocalHistoReductionTeamFor()=0
typename BunchType::bin_index_type bin_index_type
A class that bins particles in energy bins and allows for adaptive runtime rebinning.
IpplTimings::TimerRef bInitLimitsT
IpplTimings::TimerRef bVerifySortingT
typename d_histo_type::hview_type hview_type
const std::string & getBinningCmdName() const override
Returns the logical name of the active OPAL BinningCmd definition.
typename d_histo_type::hindex_transform_type hindex_transform_type
typename BunchType::bin_index_type bin_index_type
void executeInitLocalHistoReductionTeamFor() override
Initializes and performs a team-based histogram reduction for particle bins.
typename particle_position_type::view_type position_view_type
std::string binningCmdName_m
Name of the active OPAL BinningCmd definition.
void debug() override
Outputs debug information related to Kokkos and MPI configurations.
AdaptBins(BunchType &bunch, BinningSelector var_selector, bin_index_type maxBins, value_type binningAlpha, value_type binningBeta, value_type desiredWidth, const std::string &binningCmdName)
Constructs an AdaptBins object with a specified maximum number of bins and a selector.
void sortContainerByBin() override
Sorts the container of particles by their bin indices.
value_type binningAlpha_m
Alpha parameter for binning cost function.
value_type binningBeta_m
Beta parameter for binning cost function.
typename h_histo_type_g::hview_type hview_type_g
void assignBinsToParticles() override
Assigns each particle in the bunch to a bin based on its position.
value_type desiredWidth_m
Desired bin width for binning cost function.
Kokkos::RangePolicy getBinIterationPolicy(const bin_index_type &binIndex) override
Returns the bin iteration policy for a given bin index.
void initTimers()
Initializes timers for various operations in the binning process.
BunchType & bunch_m
Borrowed particle container.
value_type binWidth_m
Width of each bin (assumes a uniform histogram).
void initHistogram(HistoReductionMode modePreference=HistoReductionMode::Standard) override
Initializes the local/global histogram based on the Bin attribute.
ippl::detail::hash_type< Kokkos::DefaultExecutionSpace::memory_space > hash_type
IpplTimings::TimerRef bAllReduceLimitsT
void initLimits() override
Initializes the limits for binning based on the particle data.
void print() override
Prints the current global histogram to the Inform output stream.
value_type getBinConfigHost(std::vector< size_type > &binCounts, std::vector< value_type > &binWidths) const override
Extracts the global bin configuration (counts and widths) on the host.
void genAdaptiveHistogram() override
Generates an adaptive histogram based on a fine global histogram.
hash_type getHashArray() override
Returns the index map that sorts the particle container by bin number.
value_type getBinWidth() const override
Returns the average binwidth.
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.
value_type getXMin() const override
Returns the current lower bound of the binning coordinate (xMin).
size_type getNPartInBin(bin_index_type binIndex, bool global=false) override
Retrieves the number of particles in a specified bin.
typename BunchType::particle_position_type particle_position_type
value_type xMax_m
Maximum value of bin attribute.
typename ippl::ParticleAttrib< bin_index_type > bin_type
typename h_histo_type_g::hwidth_view_type hwidth_view_type_g
typename h_histo_type_g::hindex_transform_type hindex_transform_type_g
void setCurrentBinCount(bin_index_type nBins)
Sets the current number of bins and adjusts the bin width.
h_histo_type_g globalBinHisto_m
Global histogram view (over ranks reduced local histograms).
d_histo_type localBinHisto_m
Local histogram view for bin counts.
BinningSelector var_selector_m
Variable selector for binning.
hash_type sortedIndexArr_m
Particle index map that sorts the bunch by bin index.
typename bin_type::view_type bin_view_type
void initLocalHisto(HistoReductionMode modePreference=HistoReductionMode::Standard) override
Initializes a local histogram for particle binning.
void executeInitLocalHistoReduction(ReducerType &to_reduce)
Executes a parallel reduction to initialize the local histogram for particle bins.
void instantiateHistogram(bool setToZero=false) override
Initializes the histogram view for binning and optionally sets it to zero.
typename BinningSelector::value_type value_type
void doFullRebin(bin_index_type nBins, bool recalculateLimits=true, HistoReductionMode modePreference=HistoReductionMode::Standard) override
Performs a full rebinning operation on the bunch.
IpplTimings::TimerRef bSortContainerByBinT
bin_index_type currentBins_m
Current number of bins in use.
typename d_histo_type::dview_type dview_type
bin_index_type getCurrentBinCount() const override
Returns the current number of bins.
IpplTimings::TimerRef bAssignUniformBinsT
void initGlobalHistogram() override
Retrieves the global histogram across all processes.
value_type xMin_m
Minimum value of bin attribute.
typename d_histo_type::dindex_transform_type dindex_transform_type
typename BunchType::size_type size_type
IpplTimings::TimerRef bAllReduceGlobalHistoT
typename d_histo_type::dwidth_view_type dwidth_view_type
bin_view_type getBinView()
Returns a view to the particle bin array.
bin_index_type getMaxBinCount() const override
Gets the maximum number of bins. Will be used for the fine uniform histogram before merging.
const bin_index_type maxBins_m
Maximum number of bins.
IpplTimings::TimerRef bExecuteHistoReductionT
void printPythonArrays() const
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.
typename DeviceViewTraits< UseDualView, view_type >::h_type hview_type
typename DeviceViewTraits< UseDualView, width_view_type >::h_type hwidth_view_type
index_transform_type< Kokkos::DefaultExecutionSpace > dindex_transform_type
size_type getNPartInBin(bin_index_type binIndex)
Retrieves the number of particles in a specified bin.
typename DeviceViewTraits< UseDualView, view_type >::d_type dview_type
index_transform_type< Kokkos::HostSpace > hindex_transform_type
void init()
Synchronizes the histogram view and initializes the bin widths and post-sum.
typename DeviceViewTraits< UseDualView, width_view_type >::d_type dwidth_view_type