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>
63 bool UseDualView =
false,
class... Properties>
68 UseDualView, Kokkos::DualView<
size_type*, Properties...>,
75 UseDualView, Kokkos::DualView<
value_type*, Properties...>,
81 template <
class... Args>
84 Kokkos::DefaultExecutionSpace>;
145 if (
this == &other)
return *
this;
166 if constexpr (UseDualView) {
168 }
else if (std::is_same<typename hview_type::memory_space, Kokkos::HostSpace>::value) {
171 std::cerr <<
"Warning: Accessing BinHisto.getNPartInBin without DualView might be "
174 Kokkos::View<size_type, Kokkos::HostSpace> host_scalar(
"host_scalar");
175 Kokkos::deep_copy(host_scalar, Kokkos::subview(
histogram_m, binIndex));
176 return host_scalar();
212 template <
typename Histogram_t>
214 using other_dwidth_view_type =
typename Histogram_t::dwidth_view_type;
217 other.template getDeviceView<other_dwidth_view_type>(other.getBinWidths()));
218 if constexpr (UseDualView) {
251 using execution_space =
typename dwidth_view_type::execution_space;
256 Kokkos::parallel_for(
257 "InitConstBinWidths", Kokkos::RangePolicy<execution_space>(0,
numBins_m),
258 KOKKOS_LAMBDA(
const size_t i) { dWidthView(i) = binWidth; });
261 if constexpr (UseDualView) {
278 computeFixSum<dview_type>(
282 if constexpr (UseDualView) {
308 if constexpr (UseDualView) {
309 return Kokkos::RangePolicy<>(
311 postSum_m.view_host()(binIndex1 + numBins));
313 std::cerr <<
"Warning: Accessing BinHisto.getBinIterationPolicy without DualView "
314 "might be inefficient!"
316 Kokkos::View<bin_index_type[2], Kokkos::HostSpace> host_ranges(
"host_scalar");
319 Kokkos::subview(
postSum_m, std::make_pair(binIndex1, binIndex1 + numBins)));
320 return Kokkos::RangePolicy<>(host_ranges(0), host_ranges(1));
344 if constexpr (UseDualView) {
346 std::cerr <<
"Warning: Histogram was modified on host AND device -- "
347 "overwriting changes on host."
366 if constexpr (UseDualView)
histogram_m.modify_device();
376 if constexpr (UseDualView)
histogram_m.modify_host();
391 template <
typename return_type,
typename HistogramType>
393 if constexpr (UseDualView) {
394 return histo.view_device();
410 template <
typename return_type,
typename HistogramType>
412 if constexpr (UseDualView) {
413 return histo.view_host();
576 if (ippl::Comm->rank() != 0)
return;
586 os << std::left << std::setw(10) <<
"Bin" << std::right << std::setw(12) <<
"Count"
587 << std::setw(16) <<
"Width\n";
589 os << std::string(38,
'-') <<
"\n";
594 os << std::left << std::setw(10) << i
595 << std::right << std::setw(12) << countsHost(i) << std::fixed << std::setw(16)
596 << std::setprecision(6)
597 <<
static_cast<double>(widthsHost(i))
606 if (ippl::Comm->rank() != 0)
return;
612 Inform msg(
"AdaptBins::printPythonArrays");
615 msg << level5 <<
"bin_counts = np.array([";
617 msg << level5 << hostCounts(i);
618 if (i <
numBins_m - 1) msg << level5 <<
", ";
620 msg << level5 <<
"])" << endl;
623 msg << level5 <<
"bin_widths = np.array([";
625 msg << level5 << std::fixed << std::setprecision(6) << hostWidths(i);
626 if (i <
numBins_m - 1) msg << level5 <<
", ";
628 msg << level5 <<
"])" << endl;
634#include "BinHisto.tpp"
ippl::detail::size_type size_type
Container_t::bin_index_type bin_index_type
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() const
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
index_transform_type< Kokkos::DefaultExecutionSpace > dindex_transform_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).
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. It will be an array of ...
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).
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
std::conditional_t< UseDualView, Kokkos::DualView< size_type *, Properties... >, Kokkos::View< size_type *, Properties... > > 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.
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.