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
13#include "NeoN/core/error.hpp"
14#include "NeoN/core/logging.hpp"
16
17namespace NeoN
18{
19
20using Executor = std::variant<SerialExecutor, CPUExecutor, GPUExecutor>;
21
22/* @brief calls Kokkos::fence to wait for GPU kernels to be finished */
23inline void fence(const Executor& exec)
24{
25 if (std::holds_alternative<NeoN::GPUExecutor>(exec))
26 {
27 Kokkos::fence();
28 }
29}
30
31
32/* @brief creates highest available executor */
34 std::unique_ptr<AllocatorStrategy> strategy = std::make_unique<DefaultAllocator>()
35)
36{
37#if defined(KOKKOS_ENABLE_CUDA)
38 return GPUExecutor {std::move(strategy)};
39#elif defined(KOKKOS_ENABLE_HIP)
40 return GPUExecutor {std::move(strategy)};
41#elif defined(KOKKOS_ENABLE_SYCL)
42 return GPUExecutor {std::move(strategy)};
43#endif
44
45#if defined(KOKKOS_ENABLE_OPENMP)
46 return CPUExecutor {std::move(strategy)};
47#elif defined(KOKKOS_ENABLE_THREADS)
48 return CPUExecutor {std::move(strategy)};
49#endif
50 return SerialExecutor {std::move(strategy)};
51}
52
53inline std::string executorName(const Executor& exec)
54{
55 return std::visit(
56 []<typename Exec>(const Exec& concExec) { return concExec.name(); }
57
58 ,
59 exec
60 );
61}
62
63inline MemorySpace memorySpace(const Executor& exec)
64{
65 return std::visit(
66 []<typename Exec>(const Exec& concExec) { return concExec.memorySpace(); }, exec
67 );
68}
69
70/*@brief convenience function to get access to associated logger */
71inline std::shared_ptr<const Logging::BaseLogger> getLogger(const Executor& exec)
72{
73 return std::visit([](auto e) { return e.getLogger(); }, exec);
74}
75
76/*@brief convenience function to get access to associated logger */
77inline void setLogger(Executor& exec, std::shared_ptr<Logging::BaseLogger> logger)
78{
79 std::visit([logger](auto& e) { e.setLogger(logger); }, exec);
80}
81
82
89[[nodiscard]] inline bool operator==(const Executor& lhs, const Executor& rhs)
90{
91 return std::visit(
92 []<typename ExecLhs,
93 typename ExecRhs>([[maybe_unused]] const ExecLhs&, [[maybe_unused]] const ExecRhs&)
94 {
95 if constexpr (std::is_same_v<ExecLhs, ExecRhs>)
96 {
97 return typename ExecLhs::exec() == typename ExecRhs::exec();
98 }
99 else
100 {
101 return false;
102 }
103 },
104 lhs,
105 rhs
106 );
107};
108
116[[nodiscard]] inline bool operator!=(const Executor& lhs, const Executor& rhs)
117{
118 return !(lhs == rhs);
119};
120
121} // 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:63
void setLogger(Executor &exec, std::shared_ptr< Logging::BaseLogger > logger)
Definition executor.hpp:77
void fence(const Executor &exec)
Definition executor.hpp:23
std::shared_ptr< const Logging::BaseLogger > getLogger(const Executor &exec)
Definition executor.hpp:71
std::string executorName(const Executor &exec)
Definition executor.hpp:53
std::variant< SerialExecutor, CPUExecutor, GPUExecutor > Executor
Definition executor.hpp:20
Executor createDefaultExecutor(std::unique_ptr< AllocatorStrategy > strategy=std::make_unique< DefaultAllocator >())
Definition executor.hpp:33
MemorySpace
Definition allocator.hpp:17
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:89
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:116