OPALX (Object Oriented Parallel Accelerator Library for Exascal) MINIorX
OPALX
BinningTools.h
Go to the documentation of this file.
1#ifndef BINNINGTOOLS_H
2#define BINNINGTOOLS_H
3
4#include "ParallelReduceTools.h" // needed for HistoReductionMode and maxArrSize
5
6namespace ParticleBinning {
7
32 template <typename bin_index_type>
33 HistoReductionMode determineHistoReductionMode(HistoReductionMode modePreference, bin_index_type binCount) {
34 // Overwrite standard mode if compiled with default host execution space!
35 if (std::is_same<Kokkos::DefaultExecutionSpace, Kokkos::DefaultHostExecutionSpace>::value) return HistoReductionMode::HostOnly;
36
37 // Otherwise choose automatically if Standard and respect preference if not on host and not standard!
38 if (modePreference == HistoReductionMode::Standard) { // || modePreference == HistoReductionMode::HostOnly
39 //if (modePreference == HistoReductionMode::HostOnly) {
40 // std::cerr << "Warning: HostOnly mode is not supported on CUDA! Switching to Standard mode." << std::endl;
41 //}
43 } else {
44 return modePreference;
45 }
46 }
47
48
76 template <typename bunch_type>
79 using value_type = typename bunch_type::Layout_t::value_type;
80
82 using size_type = typename bunch_type::size_type;
83
85 using position_view_type = typename bunch_type::particle_position_type::view_type;
86
88 const int axis;
89
97 : axis(axis_) {}
98
110 void updateDataArr(std::shared_ptr<bunch_type> bunch) { data_arr = bunch->P.getView(); }
111
120 KOKKOS_INLINE_FUNCTION
122 //std::cout << "CoordinateSelector: " << i << std::endl; // TODO: debug, remove later
123 const value_type value = fabs(data_arr(i)[axis]);
124 return value / sqrt(1 + value * value); // Normalize to v/c, so v in [0, 1]
125 }
126 };
127
128
139 template <typename ViewType>
140 void computeFixSum(const ViewType& input_view, const ViewType& post_sum_view) {
141 using execution_space = typename ViewType::execution_space;
142 using size_type = typename ViewType::size_type;
143 using value_type = typename ViewType::value_type;
144
145 // Ensure the output view has the correct size
146 if (post_sum_view.extent(0) != input_view.extent(0) + 1) {
147 Inform m("computePostSum");
148 m << "Output view must have size input_view.extent(0) + 1" << endl;
149 ippl::Comm->abort();
150 }
151
152 // Initialize the first element to 0
153 Kokkos::parallel_for("InitPostSum", Kokkos::RangePolicy<execution_space>(0, 1),
154 KOKKOS_LAMBDA(const size_type) {
155 post_sum_view(0) = 0;
156 });
157
158 // Compute the fix sum
159 Kokkos::parallel_scan("ComputePostSum", Kokkos::RangePolicy<execution_space>(0, input_view.extent(0)),
160 KOKKOS_LAMBDA(const size_type& i, value_type& partial_sum, bool final) {
161 partial_sum += input_view(i);
162 if (final) {
163 post_sum_view(i + 1) = partial_sum;
164 }
165 });
166 }
167
168
181 template <typename ValueType, typename SizeType, typename HashType>
182 bool viewIsSorted (const Kokkos::View<ValueType*> view, HashType indices, SizeType npart) {
183 bool sorted = true;
184 Kokkos::parallel_reduce("CheckSorted", npart - 1, KOKKOS_LAMBDA(const SizeType& i, bool& update) {
185 if (view(indices(i)) > view(indices(i + 1))) update = false;
186 }, Kokkos::LAnd<bool>(sorted));
187 return sorted;
188 }
189
190
191} // namespace ParticleBinning
192
193#endif // BINNINGTOOLS_H
ippl::detail::size_type size_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 .....
constexpr IndexType maxArrSize
Maximum allowed array size for compile-time array reduction types.
HistoReductionMode determineHistoReductionMode(HistoReductionMode modePreference, bin_index_type binCount)
Determines the appropriate histogram reduction mode based on user preference, bin count,...
bool viewIsSorted(const Kokkos::View< ValueType * > view, HashType indices, SizeType npart)
Checks if the elements in a Kokkos::View are sorted in non-decreasing order.
position_view_type data_arr
Kokkos view of the particle data array.
void updateDataArr(std::shared_ptr< bunch_type > bunch)
Updates the data array view with the latest particle data.
KOKKOS_INLINE_FUNCTION value_type operator()(const size_type &i) const
Returns the value of the binning variable for a given particle index.
typename bunch_type::particle_position_type::view_type position_view_type
Type representing the view of particle positions.
const int axis
Index of the coordinate axis to use for binning.
CoordinateSelector(int axis_)
Constructs a CoordinateSelector for a specific axis.
typename bunch_type::size_type size_type
Type representing the size of the particle bunch.
typename bunch_type::Layout_t::value_type value_type
Type representing the value of the binning variable (e.g., position or velocity).