5#include <Kokkos_Core.hpp>
14template<
typename ValueType>
19template<
typename Kernel>
23 } -> std::same_as<void>;
26template<
typename Executor, parallelForKernel Kernel>
28 [[maybe_unused]]
const Executor& exec,
29 std::pair<size_t, size_t> range,
31 std::string
name =
"parallelFor"
34 auto [start, end] = range;
35 if constexpr (std::is_same<std::remove_reference_t<Executor>,
SerialExecutor>::value)
37 for (
size_t i = start; i < end; i++)
44 using runOn =
typename Executor::exec;
47 Kokkos::RangePolicy<runOn>(start, end),
48 KOKKOS_LAMBDA(
const size_t i) { kernel(i); }
54template<parallelForKernel Kernel>
57 std::pair<size_t, size_t> range,
59 std::string
name =
"parallelFor"
62 std::visit([&](
const auto& e) {
parallelFor(e, range, kernel,
name); }, exec);
66template<
typename Kernel,
typename ValueType>
70 } -> std::same_as<ValueType>;
73template<
typename Executor,
typename ValueType, parallelForFieldKernel<ValueType> Kernel>
75 [[maybe_unused]]
const Executor& exec,
78 std::string
name =
"parallelFor"
81 auto span = field.
span();
82 if constexpr (std::is_same<std::remove_reference_t<Executor>,
SerialExecutor>::value)
84 size_t fieldSize = field.
size();
85 for (
size_t i = 0; i < fieldSize; i++)
92 using runOn =
typename Executor::exec;
95 Kokkos::RangePolicy<runOn>(0, field.
size()),
96 KOKKOS_LAMBDA(
const size_t i) { span[i] = kernel(i); }
101template<
typename ValueType, parallelForFieldKernel<ValueType> Kernel>
107template<
typename Executor,
typename Kernel,
typename T>
109 [[maybe_unused]]
const Executor& exec, std::pair<size_t, size_t> range, Kernel kernel, T& value
112 auto [start, end] = range;
113 if constexpr (std::is_same<std::remove_reference_t<Executor>,
SerialExecutor>::value)
115 for (
size_t i = start; i < end; i++)
117 if constexpr (Kokkos::is_reducer<T>::value)
119 kernel(i, value.reference());
129 using runOn =
typename Executor::exec;
130 Kokkos::parallel_reduce(
131 "parallelReduce", Kokkos::RangePolicy<runOn>(start, end), kernel, value
136template<
typename Kernel,
typename T>
138 const NeoFOAM::Executor& exec, std::pair<size_t, size_t> range, Kernel kernel, T& value
141 return std::visit([&](
const auto& e) {
return parallelReduce(e, range, kernel, value); }, exec);
145template<
typename Executor,
typename ValueType,
typename Kernel,
typename T>
150 if constexpr (std::is_same<std::remove_reference_t<Executor>,
SerialExecutor>::value)
152 size_t fieldSize = field.
size();
153 for (
size_t i = 0; i < fieldSize; i++)
155 if constexpr (Kokkos::is_reducer<T>::value)
157 kernel(i, value.reference());
167 using runOn =
typename Executor::exec;
168 Kokkos::parallel_reduce(
169 "parallelReduce", Kokkos::RangePolicy<runOn>(0, field.
size()), kernel, value
174template<
typename ValueType,
typename Kernel,
typename T>
182template<
typename Executor,
typename Kernel>
184 [[maybe_unused]]
const Executor& exec, std::pair<size_t, size_t> range, Kernel kernel
187 auto [start, end] = range;
188 using runOn =
typename Executor::exec;
189 Kokkos::parallel_scan(
"parallelScan", Kokkos::RangePolicy<runOn>(start, end), kernel);
192template<
typename Kernel>
195 std::visit([&](
const auto& e) {
parallelScan(e, range, kernel); }, exec);
198template<
typename Executor,
typename Kernel,
typename ReturnType>
200 [[maybe_unused]]
const Executor& exec,
201 std::pair<size_t, size_t> range,
203 ReturnType& returnValue
206 auto [start, end] = range;
207 using runOn =
typename Executor::exec;
208 Kokkos::parallel_scan(
209 "parallelScan", Kokkos::RangePolicy<runOn>(start, end), kernel, returnValue
213template<
typename Kernel,
typename ReturnType>
216 std::pair<size_t, size_t> range,
218 ReturnType& returnValue
222 [&](
const auto& e) {
return parallelScan(e, range, kernel, returnValue); }, exec
A class to contain the data and executors for a field and define some basic operations.
size_t size() const
Gets the size of the field.
const Executor & exec() const
Gets the executor associated with the field.
std::span< ValueType > span() &&=delete
Reference executor for serial CPU execution.
const std::string & name(const NeoFOAM::Document &doc)
Retrieves the name of a Document.
void parallelReduce(const Executor &exec, std::pair< size_t, size_t > range, Kernel kernel, T &value)
std::variant< SerialExecutor, CPUExecutor, GPUExecutor > Executor
void parallelScan(const Executor &exec, std::pair< size_t, size_t > range, Kernel kernel)
void parallelFor(const Executor &exec, std::pair< size_t, size_t > range, Kernel kernel, std::string name="parallelFor")