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,
99 value_type binningAlpha, value_type binningBeta, value_type desiredWidth)
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;
199 if constexpr (UseDualView) {
231 const value_type binWidth = constBinWidth /
numBins_m;
232 using execution_space =
typename dwidth_view_type::execution_space;
236 Kokkos::parallel_for(
"InitConstBinWidths",
237 Kokkos::RangePolicy<execution_space>(0,
numBins_m), KOKKOS_LAMBDA(
const size_t i) {
238 dWidthView(i) = binWidth;
243 if constexpr (UseDualView) {
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,
539 if (ippl::Comm->rank() != 0)
return;
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))
571 if (ippl::Comm->rank() != 0)
return;
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 <<
", ";
582 std::cout <<
"])" << std::endl;
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 <<
", ";
590 std::cout <<
"])" << std::endl;
ippl::detail::size_type size_type
typename ViewType::t_dev d_type
void computeFixSum(const ViewType &input_view, const ViewType &post_sum_view)
Computes the post- or prefix-sum of the input view and stores the result in the .....
typename ViewType::t_host h_type
Traits class to extract host and device view types from a given ViewType.
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
std::conditional_t< UseDualView, Kokkos::DualView< size_type *, Properties... >, Kokkos::View< size_type *, Properties... > > view_type
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.
std::conditional_t< UseDualView, Kokkos::DualView< value_type *, Properties... >, Kokkos::View< value_type *, Properties... > > width_view_type
Histogram()=default
Default constructor for the Histogram class.
Kokkos::View< bin_index_type *, Args... > index_transform_type
value_type binningAlpha_m
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
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.
value_type totalBinWidth_m
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
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. It will be an array of ...
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
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)
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.