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