19 template <
bool UseDualView,
typename ViewType>
25 template <
typename ViewType>
27 using h_type =
typename ViewType::t_host;
28 using d_type =
typename ViewType::t_dev;
34 template <
typename ViewType>
57 template <
typename size_type,
typename bin_index_type,
typename value_type,
58 bool UseDualView =
false,
class... Properties>
64 Kokkos::DualView<
size_type*, Properties...>,
71 Kokkos::DualView<value_type*, Properties...>,
72 Kokkos::View<value_type*, Properties...>>;
77 template <
class... Args>
98 Histogram(std::string debug_name, bin_index_type numBins, value_type totalBinWidth,
133 if (
this == &other)
return *
this;
153 if constexpr (UseDualView) {
155 }
else if (std::is_same<typename hview_type::memory_space, Kokkos::HostSpace>::value) {
158 std::cerr <<
"Warning: Accessing BinHisto.getNPartInBin without DualView might be inefficient!" <<
std::endl;
159 Kokkos::View<size_type, Kokkos::HostSpace> host_scalar(
"host_scalar");
160 Kokkos::deep_copy(host_scalar, Kokkos::subview(
histogram_m, binIndex));
161 return host_scalar();
194 template <
typename Histogram_t>
196 using other_dwidth_view_type =
typename Histogram_t::dwidth_view_type;
197 Kokkos::deep_copy(getDeviceView<dwidth_view_type>(
binWidths_m),
198 other.template getDeviceView<other_dwidth_view_type>(other.getBinWidths()));
199 if constexpr (UseDualView) {
231 const value_type binWidth = constBinWidth /
numBins_m;
232 using execution_space =
typename dwidth_view_type::execution_space;
237 Kokkos::RangePolicy<execution_space>(0,
numBins_m), KOKKOS_LAMBDA(
const size_t i) {
238 dWidthView(i) = binWidth;
243 if constexpr (UseDualView) {
260 computeFixSum<dview_type>(getDeviceView<dview_type>(
histogram_m), getDeviceView<dview_type>(
postSum_m));
263 if constexpr (UseDualView) {
287 if constexpr (UseDualView) {
288 return Kokkos::RangePolicy<>(
postSum_m.h_view(binIndex1),
postSum_m.h_view(binIndex1 + numBins));
290 std::cerr <<
"Warning: Accessing BinHisto.getBinIterationPolicy without DualView might be inefficient!" <<
std::endl;
291 Kokkos::View<bin_index_type[2], Kokkos::HostSpace> host_ranges(
"host_scalar");
292 Kokkos::deep_copy(host_ranges, Kokkos::subview(
postSum_m, std::make_pair(binIndex1, binIndex1 + numBins)));
293 return Kokkos::RangePolicy<>(host_ranges(0), host_ranges(1));
318 if constexpr (UseDualView) {
320 std::cerr <<
"Warning: Histogram was modified on host AND device -- overwriting changes on host." <<
std::endl;
358 template <
typename return_type,
typename HistogramType>
360 if constexpr (UseDualView) {
361 return histo.view_device();
378 template <
typename return_type,
typename HistogramType>
380 if constexpr (UseDualView) {
381 return histo.view_host();
471 const value_type& sumWidth,
549 << std::setw(10) <<
"Bin"
551 << std::setw(12) <<
"Count"
552 << std::setw(16) <<
"Width\n";
554 os << std::string(38,
'-') <<
"\n";
558 for (bin_index_type i = 0; i <
numBins_m; ++i) {
559 os << std::left << std::setw(10) << i
560 << std::right << std::setw(12) << countsHost(i)
561 << std::fixed << std::setw(16) << std::setprecision(6)
562 <<
static_cast<double>(widthsHost(i))
577 std::cout <<
"bin_counts = np.array([";
578 for (bin_index_type i = 0; i <
numBins_m; ++i) {
579 std::cout << hostCounts(i);
580 if (i <
numBins_m - 1) std::cout <<
", ";
585 std::cout <<
"bin_widths = np.array([";
586 for (bin_index_type i = 0; i <
numBins_m; ++i) {
587 std::cout << std::fixed << std::setprecision(6) << hostWidths(i);
588 if (i <
numBins_m - 1) std::cout <<
", ";
597#include "BinHisto.tpp"
ippl::detail::size_type size_type
Inform & endl(Inform &inf)
typename ViewType::t_dev d_type
typename ViewType::t_host h_type
Traits class to extract host and device view types from a given ViewType.
void parallel_for(const std::string &name, const ExecPolicy &policy, const FunctorType &functor)
std::unique_ptr< mpi::Communicator > Comm
Template class providing adaptive particle histogram binning with support for Kokkos Views and DualVi...
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.
IpplTimings::TimerRef bMergeBinsT
void printPythonArrays() const
value_type binningBeta_m
Alpha parameter for the adaptive binning (merging) cost function.
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.
static constexpr return_type getHostView(HistogramType histo)
Retrieves a host view of the given histogram.
Histogram()=default
Default constructor for the Histogram class.
Kokkos::View< bin_index_type *, Args... > index_transform_type
value_type binningAlpha_m
Total width of all bins combined.
view_type getHistogram()
Returns the Kokkos View containing the histogram bin counts.
typename DeviceViewTraits< UseDualView, view_type >::h_type hview_type
typename DeviceViewTraits< UseDualView, width_view_type >::h_type hwidth_view_type
value_type desiredWidth_m
Beta parameter for the adaptive binning (merging) cost function.
size_type getNPartInBin(bin_index_type binIndex)
Retrieves the number of particles in a specified bin.
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.
view_type postSum_m
View storing the widths of the bins.
value_type totalBinWidth_m
Number of bins in the histogram.
bin_index_type numBins_m
Debug name for identifying the histogram instance.
void initPostSum()
Initializes and computes the post-sum for the histogram.
void initConstBinWidths(const value_type constBinWidth)
Initializes the bin widths with a constant value.
typename DeviceViewTraits< UseDualView, view_type >::d_type dview_type
view_type getPostSum()
Returns the Kokkos View containing the post-sum of bin counts.
IpplTimings::TimerRef bDeviceSyncronizationT
View storing the cumulative sum of bin counts (used in sorting, generating range policies).
index_transform_type< Kokkos::DefaultExecutionSpace > dindex_transform_type
hindex_transform_type mergeBins()
Merges bins in a histogram to reduce the number of bins while minimizing a cost function.
width_view_type getBinWidths() const
Returns the Kokkos View of bin widths in the current histogram configuration.
std::conditional_t< UseDualView, Kokkos::DualView< value_type *, Properties... >, Kokkos::View< value_type *, Properties... > > width_view_type
view_type histogram_m
Desired width for the adaptive binning (merging) cost function.
void sync()
Synchronizes the histogram data between host and device.
void modify_host()
If a DualView is used, it sets the flag on the view that the host has been modified.
void copyBinWidths(const Histogram_t &other)
Sets the bin widths by copying them from a different Histogram instance (usually neccessary after mer...
width_view_type binWidths_m
View storing the particle counts in each bin.
index_transform_type< Kokkos::HostSpace > hindex_transform_type
void initTimers()
Initializes timers for various operations in the binning process.
void modify_device()
If a DualView is used, it sets the flag on the view that the device has been modified.
void copyFields(const Histogram &other)
Copies the fields from another Histogram object.
Histogram & operator=(const Histogram &other)
Assignment operator for copying the fields from another Histogram object.
void init()
Synchronizes the histogram view and initializes the bin widths and post-sum.
void instantiateHistograms()
Instantiates the histogram, bin widths, and post-sum views (Possibly DualView).
std::conditional_t< UseDualView, Kokkos::DualView< size_type *, Properties... >, Kokkos::View< size_type *, Properties... > > view_type
static constexpr return_type getDeviceView(HistogramType histo)
Retrieves the device view of the histogram.
IpplTimings::TimerRef bHistogramInitT
typename DeviceViewTraits< UseDualView, width_view_type >::d_type dwidth_view_type
void printHistogram(std::ostream &os=std::cout)
Prints a nicely formatted table of bin indices, counts, and widths.
size_type getCurrentBinCount() const
Returns the current number of bins in the histogram.
Histogram(const Histogram &other)
Default destructor for the Histogram class.
Timing::TimerRef TimerRef
static TimerRef getTimer(const char *nm)
static void stopTimer(TimerRef t)
static void startTimer(TimerRef t)