5#include <Kokkos_Core.hpp>
15template<
typename ValueType>
20template<
typename Kernel>
24 } -> std::same_as<void>;
27template<
typename Executor, parallelForKernel Kernel>
29 [[maybe_unused]]
const Executor& exec,
30 std::pair<localIdx, localIdx> range,
32 std::string
name =
"parallelFor"
35 auto [start, end] = range;
36 if constexpr (std::is_same<std::remove_reference_t<Executor>,
SerialExecutor>::value)
38 for (
localIdx i = start; i < end; i++)
45 using runOn =
typename Executor::exec;
48 Kokkos::RangePolicy<runOn>(start, end),
49 KOKKOS_LAMBDA(
const localIdx i) { kernel(i); }
55template<parallelForKernel Kernel>
58 std::pair<localIdx, localIdx> range,
60 std::string
name =
"parallelFor"
63 std::visit([&](
const auto& e) {
parallelFor(e, range, kernel,
name); }, exec);
67template<
typename Kernel,
typename ValueType>
71 } -> std::same_as<ValueType>;
74template<
typename Executor,
typename ValueType, parallelForVectorKernel<ValueType> Kernel>
76 [[maybe_unused]]
const Executor& exec,
79 std::string
name =
"parallelFor"
82 auto view = field.
view();
83 if constexpr (std::is_same<std::remove_reference_t<Executor>,
SerialExecutor>::value)
86 for (
localIdx i = 0; i < fieldSize; i++)
93 using runOn =
typename Executor::exec;
96 Kokkos::RangePolicy<runOn>(0, field.
size()),
97 KOKKOS_LAMBDA(
const localIdx i) { view[i] = kernel(i); }
102template<
typename ValueType, parallelForVectorKernel<ValueType> Kernel>
108template<
typename Executor,
typename Kernel,
typename T>
110 [[maybe_unused]]
const Executor& exec,
111 std::pair<localIdx, localIdx> range,
116 auto [start, end] = range;
117 if constexpr (std::is_same<std::remove_reference_t<Executor>,
SerialExecutor>::value)
119 for (
localIdx i = start; i < end; i++)
121 if constexpr (Kokkos::is_reducer<T>::value)
123 kernel(i, value.reference());
133 using runOn =
typename Executor::exec;
134 Kokkos::parallel_reduce(
135 "parallelReduce", Kokkos::RangePolicy<runOn>(start, end), kernel, value
140template<
typename Kernel,
typename T>
142 const NeoN::Executor& exec, std::pair<localIdx, localIdx> range, Kernel kernel, T& value
145 std::visit([&](
const auto& e) {
parallelReduce(e, range, kernel, value); }, exec);
149template<
typename Executor,
typename ValueType,
typename Kernel,
typename T>
154 if constexpr (std::is_same<std::remove_reference_t<Executor>,
SerialExecutor>::value)
157 for (
localIdx i = 0; i < fieldSize; i++)
159 if constexpr (Kokkos::is_reducer<T>::value)
161 kernel(i, value.reference());
171 using runOn =
typename Executor::exec;
172 Kokkos::parallel_reduce(
173 "parallelReduce", Kokkos::RangePolicy<runOn>(0, field.
size()), kernel, value
178template<
typename ValueType,
typename Kernel,
typename T>
181 std::visit([&](
const auto& e) {
parallelReduce(e, field, kernel, value); }, field.
exec());
184template<
typename Executor,
typename Kernel>
186 [[maybe_unused]]
const Executor& exec, std::pair<localIdx, localIdx> range, Kernel kernel
189 auto [start, end] = range;
190 using runOn =
typename Executor::exec;
191 Kokkos::parallel_scan(
"parallelScan", Kokkos::RangePolicy<runOn>(start, end), kernel);
194template<
typename Kernel>
197 std::visit([&](
const auto& e) {
parallelScan(e, range, kernel); }, exec);
200template<
typename Executor,
typename Kernel,
typename ReturnType>
202 [[maybe_unused]]
const Executor& exec,
203 std::pair<localIdx, localIdx> range,
205 ReturnType& returnValue
208 auto [start, end] = range;
209 using runOn =
typename Executor::exec;
210 Kokkos::parallel_scan(
211 "parallelScan", Kokkos::RangePolicy<runOn>(start, end), kernel, returnValue
215template<
typename Kernel,
typename ReturnType>
218 std::pair<localIdx, localIdx> range,
220 ReturnType& returnValue
223 std::visit([&](
const auto& e) {
parallelScan(e, range, kernel, returnValue); }, exec);
Reference executor for serial CPU execution.
A class to contain the data and executors for a field and define some basic operations.
localIdx size() const
Gets the size of the field.
const Executor & exec() const
Gets the executor associated with the field.
View< ValueType > view() &&=delete
void parallelFor(const Executor &exec, std::pair< localIdx, localIdx > range, Kernel kernel, std::string name="parallelFor")
std::variant< SerialExecutor, CPUExecutor, GPUExecutor > Executor
void parallelReduce(const Executor &exec, std::pair< localIdx, localIdx > range, Kernel kernel, T &value)
const std::string & name(const NeoN::Document &doc)
Retrieves the name of a Document.
void parallelScan(const Executor &exec, std::pair< localIdx, localIdx > range, Kernel kernel)