OPALX (Object Oriented Parallel Accelerator Library for Exascal) MINIorX
OPALX
AdaptBins.h
Go to the documentation of this file.
1
9
10#ifndef ADAPT_BINS_H
11#define ADAPT_BINS_H
12
13#include "Ippl.h"
14
15#include <Kokkos_DualView.hpp>
16#include "ParallelReduceTools.h"
17#include "BinningTools.h"
18#include "BinHisto.h"
19
20namespace ParticleBinning {
21
35 template <typename BunchType, typename BinningSelector>
36 class AdaptBins {
37 public:
38 // Variable type definitions
39 using value_type = typename BinningSelector::value_type;
40 using particle_position_type = typename BunchType::particle_position_type;
41 using position_view_type = typename particle_position_type::view_type;
42 using size_type = typename BunchType::size_type;
43 using bin_index_type = typename BunchType::bin_index_type;
44 using bin_type = typename ippl::ParticleAttrib<bin_index_type>;
45 using bin_view_type = typename bin_type::view_type;
46 using hash_type = ippl::detail::hash_type<Kokkos::DefaultExecutionSpace::memory_space>;
47
48 // Types from BinHisto.h to manage histogram structures without "auto"
55
59
74 AdaptBins(std::shared_ptr<BunchType> bunch, BinningSelector var_selector, bin_index_type maxBins,
75 value_type binningAlpha, value_type binningBeta, value_type desiredWidth)
76 : bunch_m(bunch)
77 , var_selector_m(var_selector)
78 , maxBins_m(maxBins)
79 , binningAlpha_m(binningAlpha)
80 , binningBeta_m(binningBeta)
81 , desiredWidth_m(desiredWidth) {
82
83 // Will be set during usage/rebinning
84 currentBins_m = maxBins;
85
86 initTimers();
87
88 Inform msg("AdaptBins");
89 msg << "AdaptBins initialized with maxBins = " << maxBins_m
90 << ", alpha = " << binningAlpha_m
91 << ", beta = " << binningBeta_m
92 << ", desiredWidth = " << desiredWidth_m << endl;
93 }
94
95
99 void initTimers() {
100 bInitLimitsT = IpplTimings::getTimer("bInitLimits");
101 bAllReduceLimitsT = IpplTimings::getTimer("bAllReduceLimits");
102 bAllReduceGlobalHistoT = IpplTimings::getTimer("bAllReduceGlobalHisto");
103 bAssignUniformBinsT = IpplTimings::getTimer("bAssignUniformBins");
104 bExecuteHistoReductionT = IpplTimings::getTimer("bExecuteHistoReduction");
105 bSortContainerByBinT = IpplTimings::getTimer("bSortContainerByBin");
106 bVerifySortingT = IpplTimings::getTimer("bVerifySorting");
107 }
108
114 bin_view_type getBinView() { return bunch_m->Bin.getView(); }
115
122
129
136
145 currentBins_m = (nBins > maxBins_m) ? maxBins_m : nBins;
147 }
148
155
172 KOKKOS_INLINE_FUNCTION
173 static bin_index_type getBin(value_type x, value_type xMin, value_type xMax, value_type binWidthInv, bin_index_type numBins);
174
184
191 void instantiateHistogram(bool setToZero = false);
192
203
214
246
268 template<typename ReducerType>
269 void executeInitLocalHistoReduction(ReducerType& to_reduce);
270
280
295 void doFullRebin(bin_index_type nBins, bool recalculateLimits = true, HistoReductionMode modePreference = HistoReductionMode::Standard) {
296 if (recalculateLimits) initLimits();
297 setCurrentBinCount(nBins);
299 initHistogram(modePreference);
300 }
301
309 initLocalHisto(modePreference);
311
312 // Init both histograms --> buils postSums and widths arrays
313 localBinHisto_m.init();
314 globalBinHisto_m.init();
315 }
316
332 size_type getNPartInBin(bin_index_type binIndex, bool global = false) {
333 // shouldn't happen..., "binIndex < 0" unnecessary, since binIndex is usually unsigned; but just in case the type is changed
334 if (binIndex < 0 || binIndex >= getCurrentBinCount()) { return bunch_m->getTotalNum(); }
335
336 if (global) {
337 return globalBinHisto_m.getNPartInBin(binIndex);
338 } else {
339 return localBinHisto_m.getNPartInBin(binIndex);
340 }
341 }
342
364
379 Kokkos::RangePolicy<> getBinIterationPolicy(const bin_index_type& binIndex) {
380 return localBinHisto_m.getBinIterationPolicy(binIndex);
381 }
382
399
406 void print() {
407 globalBinHisto_m.printPythonArrays();
408 }
409
416 void debug() {
417 Inform msg("KOKKOS DEBUG"); // , INFORM_ALL_NODES
418
419 int rank = ippl::Comm->rank();
420 msg << "=====================================" << endl;
421 msg << " Kokkos Debug Information (Rank " << rank << ")" << endl;
422 msg << "=====================================" << endl;
423
424 // Check number of CPU threads (OpenMP or other CPU execution spaces)
425 #ifdef KOKKOS_ENABLE_OPENMP
426 int num_threads = Kokkos::OpenMP::concurrency();
427 msg << "CPU Threads (OpenMP): " << num_threads << endl;
428 #elif defined(KOKKOS_ENABLE_THREADS)
429 int num_threads = Kokkos::Threads::concurrency();
430 msg << "CPU Threads (Kokkos::Threads): " << num_threads << endl;
431 #else
432 msg << "CPU Threads: No multi-threaded CPU execution space enabled." << endl;
433 #endif
434
435 // Check number of GPUs (CUDA devices)
436 #ifdef KOKKOS_ENABLE_CUDA
437 int num_gpus = Kokkos::Cuda::detect_device_count();
438 msg << "CUDA Enabled: Rank " << rank << " sees " << num_gpus << " GPU(s) available." << endl;
439 Kokkos::Cuda cuda_instance;
440 std::stringstream ss;
441 cuda_instance.print_configuration(ss);
442 msg << ss.str();
443 #else
444 msg << "CUDA: GPU support disabled.\n";
445 #endif
446
447 // Additional information on concurrency in the default execution space
448 int default_concurrency = Kokkos::DefaultExecutionSpace::concurrency();
449 msg << "Default Execution Space Concurrency: " << default_concurrency << endl;
450 msg << "Binning cost function parameters: alpha = " << binningAlpha_m << ", beta = " << binningBeta_m << ", desiredWidth = " << desiredWidth_m << endl;
451
452 msg << "=====================================" << endl;
453 }
454
483 template <typename T, unsigned Dim>
484 VField_t<T, Dim>& LTrans(VField_t<T, Dim>& field, const bin_index_type& currentBin); // TODO: may want to add usage of c constant when it exists...
485
486 private:
487 std::shared_ptr<BunchType> bunch_m;
488 BinningSelector var_selector_m;
494
495 // Merging cost function parameters
499
500 // Histograms
503
505
506 // Timers...
507 IpplTimings::TimerRef bInitLimitsT;
508 IpplTimings::TimerRef bAllReduceLimitsT;
509 IpplTimings::TimerRef bAllReduceGlobalHistoT;
510 IpplTimings::TimerRef bAssignUniformBinsT;
511 IpplTimings::TimerRef bExecuteHistoReductionT;
512 IpplTimings::TimerRef bSortContainerByBinT;
513 IpplTimings::TimerRef bVerifySortingT;
514 };
515
516}
517
518#include "AdaptBins.tpp"
519
520#endif // ADAPT_BINS_H
521
522
ippl::Field< ippl::Vector< T, Dim >, Dim, ViewArgs... > VField_t
Definition PBunchDefs.h:33
IpplTimings::TimerRef bInitLimitsT
Definition AdaptBins.h:507
void assignBinsToParticles()
Assigns each particle in the bunch to a bin based on its position.
IpplTimings::TimerRef bVerifySortingT
Definition AdaptBins.h:513
typename d_histo_type::hview_type hview_type
Definition AdaptBins.h:51
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.
Definition AdaptBins.h:74
typename d_histo_type::hindex_transform_type hindex_transform_type
Definition AdaptBins.h:53
typename BunchType::bin_index_type bin_index_type
Definition AdaptBins.h:43
Histogram< size_type, bin_index_type, value_type, true > d_histo_type
Definition AdaptBins.h:49
typename particle_position_type::view_type position_view_type
Definition AdaptBins.h:41
Histogram< size_type, bin_index_type, value_type, false, Kokkos::HostSpace > h_histo_type_g
Definition AdaptBins.h:56
size_type getNPartInBin(bin_index_type binIndex, bool global=false)
Retrieves the number of particles in a specified bin.
Definition AdaptBins.h:332
std::shared_ptr< BunchType > bunch_m
Shared pointer to the particle container.
Definition AdaptBins.h:487
void initGlobalHistogram()
Retrieves the global histogram across all processes.
value_type binningAlpha_m
Alpha parameter for binning cost function.
Definition AdaptBins.h:496
value_type binningBeta_m
Beta parameter for binning cost function.
Definition AdaptBins.h:497
typename h_histo_type_g::hview_type hview_type_g
Definition AdaptBins.h:57
value_type desiredWidth_m
Desired bin width for binning cost function.
Definition AdaptBins.h:498
VField_t< T, Dim > & LTrans(VField_t< T, Dim > &field, const bin_index_type &currentBin)
Applies a Lorentz transformation to a given vector field based on particle velocities.
void initLocalHisto(HistoReductionMode modePreference=HistoReductionMode::Standard)
Initializes a local histogram for particle binning.
void initTimers()
Initializes timers for various operations in the binning process.
Definition AdaptBins.h:99
bin_index_type getCurrentBinCount() const
Returns the current number of bins.
Definition AdaptBins.h:121
value_type getBinWidth() const
Returns the average binwidth.
Definition AdaptBins.h:135
Kokkos::RangePolicy getBinIterationPolicy(const bin_index_type &binIndex)
Returns the bin iteration policy for a given bin index.
Definition AdaptBins.h:379
value_type binWidth_m
Width of each bin (assumes a uniform histogram).
Definition AdaptBins.h:493
ippl::detail::hash_type< Kokkos::DefaultExecutionSpace::memory_space > hash_type
Definition AdaptBins.h:46
IpplTimings::TimerRef bAllReduceLimitsT
Definition AdaptBins.h:508
void initHistogram(HistoReductionMode modePreference=HistoReductionMode::Standard)
Initializes the local/global histogram based on the Bin attribute.
Definition AdaptBins.h:307
hash_type getHashArray()
Returns the index map that sorts the particle container by bin number.
Definition AdaptBins.h:154
void instantiateHistogram(bool setToZero=false)
Initializes the histogram view for binning and optionally sets it to zero.
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.
void debug()
Outputs debug information related to Kokkos and MPI configurations.
Definition AdaptBins.h:416
typename BunchType::particle_position_type particle_position_type
Definition AdaptBins.h:40
value_type xMax_m
Maximum value of bin attribute.
Definition AdaptBins.h:492
void print()
Prints the current global histogram to the Inform output stream.
Definition AdaptBins.h:406
typename ippl::ParticleAttrib< bin_index_type > bin_type
Definition AdaptBins.h:44
void initLimits()
Initializes the limits for binning based on the particle data.
typename h_histo_type_g::hindex_transform_type hindex_transform_type_g
Definition AdaptBins.h:58
void setCurrentBinCount(bin_index_type nBins)
Sets the current number of bins and adjusts the bin width.
Definition AdaptBins.h:144
h_histo_type_g globalBinHisto_m
Global histogram view (over ranks reduced local histograms).
Definition AdaptBins.h:502
d_histo_type localBinHisto_m
Local histogram view for bin counts.
Definition AdaptBins.h:501
BinningSelector var_selector_m
Variable selector for binning.
Definition AdaptBins.h:488
hash_type sortedIndexArr_m
Particle index map that sorts the bunch by bin index.
Definition AdaptBins.h:504
typename bin_type::view_type bin_view_type
Definition AdaptBins.h:45
void executeInitLocalHistoReduction(ReducerType &to_reduce)
Executes a parallel reduction to initialize the local histogram for particle bins.
bin_index_type getMaxBinCount() const
Gets the maximum number of bins. Will be used for the fine uniform histogram before merging.
Definition AdaptBins.h:128
void executeInitLocalHistoReductionTeamFor()
Initializes and performs a team-based histogram reduction for particle bins.
typename BinningSelector::value_type value_type
Definition AdaptBins.h:39
void genAdaptiveHistogram()
Generates an adaptive histogram based on a fine global histogram.
IpplTimings::TimerRef bSortContainerByBinT
Definition AdaptBins.h:512
bin_index_type currentBins_m
Current number of bins in use.
Definition AdaptBins.h:490
typename d_histo_type::dview_type dview_type
Definition AdaptBins.h:50
IpplTimings::TimerRef bAssignUniformBinsT
Definition AdaptBins.h:510
value_type xMin_m
Minimum value of bin attribute.
Definition AdaptBins.h:491
typename d_histo_type::dindex_transform_type dindex_transform_type
Definition AdaptBins.h:54
typename BunchType::size_type size_type
Definition AdaptBins.h:42
IpplTimings::TimerRef bAllReduceGlobalHistoT
Definition AdaptBins.h:509
typename d_histo_type::dwidth_view_type dwidth_view_type
Definition AdaptBins.h:52
bin_view_type getBinView()
Returns a view to the particle bin array.
Definition AdaptBins.h:114
void doFullRebin(bin_index_type nBins, bool recalculateLimits=true, HistoReductionMode modePreference=HistoReductionMode::Standard)
Performs a full rebinning operation on the bunch.
Definition AdaptBins.h:295
void sortContainerByBin()
Sorts the container of particles by their bin indices.
const bin_index_type maxBins_m
Maximum number of bins.
Definition AdaptBins.h:489
IpplTimings::TimerRef bExecuteHistoReductionT
Definition AdaptBins.h:511
Template class providing adaptive particle histogram binning with support for Kokkos Views and DualVi...
Definition BinHisto.h:59
typename DeviceViewTraits< UseDualView, view_type >::h_type hview_type
Definition BinHisto.h:67
typename DeviceViewTraits< UseDualView, view_type >::d_type dview_type
Definition BinHisto.h:66
index_transform_type< Kokkos::DefaultExecutionSpace > dindex_transform_type
Definition BinHisto.h:79
index_transform_type< Kokkos::HostSpace > hindex_transform_type
Definition BinHisto.h:80
typename DeviceViewTraits< UseDualView, width_view_type >::d_type dwidth_view_type
Definition BinHisto.h:74