Block-Structured AMR Software Framework
Loading...
Searching...
No Matches
AMReX_GpuControl.H
Go to the documentation of this file.
1#ifndef AMREX_GPU_CONTROL_H_
2#define AMREX_GPU_CONTROL_H_
3#include <AMReX_Config.H>
4
6#include <AMReX_GpuTypes.H>
7
8#include <utility>
9
10#if defined(AMREX_USE_HIP) || defined(AMREX_USE_CUDA)
11#define AMREX_GPU_STREAM_ALLOC_SUPPORT 1
12#endif
13
14#if defined(AMREX_USE_HIP)
15#define AMREX_HIP_OR_CUDA(a,b) a
16#elif defined(AMREX_USE_CUDA)
17#define AMREX_HIP_OR_CUDA(a,b) b
18#else
19#define AMREX_HIP_OR_CUDA(a,b) ((void)0);
20#endif
21
22#if defined(AMREX_USE_HIP)
23#define AMREX_HIP_OR_CUDA_OR_SYCL(a,b,c) a
24#elif defined(AMREX_USE_CUDA)
25#define AMREX_HIP_OR_CUDA_OR_SYCL(a,b,c) b
26#elif defined(AMREX_USE_SYCL)
27#define AMREX_HIP_OR_CUDA_OR_SYCL(a,b,c) c
28#else
29#define AMREX_HIP_OR_CUDA_OR_SYCL(a,b,c) ((void)0);
30#endif
31
32#ifdef AMREX_USE_GPU
33#define AMREX_GPU_OR_CPU(a,b) a
34#else
35#define AMREX_GPU_OR_CPU(a,b) b
36#endif
37
38#ifdef AMREX_USE_SYCL
39#define AMREX_SYCL_ONLY(a) a
40#else
41#define AMREX_SYCL_ONLY(a) ((void)0)
42#endif
43
44#ifdef AMREX_USE_SYCL
45#if (AMREX_SPACEDIM == 1)
46# define AMREX_SYCL_1D_ONLY(a) a
47# define AMREX_SYCL_2D_ONLY(a) ((void)0)
48# define AMREX_SYCL_3D_ONLY(a) ((void)0)
49#elif (AMREX_SPACEDIM == 2)
50# define AMREX_SYCL_1D_ONLY(a) ((void)0)
51# define AMREX_SYCL_2D_ONLY(a) a
52# define AMREX_SYCL_3D_ONLY(a) ((void)0)
53#elif (AMREX_SPACEDIM == 3)
54# define AMREX_SYCL_1D_ONLY(a) ((void)0)
55# define AMREX_SYCL_2D_ONLY(a) ((void)0)
56# define AMREX_SYCL_3D_ONLY(a) a
57#endif
58#else
59# define AMREX_SYCL_1D_ONLY(a) ((void)0)
60# define AMREX_SYCL_2D_ONLY(a) ((void)0)
61# define AMREX_SYCL_3D_ONLY(a) ((void)0)
62#endif
63
64namespace amrex {
65 enum struct RunOn { Gpu, Cpu, Device=Gpu, Host=Cpu }; // NOLINT(readability-enum-initial-value)
66}
67
68#ifdef AMREX_USE_GPU
69#define AMREX_DEFAULT_RUNON // need explicit RunOn when compiling for Gpu
70#else
71#define AMREX_DEFAULT_RUNON =amrex::RunOn::Host // by default run on Host when compiling for Cpu
72#endif
73
74namespace amrex { // NOLINT(modernize-concat-nested-namespaces)
75
76#ifdef AMREX_USE_HIP
77using gpuStream_t = hipStream_t;
78#elif defined(AMREX_USE_CUDA)
79using gpuStream_t = cudaStream_t;
80#endif
81
82namespace Gpu {
83
84#if defined(AMREX_USE_GPU)
85
86 extern bool in_launch_region;
87
88 [[nodiscard]] inline bool inLaunchRegion () noexcept { return in_launch_region; }
89 [[nodiscard]] inline bool notInLaunchRegion () noexcept { return !in_launch_region; }
90
110 inline bool setLaunchRegion (bool launch) noexcept {
111 bool r = in_launch_region;
113 return r;
114 }
115
116 extern bool in_graph_region;
117 [[nodiscard]] inline bool inGraphRegion() { return (in_graph_region && in_launch_region); }
118 [[nodiscard]] inline bool notInGraphRegion() { return (!in_graph_region || !in_launch_region); }
119
120 inline bool setGraphRegion (bool graph) {
121 bool r = in_graph_region;
122 in_graph_region = graph;
123 return r;
124 }
125
126 struct [[nodiscard]] LaunchSafeGuard
127 {
128 explicit LaunchSafeGuard (bool flag) noexcept
129 : m_old(setLaunchRegion(flag)) {}
131 private:
132 bool m_old;
133 };
134
135 struct [[nodiscard]] GraphSafeGuard
136 {
137 explicit GraphSafeGuard (bool flag) noexcept
138 : m_old(setGraphRegion(flag)) {}
140 private:
141 bool m_old;
142 };
143
144 extern bool in_single_stream_region;
145 extern bool in_nosync_region;
146
147 [[nodiscard]] inline bool inSingleStreamRegion () noexcept { return in_single_stream_region; }
148 [[nodiscard]] inline bool inNoSyncRegion () noexcept { return in_nosync_region; }
149
150 inline bool setSingleStreamRegion (bool b) noexcept {
151 return std::exchange(in_single_stream_region, b);
152 }
153
154 inline bool setNoSyncRegion (bool b) noexcept {
155 return std::exchange(in_nosync_region, b);
156 }
157
162 struct [[nodiscard]] SingleStreamRegion
163 {
165 : m_prev_flag(std::exchange(in_single_stream_region,true))
166 {}
167
169
170 private:
171 bool m_prev_flag;
172 };
173
179 struct [[nodiscard]] NoSyncRegion
180 {
181 NoSyncRegion () noexcept
182 : m_prev_flag(std::exchange(in_nosync_region,true))
183 {}
184
185 ~NoSyncRegion () { in_nosync_region = m_prev_flag; }
186
187 [[nodiscard]] bool inNoSyncRegionPreviously () const { return m_prev_flag; }
188
189 protected:
191 };
192
193#else
194
195 [[nodiscard]] constexpr bool inLaunchRegion () { return false; }
196 [[nodiscard]] constexpr bool notInLaunchRegion () { return true; }
197 [[nodiscard]] constexpr bool setLaunchRegion (bool) { return false; }
198
199 [[nodiscard]] constexpr bool inGraphRegion () { return false; }
200 [[nodiscard]] constexpr bool notInGraphRegion () { return true; }
201 [[nodiscard]] constexpr bool setGraphRegion (bool) { return false; }
202
203 struct [[nodiscard]] LaunchSafeGuard
204 {
205 explicit LaunchSafeGuard (bool) {}
206 };
207
208 struct [[nodiscard]] GraphSafeGuard
209 {
210 explicit GraphSafeGuard (bool) {}
211 };
212
213 [[nodiscard]] constexpr bool inSingleStreamRegion () { return false; }
214 [[nodiscard]] constexpr bool inNoSyncRegion () { return true; }
215 [[nodiscard]] constexpr bool setSingleStreamRegion (bool) { return false; }
216 [[nodiscard]] constexpr bool setNoSyncRegion (bool) { return true; }
217 struct [[nodiscard]] SingleStreamRegion {};
218 struct [[nodiscard]] NoSyncRegion {
219 static constexpr bool inNoSyncRegionPreviously () { return true; }
220 };
221
222#endif
223
224}
225}
226
227#endif
bool setGraphRegion(bool graph)
Definition AMReX_GpuControl.H:120
bool inGraphRegion()
Definition AMReX_GpuControl.H:117
bool setNoSyncRegion(bool b) noexcept
Definition AMReX_GpuControl.H:154
bool in_graph_region
Definition AMReX_GpuControl.cpp:8
bool in_launch_region
Definition AMReX_GpuControl.cpp:7
bool notInGraphRegion()
Definition AMReX_GpuControl.H:118
bool in_single_stream_region
Definition AMReX_GpuControl.cpp:9
bool inLaunchRegion() noexcept
Definition AMReX_GpuControl.H:88
bool inSingleStreamRegion() noexcept
Definition AMReX_GpuControl.H:147
bool in_nosync_region
Definition AMReX_GpuControl.cpp:10
bool notInLaunchRegion() noexcept
Definition AMReX_GpuControl.H:89
bool setLaunchRegion(bool launch) noexcept
Definition AMReX_GpuControl.H:110
bool inNoSyncRegion() noexcept
Definition AMReX_GpuControl.H:148
bool setSingleStreamRegion(bool b) noexcept
Definition AMReX_GpuControl.H:150
Definition AMReX_Amr.cpp:50
RunOn
Definition AMReX_GpuControl.H:65
cudaStream_t gpuStream_t
Definition AMReX_GpuControl.H:79
void launch(T const &n, L &&f) noexcept
Definition AMReX_GpuLaunchFunctsC.H:122
Definition AMReX_GpuControl.H:136
~GraphSafeGuard()
Definition AMReX_GpuControl.H:139
GraphSafeGuard(bool flag) noexcept
Definition AMReX_GpuControl.H:137
Definition AMReX_GpuControl.H:127
LaunchSafeGuard(bool flag) noexcept
Definition AMReX_GpuControl.H:128
~LaunchSafeGuard()
Definition AMReX_GpuControl.H:130
Definition AMReX_GpuControl.H:180
NoSyncRegion() noexcept
Definition AMReX_GpuControl.H:181
bool inNoSyncRegionPreviously() const
Definition AMReX_GpuControl.H:187
bool m_prev_flag
Definition AMReX_GpuControl.H:190
~NoSyncRegion()
Definition AMReX_GpuControl.H:185
Definition AMReX_GpuControl.H:163
SingleStreamRegion() noexcept
Definition AMReX_GpuControl.H:164
~SingleStreamRegion()
Definition AMReX_GpuControl.H:168