NeoFOAM
WIP Prototype of a modern OpenFOAM core
Loading...
Searching...
No Matches
sum.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: MIT
2// SPDX-FileCopyrightText: 2024 NeoFOAM authors
3#pragma once
4
5#include <Kokkos_Core.hpp>
7
8namespace NeoFOAM
9{
10
12{
13 template<typename T>
14 void operator()([[maybe_unused]] const GPUExecutor& exec, const Field<T>& field, T& sum) const
15 {
16 using executor = typename GPUExecutor::exec;
17 auto field_f = field.span();
18 auto end = field.size();
19 Kokkos::parallel_reduce(
20 "sum",
21 Kokkos::RangePolicy<executor>(0, end),
22 KOKKOS_LAMBDA(const size_t i, T& lsum) { lsum += field_f[i]; },
23 sum
24 );
25 }
26
27 template<typename T>
28 void operator()([[maybe_unused]] const CPUExecutor& exec, const Field<T>& field, T& sum)
29 {
30 using executor = typename CPUExecutor::exec;
31 auto field_f = field.span();
32 auto end = field.size();
33 Kokkos::parallel_reduce(
34 "sum",
35 Kokkos::RangePolicy<executor>(0, end),
36 KOKKOS_LAMBDA(const size_t i, T& lsum) { lsum += field_f[i]; },
37 sum
38 );
39 }
40
41 template<typename T>
42 void operator()([[maybe_unused]] const SerialExecutor& exec, const Field<T>& field, T& sum)
43 {
44 using executor = typename SerialExecutor::exec;
45 auto field_f = field.span();
46 auto end = field.size();
47 Kokkos::parallel_reduce(
48 "sum",
49 Kokkos::RangePolicy<executor>(0, end),
50 KOKKOS_LAMBDA(const size_t i, T& lsum) { lsum += field_f[i]; },
51 sum
52 );
53 }
54};
55
56
57template<typename T>
58T sum(const Field<T>& field)
59{
60 T sumValue {};
61 SumKernel kernel {};
62 std::visit([&](const auto& exec) { kernel(exec, field, sumValue); }, field.exec());
63 return sumValue;
64};
65
66template<>
68{
69 scalar sumValue = 0;
70 SumKernel kernel {};
71 std::visit([&](const auto& exec) { kernel(exec, field, sumValue); }, field.exec());
72 return sumValue;
73};
74
75
76} // namespace NeoFOAM
Executor for handling multicore CPU based parallelization.
Kokkos::DefaultHostExecutionSpace exec
A class to contain the data and executors for a field and define some basic operations.
Definition field.hpp:49
size_t size() const
Gets the size of the field.
Definition field.hpp:355
const Executor & exec() const
Gets the executor associated with the field.
Definition field.hpp:349
std::span< ValueType > span() &&=delete
Executor for GPU offloading.
Kokkos::DefaultExecutionSpace exec
Reference executor for serial CPU execution.
float scalar
Definition scalar.hpp:11
T sum(const Field< T > &field)
Definition sum.hpp:58
void operator()(const CPUExecutor &exec, const Field< T > &field, T &sum)
Definition sum.hpp:28
void operator()(const SerialExecutor &exec, const Field< T > &field, T &sum)
Definition sum.hpp:42
void operator()(const GPUExecutor &exec, const Field< T > &field, T &sum) const
Definition sum.hpp:14