OPALX (Object Oriented Parallel Accelerator Library for Exascal) master (dc2a29eed580)
OPALX
Loading...
Searching...
No Matches
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
6
7namespace ParticleBinning {
8
34 template <typename bin_index_type>
36 HistoReductionMode modePreference, bin_index_type binCount) {
37 // Overwrite standard mode if compiled with default host execution space!
38 if (std::is_same<Kokkos::DefaultExecutionSpace, Kokkos::DefaultHostExecutionSpace>::value)
40
41#ifdef OPALX_DEVICE_COMPILATION
42 // In device builds HostOnly reductions are not supported; calling them is a logic error.
43 if (modePreference == HistoReductionMode::HostOnly) {
44 throw OpalException(
45 "ParticleBinning::determineHistoReductionMode",
46 "HistoReductionMode::HostOnly is not supported when OPALX_DEVICE_COMPILATION "
47 "is "
48 "enabled.");
49 }
50#endif
51 // Otherwise choose automatically if Standard and respect preference if not on host and not
52 // standard!
53 if (modePreference == HistoReductionMode::Standard) {
54 return (binCount <= maxArrSize<bin_index_type>) ? HistoReductionMode::ParallelReduce
56 } else {
57 return modePreference;
58 }
59 }
60
88 template <typename bunch_type>
91 using value_type = typename bunch_type::Layout_t::value_type;
92
94 using size_type = typename bunch_type::size_type;
95
97 using position_view_type = typename bunch_type::particle_position_type::view_type;
98
100 const int axis;
101
108 CoordinateSelector(int axis_) : axis(axis_) {}
109
121 void updateDataArr(bunch_type& bunch) { data_arr = bunch.P.getView(); }
122
131 KOKKOS_INLINE_FUNCTION
133 // std::cout << "CoordinateSelector: " << i << std::endl; // TODO: debug, remove later
134 const value_type value = fabs(data_arr(i)[axis]);
135 return value / sqrt(1 + value * value); // Normalize to v/c, so v in [0, 1]
136 }
137 };
138
148 template <typename bunch_type>
150 using value_type = typename bunch_type::Layout_t::value_type;
151 using size_type = typename bunch_type::size_type;
152 using position_view_type = typename bunch_type::particle_position_type::view_type;
153
155 const int axis;
156
157 GammaSelector(int axis_) : axis(axis_) {}
158
159 void updateDataArr(bunch_type& bunch) { data_arr = bunch.P.getView(); }
160
161 KOKKOS_INLINE_FUNCTION
163 const value_type value = data_arr(i)[axis];
164 return sqrt(1 + value * value);
165 }
166 };
167
181 template <typename ViewType>
182 void computeFixSum(const ViewType& input_view, const ViewType& post_sum_view) {
183 using execution_space = typename ViewType::execution_space;
184 using size_type = typename ViewType::size_type;
185 using value_type = typename ViewType::value_type;
186
187 // Ensure the output view has the correct size
188 if (post_sum_view.extent(0) != input_view.extent(0) + 1) {
189 Inform m("computePostSum");
190 m << level4 << "Output view must have size input_view.extent(0) + 1" << endl;
191 ippl::Comm->abort();
192 }
193
194 // Initialize the first element to 0
195 Kokkos::parallel_for(
196 "InitPostSum", Kokkos::RangePolicy<execution_space>(0, 1),
197 KOKKOS_LAMBDA(const size_type) { post_sum_view(0) = 0; });
198
199 // Compute the fix sum
200 Kokkos::parallel_scan(
201 "ComputePostSum", Kokkos::RangePolicy<execution_space>(0, input_view.extent(0)),
202 KOKKOS_LAMBDA(const size_type& i, value_type& partial_sum, bool final) {
203 partial_sum += input_view(i);
204 if (final) {
205 post_sum_view(i + 1) = partial_sum;
206 }
207 });
208 }
209
222 template <typename ValueType, typename SizeType, typename HashType>
223 bool viewIsSorted(const Kokkos::View<ValueType*> view, HashType indices, SizeType npart) {
224 bool sorted = true;
225 Kokkos::parallel_reduce(
226 "CheckSorted", npart - 1,
227 KOKKOS_LAMBDA(const SizeType& i, bool& update) {
228 if (view(indices(i)) > view(indices(i + 1))) update = false;
229 },
230 Kokkos::LAnd<bool>(sorted));
231 return sorted;
232 }
233
234} // namespace ParticleBinning
235
236#endif // BINNINGTOOLS_H
ippl::detail::size_type size_type
TestT value_type
Container_t::bin_index_type bin_index_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 .....
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.
Example struct used to access the binning variable for each particle.
position_view_type data_arr
Kokkos view of the particle data array.
KOKKOS_INLINE_FUNCTION value_type operator()(const size_type &i) const
Returns the value of the binning variable for a given particle index.
void updateDataArr(bunch_type &bunch)
Updates the data array view with the latest particle data.
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).
Selects the gamma factor for binning.
typename bunch_type::size_type size_type
void updateDataArr(bunch_type &bunch)
typename bunch_type::particle_position_type::view_type position_view_type
typename bunch_type::Layout_t::value_type value_type
KOKKOS_INLINE_FUNCTION value_type operator()(const size_type &i) const