OPALX (Object Oriented Parallel Accelerator Library for Exascal) master (dc2a29eed580)
OPALX
Loading...
Searching...
No Matches
AdaptBins.h
Go to the documentation of this file.
1
10#ifndef ADAPT_BINS_H
11#define ADAPT_BINS_H
12
13#include "Ippl.h"
14
15#include <Kokkos_DualView.hpp>
16#include <algorithm>
17#include <string>
18#include <vector>
19
20#include "BinHisto.h"
21#include "BinningTools.h"
22#include "ParallelReduceTools.h"
23
24namespace ParticleBinning {
25
26 template <typename BunchType>
28 public:
29 using value_type = typename BunchType::Layout_t::value_type;
30 using size_type = typename BunchType::size_type;
31 using bin_index_type = typename BunchType::bin_index_type;
32 using hash_type = ippl::detail::hash_type<Kokkos::DefaultExecutionSpace::memory_space>;
33
34 virtual ~AdaptBinsBase() = default;
35
37 virtual const std::string& getBinningCmdName() const = 0;
38 virtual bin_index_type getMaxBinCount() const = 0;
39 virtual value_type getBinWidth() const = 0;
40 virtual value_type getXMin() const = 0;
41 virtual hash_type getHashArray() = 0;
42 virtual void initLimits() = 0;
43 virtual void instantiateHistogram(bool setToZero = false) = 0;
44 virtual void assignBinsToParticles() = 0;
45 virtual void initLocalHisto(
48 virtual void initGlobalHistogram() = 0;
49 virtual void doFullRebin(
50 bin_index_type nBins, bool recalculateLimits = true,
52 virtual void initHistogram(
54 virtual size_type getNPartInBin(bin_index_type binIndex, bool global = false) = 0;
55 virtual void sortContainerByBin() = 0;
56 virtual Kokkos::RangePolicy<> getBinIterationPolicy(const bin_index_type& binIndex) = 0;
57 virtual void genAdaptiveHistogram() = 0;
59 std::vector<size_type>& binCounts, std::vector<value_type>& binWidths) const = 0;
60 virtual void print() = 0;
61 virtual void debug() = 0;
62 };
63
78 template <typename BunchType, typename BinningSelector>
79 class AdaptBins : public AdaptBinsBase<BunchType> {
80 public:
81 // Variable type definitions
82 using value_type = typename BinningSelector::value_type;
83 using particle_position_type = typename BunchType::particle_position_type;
84 using position_view_type = typename particle_position_type::view_type;
85 using size_type = typename BunchType::size_type;
86 using bin_index_type = typename BunchType::bin_index_type;
87 using bin_type = typename ippl::ParticleAttrib<bin_index_type>;
88 using bin_view_type = typename bin_type::view_type;
89 using hash_type = ippl::detail::hash_type<Kokkos::DefaultExecutionSpace::memory_space>;
90
91 // Types from BinHisto.h to manage histogram structures without "auto"
98
104
122 BunchType& bunch, BinningSelector var_selector, bin_index_type maxBins,
123 value_type binningAlpha, value_type binningBeta, value_type desiredWidth,
124 const std::string& binningCmdName)
125 : bunch_m(bunch),
126 var_selector_m(var_selector),
127 maxBins_m(maxBins),
128 binningCmdName_m(binningCmdName),
129 binningAlpha_m(binningAlpha),
130 binningBeta_m(binningBeta),
131 desiredWidth_m(desiredWidth) {
132 // Will be set during usage/rebinning
133 currentBins_m = maxBins;
134
135 initTimers();
136
137 Inform msg("AdaptBins");
138 msg << level4 << "AdaptBins initialized with maxBins = " << maxBins_m
139 << ", alpha = " << binningAlpha_m << ", beta = " << binningBeta_m
140 << ", desiredWidth = " << desiredWidth_m << endl;
141 }
142
146 void initTimers() {
147 bInitLimitsT = IpplTimings::getTimer("bInitLimits");
148 bAllReduceLimitsT = IpplTimings::getTimer("bAllReduceLimits");
149 bAllReduceGlobalHistoT = IpplTimings::getTimer("bAllReduceGlobalHisto");
150 bAssignUniformBinsT = IpplTimings::getTimer("bAssignUniformBins");
151 bExecuteHistoReductionT = IpplTimings::getTimer("bExecuteHistoReduction");
152 bSortContainerByBinT = IpplTimings::getTimer("bSortContainerByBin");
153 bVerifySortingT = IpplTimings::getTimer("bVerifySorting");
154 }
155
161 bin_view_type getBinView() { return bunch_m.Bin.getView(); }
162
169
175 const std::string& getBinningCmdName() const override { return binningCmdName_m; }
176
183 bin_index_type getMaxBinCount() const override { return maxBins_m; }
184
190 value_type getBinWidth() const override { return binWidth_m; }
191
195 value_type getXMin() const override { return xMin_m; }
196
206 // Hard safety: never allow 0 bins (would break reducers and cause div-by-zero).
207 const bin_index_type clampedUpper = (nBins > maxBins_m) ? maxBins_m : nBins;
208 currentBins_m = std::max<bin_index_type>(1, clampedUpper);
210 }
211
218
235 KOKKOS_INLINE_FUNCTION
237 value_type x, value_type xMin, value_type xMax, value_type binWidthInv,
238 bin_index_type numBins);
239
248 void initLimits() override;
249
256 void instantiateHistogram(bool setToZero = false) override;
257
267 void assignBinsToParticles() override;
268
279 HistoReductionMode modePreference = HistoReductionMode::Standard) override;
280
316
342 template <typename ReducerType>
343 void executeInitLocalHistoReduction(ReducerType& to_reduce);
344
354 void initGlobalHistogram() override;
355
372 bin_index_type nBins, bool recalculateLimits = true,
373 HistoReductionMode modePreference = HistoReductionMode::Standard) override {
374 if (recalculateLimits) initLimits();
375 setCurrentBinCount(nBins);
377 initHistogram(modePreference);
378 }
379
386 HistoReductionMode modePreference = HistoReductionMode::Standard) override {
388 initLocalHisto(modePreference);
390
391 // Init both histograms --> buils postSums and widths arrays
394 }
395
412 size_type getNPartInBin(bin_index_type binIndex, bool global = false) override {
413 // shouldn't happen..., "binIndex < 0" unnecessary, since binIndex is usually unsigned;
414 // but just in case the type is changed
415 if (binIndex < 0 || binIndex >= getCurrentBinCount()) {
416 return bunch_m.getTotalNum();
417 }
418
419 if (global) {
420 return globalBinHisto_m.getNPartInBin(binIndex);
421 } else {
422 return localBinHisto_m.getNPartInBin(binIndex);
423 }
424 }
425
446 void sortContainerByBin() override;
447
463 Kokkos::RangePolicy<> getBinIterationPolicy(const bin_index_type& binIndex) override {
464 return localBinHisto_m.getBinIterationPolicy(binIndex);
465 }
466
485 void genAdaptiveHistogram() override;
486
498 std::vector<size_type>& binCounts,
499 std::vector<value_type>& binWidths) const override;
500
508
515 void debug() override {
516 Inform msg("KOKKOS DEBUG"); // , INFORM_ALL_NODES
517
518 int rank = ippl::Comm->rank();
519 msg << level2 << "=====================================" << endl;
520 msg << level2 << " Kokkos Debug Information (Rank " << rank << ")" << endl;
521 msg << level2 << "=====================================" << endl;
522
523// Check number of CPU threads (OpenMP or other CPU execution spaces)
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;
530#else
531 msg << level2 << "CPU Threads: No multi-threaded CPU execution space enabled." << endl;
532#endif
533
534// Check number of GPUs (CUDA/HIP devices)
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();
551#else
552 msg << level2 << "CUDA/HIP: GPU support disabled.\n";
553#endif
554
555 // Additional information on concurrency in the default execution space
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
559 << ", beta = " << binningBeta_m << ", desiredWidth = " << desiredWidth_m << endl;
560
561 msg << level2 << "=====================================" << endl;
562 }
563
564 private:
565 BunchType& bunch_m;
566 BinningSelector var_selector_m;
569 std::string binningCmdName_m;
573
574 // Merging cost function parameters
578
579 // Histograms
583
585
586 // Timers...
587 IpplTimings::TimerRef bInitLimitsT;
588 IpplTimings::TimerRef bAllReduceLimitsT;
589 IpplTimings::TimerRef bAllReduceGlobalHistoT;
590 IpplTimings::TimerRef bAssignUniformBinsT;
591 IpplTimings::TimerRef bExecuteHistoReductionT;
592 IpplTimings::TimerRef bSortContainerByBinT;
593 IpplTimings::TimerRef bVerifySortingT;
594 };
595
596} // namespace ParticleBinning
597
598#include "AdaptBins.tpp"
599
600#endif // ADAPT_BINS_H
typename BunchType::Layout_t::value_type value_type
Definition AdaptBins.h:29
virtual hash_type getHashArray()=0
virtual bin_index_type getCurrentBinCount() const =0
ippl::detail::hash_type< Kokkos::DefaultExecutionSpace::memory_space > hash_type
Definition AdaptBins.h:32
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
Definition AdaptBins.h:30
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 assignBinsToParticles()=0
virtual value_type getBinWidth() const =0
virtual void executeInitLocalHistoReductionTeamFor()=0
typename BunchType::bin_index_type bin_index_type
Definition AdaptBins.h:31
A class that bins particles in energy bins and allows for adaptive runtime rebinning.
Definition AdaptBins.h:79
IpplTimings::TimerRef bInitLimitsT
Definition AdaptBins.h:587
IpplTimings::TimerRef bVerifySortingT
Definition AdaptBins.h:593
typename d_histo_type::hview_type hview_type
Definition AdaptBins.h:94
const std::string & getBinningCmdName() const override
Returns the logical name of the active OPAL BinningCmd definition.
Definition AdaptBins.h:175
typename d_histo_type::hindex_transform_type hindex_transform_type
Definition AdaptBins.h:96
typename BunchType::bin_index_type bin_index_type
Definition AdaptBins.h:86
void executeInitLocalHistoReductionTeamFor() override
Initializes and performs a team-based histogram reduction for particle bins.
typename particle_position_type::view_type position_view_type
Definition AdaptBins.h:84
std::string binningCmdName_m
Name of the active OPAL BinningCmd definition.
Definition AdaptBins.h:569
void debug() override
Outputs debug information related to Kokkos and MPI configurations.
Definition AdaptBins.h:515
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.
Definition AdaptBins.h:121
void sortContainerByBin() override
Sorts the container of particles by their bin indices.
value_type binningAlpha_m
Alpha parameter for binning cost function.
Definition AdaptBins.h:575
value_type binningBeta_m
Beta parameter for binning cost function.
Definition AdaptBins.h:576
typename h_histo_type_g::hview_type hview_type_g
Definition AdaptBins.h:101
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.
Definition AdaptBins.h:577
Kokkos::RangePolicy getBinIterationPolicy(const bin_index_type &binIndex) override
Returns the bin iteration policy for a given bin index.
Definition AdaptBins.h:463
void initTimers()
Initializes timers for various operations in the binning process.
Definition AdaptBins.h:146
BunchType & bunch_m
Borrowed particle container.
Definition AdaptBins.h:565
value_type binWidth_m
Width of each bin (assumes a uniform histogram).
Definition AdaptBins.h:572
void initHistogram(HistoReductionMode modePreference=HistoReductionMode::Standard) override
Initializes the local/global histogram based on the Bin attribute.
Definition AdaptBins.h:385
ippl::detail::hash_type< Kokkos::DefaultExecutionSpace::memory_space > hash_type
Definition AdaptBins.h:89
IpplTimings::TimerRef bAllReduceLimitsT
Definition AdaptBins.h:588
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.
Definition AdaptBins.h:507
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.
Definition AdaptBins.h:217
value_type getBinWidth() const override
Returns the average binwidth.
Definition AdaptBins.h:190
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).
Definition AdaptBins.h:195
size_type getNPartInBin(bin_index_type binIndex, bool global=false) override
Retrieves the number of particles in a specified bin.
Definition AdaptBins.h:412
typename BunchType::particle_position_type particle_position_type
Definition AdaptBins.h:83
value_type xMax_m
Maximum value of bin attribute.
Definition AdaptBins.h:571
typename ippl::ParticleAttrib< bin_index_type > bin_type
Definition AdaptBins.h:87
typename h_histo_type_g::hwidth_view_type hwidth_view_type_g
Definition AdaptBins.h:102
typename h_histo_type_g::hindex_transform_type hindex_transform_type_g
Definition AdaptBins.h:103
void setCurrentBinCount(bin_index_type nBins)
Sets the current number of bins and adjusts the bin width.
Definition AdaptBins.h:205
h_histo_type_g globalBinHisto_m
Global histogram view (over ranks reduced local histograms).
Definition AdaptBins.h:582
d_histo_type localBinHisto_m
Local histogram view for bin counts.
Definition AdaptBins.h:580
BinningSelector var_selector_m
Variable selector for binning.
Definition AdaptBins.h:566
hash_type sortedIndexArr_m
Particle index map that sorts the bunch by bin index.
Definition AdaptBins.h:584
typename bin_type::view_type bin_view_type
Definition AdaptBins.h:88
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
Definition AdaptBins.h:82
void doFullRebin(bin_index_type nBins, bool recalculateLimits=true, HistoReductionMode modePreference=HistoReductionMode::Standard) override
Performs a full rebinning operation on the bunch.
Definition AdaptBins.h:371
IpplTimings::TimerRef bSortContainerByBinT
Definition AdaptBins.h:592
bin_index_type currentBins_m
Current number of bins in use.
Definition AdaptBins.h:568
typename d_histo_type::dview_type dview_type
Definition AdaptBins.h:93
bin_index_type getCurrentBinCount() const override
Returns the current number of bins.
Definition AdaptBins.h:168
IpplTimings::TimerRef bAssignUniformBinsT
Definition AdaptBins.h:590
void initGlobalHistogram() override
Retrieves the global histogram across all processes.
value_type xMin_m
Minimum value of bin attribute.
Definition AdaptBins.h:570
typename d_histo_type::dindex_transform_type dindex_transform_type
Definition AdaptBins.h:97
typename BunchType::size_type size_type
Definition AdaptBins.h:85
IpplTimings::TimerRef bAllReduceGlobalHistoT
Definition AdaptBins.h:589
typename d_histo_type::dwidth_view_type dwidth_view_type
Definition AdaptBins.h:95
bin_view_type getBinView()
Returns a view to the particle bin array.
Definition AdaptBins.h:161
bin_index_type getMaxBinCount() const override
Gets the maximum number of bins. Will be used for the fine uniform histogram before merging.
Definition AdaptBins.h:183
const bin_index_type maxBins_m
Maximum number of bins.
Definition AdaptBins.h:567
IpplTimings::TimerRef bExecuteHistoReductionT
Definition AdaptBins.h:591
void printPythonArrays() const
Definition BinHisto.h:605
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.
Definition BinHisto.h:306
typename DeviceViewTraits< UseDualView, view_type >::h_type hview_type
Definition BinHisto.h:71
typename DeviceViewTraits< UseDualView, width_view_type >::h_type hwidth_view_type
Definition BinHisto.h:77
index_transform_type< Kokkos::DefaultExecutionSpace > dindex_transform_type
Definition BinHisto.h:84
size_type getNPartInBin(bin_index_type binIndex)
Retrieves the number of particles in a specified bin.
Definition BinHisto.h:165
typename DeviceViewTraits< UseDualView, view_type >::d_type dview_type
Definition BinHisto.h:70
index_transform_type< Kokkos::HostSpace > hindex_transform_type
Definition BinHisto.h:86
void init()
Synchronizes the histogram view and initializes the bin widths and post-sum.
Definition BinHisto.h:233
typename DeviceViewTraits< UseDualView, width_view_type >::d_type dwidth_view_type
Definition BinHisto.h:78