NeoN
A framework for CFD software
Loading...
Searching...
No Matches
executor.hpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2023 - 2026 NeoN authors
2//
3// SPDX-License-Identifier: MIT
4
5#pragma once
6
7#include <string>
8#include <variant>
9
14#include "NeoN/core/error.hpp"
15#include "NeoN/core/logging.hpp"
17
18namespace NeoN
19{
20
21using Executor = std::variant<SerialExecutor, CPUExecutor, GPUExecutor>;
22
23/* @brief calls Kokkos::fence to wait for GPU kernels to be finished */
24inline void fence(const Executor& exec)
25{
26 if (std::holds_alternative<NeoN::GPUExecutor>(exec))
27 {
28 Kokkos::fence();
29 }
30}
31
32
33/* @brief creates highest available executor */
35 std::unique_ptr<AllocatorStrategy> strategy = std::make_unique<DefaultAllocator>()
36)
37{
38#if defined(KOKKOS_ENABLE_CUDA)
39 return GPUExecutor {std::move(strategy)};
40#elif defined(KOKKOS_ENABLE_HIP)
41 return GPUExecutor {std::move(strategy)};
42#elif defined(KOKKOS_ENABLE_SYCL)
43 return GPUExecutor {std::move(strategy)};
44#endif
45
46#if defined(KOKKOS_ENABLE_OPENMP)
47 return CPUExecutor {std::move(strategy)};
48#elif defined(KOKKOS_ENABLE_THREADS)
49 return CPUExecutor {std::move(strategy)};
50#endif
51 return SerialExecutor {std::move(strategy)};
52}
53
54inline std::string executorName(const Executor& exec)
55{
56 return std::visit(
57 []<typename Exec>(const Exec& concExec) { return concExec.name(); }
58
59 ,
60 exec
61 );
62}
63
64inline MemorySpace memorySpace(const Executor& exec)
65{
66 return std::visit(
67 []<typename Exec>(const Exec& concExec) { return concExec.memorySpace(); }, exec
68 );
69}
70
71/*@brief convenience function to get access to associated logger */
72inline std::shared_ptr<const Logging::BaseLogger> getLogger(const Executor& exec)
73{
74 return std::visit([](auto e) { return e.getLogger(); }, exec);
75}
76
77/*@brief convenience function to get access to associated logger */
78inline void setLogger(Executor& exec, std::shared_ptr<Logging::BaseLogger> logger)
79{
80 std::visit([logger](auto& e) { e.setLogger(logger); }, exec);
81}
82
83
90[[nodiscard]] inline bool operator==(const Executor& lhs, const Executor& rhs)
91{
92 return std::visit(
93 []<typename ExecLhs,
94 typename ExecRhs>([[maybe_unused]] const ExecLhs&, [[maybe_unused]] const ExecRhs&)
95 {
96 if constexpr (std::is_same_v<ExecLhs, ExecRhs>)
97 {
98 return typename ExecLhs::exec() == typename ExecRhs::exec();
99 }
100 else
101 {
102 return false;
103 }
104 },
105 lhs,
106 rhs
107 );
108};
109
117[[nodiscard]] inline bool operator!=(const Executor& lhs, const Executor& rhs)
118{
119 return !(lhs == rhs);
120};
121
122} // namespace NeoN
Executor for handling multicore CPU based parallelization.
Executor for GPU offloading.
Reference executor for serial CPU execution.
Integer types used throughout NeoN.
Definition array.hpp:18
MemorySpace memorySpace(const Executor &exec)
Definition executor.hpp:64
std::variant< SerialExecutor, CPUExecutor, GPUExecutor > Executor
Definition executor.hpp:21
void setLogger(Executor &exec, std::shared_ptr< Logging::BaseLogger > logger)
Definition executor.hpp:78
void fence(const Executor &exec)
Definition executor.hpp:24
std::shared_ptr< const Logging::BaseLogger > getLogger(const Executor &exec)
Definition executor.hpp:72
std::string executorName(const Executor &exec)
Definition executor.hpp:54
Executor createDefaultExecutor(std::unique_ptr< AllocatorStrategy > strategy=std::make_unique< DefaultAllocator >())
Definition executor.hpp:34
MemorySpace
Definition allocator.hpp:18
bool operator==(const Executor &lhs, const Executor &rhs)
Checks if two executors are equal, i.e. they are of the same type.
Definition executor.hpp:90
bool operator!=(const Executor &lhs, const Executor &rhs)
Checks if two executors are not equal, i.e. they are not of the same type.
Definition executor.hpp:117