Block-Structured AMR Software Framework
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 
5 #include <AMReX_GpuQualifiers.H>
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 
68 namespace amrex {
69  enum struct RunOn { Gpu, Cpu, Device=Gpu, Host=Cpu };
70 }
71 
72 namespace amrex { // NOLINT(modernize-concat-nested-namespaces)
73 
74 #ifdef AMREX_USE_HIP
75 using gpuStream_t = hipStream_t;
76 #elif defined(AMREX_USE_CUDA)
77 using gpuStream_t = cudaStream_t;
78 #endif
79 
80 namespace Gpu {
81 
82 #if defined(AMREX_USE_GPU)
83 
84  extern bool in_launch_region;
85 
86  [[nodiscard]] inline bool inLaunchRegion () noexcept { return in_launch_region; }
87  [[nodiscard]] inline bool notInLaunchRegion () noexcept { return !in_launch_region; }
88 
108  inline bool setLaunchRegion (bool launch) noexcept {
109  bool r = in_launch_region;
111  return r;
112  }
113 
114  extern bool in_graph_region;
115  [[nodiscard]] inline bool inGraphRegion() { return (in_graph_region && in_launch_region); }
116  [[nodiscard]] inline bool notInGraphRegion() { return (!in_graph_region || !in_launch_region); }
117 
118  inline bool setGraphRegion (bool graph) {
119  bool r = in_graph_region;
120  in_graph_region = graph;
121  return r;
122  }
123 
124  struct [[nodiscard]] LaunchSafeGuard
125  {
126  explicit LaunchSafeGuard (bool flag) noexcept
127  : m_old(setLaunchRegion(flag)) {}
129  private:
130  bool m_old;
131  };
132 
133  struct [[nodiscard]] GraphSafeGuard
134  {
135  explicit GraphSafeGuard (bool flag) noexcept
136  : m_old(setGraphRegion(flag)) {}
138  private:
139  bool m_old;
140  };
141 
142  extern bool in_single_stream_region;
143  extern bool in_nosync_region;
144 
145  [[nodiscard]] inline bool inSingleStreamRegion () noexcept { return in_single_stream_region; }
146  [[nodiscard]] inline bool inNoSyncRegion () noexcept { return in_nosync_region; }
147 
148  inline bool setSingleStreamRegion (bool b) noexcept {
149  return std::exchange(in_single_stream_region, b);
150  }
151 
152  inline bool setNoSyncRegion (bool b) noexcept {
153  return std::exchange(in_nosync_region, b);
154  }
155 
160  struct [[nodiscard]] SingleStreamRegion
161  {
162  SingleStreamRegion () noexcept
163  : m_prev_flag(std::exchange(in_single_stream_region,true))
164  {}
165 
167 
168  private:
170  };
171 
177  struct [[nodiscard]] NoSyncRegion
178  {
179  NoSyncRegion () noexcept
180  : m_prev_flag(std::exchange(in_nosync_region,true))
181  {}
182 
183  ~NoSyncRegion () { in_nosync_region = m_prev_flag; }
184 
185  private:
187  };
188 
189 #else
190 
191  [[nodiscard]] inline constexpr bool inLaunchRegion () { return false; }
192  [[nodiscard]] inline constexpr bool notInLaunchRegion () { return true; }
193  [[nodiscard]] inline constexpr bool setLaunchRegion (bool) { return false; }
194 
195  [[nodiscard]] inline constexpr bool inGraphRegion () { return false; }
196  [[nodiscard]] inline constexpr bool notInGraphRegion () { return true; }
197  [[nodiscard]] inline constexpr bool setGraphRegion (bool) { return false; }
198 
199  struct [[nodiscard]] LaunchSafeGuard
200  {
201  explicit LaunchSafeGuard (bool) {}
202  };
203 
204  struct [[nodiscard]] GraphSafeGuard
205  {
206  explicit GraphSafeGuard (bool) {}
207  };
208 
209  [[nodiscard]] inline constexpr bool inSingleStreamRegion () { return false; }
210  [[nodiscard]] inline constexpr bool inNoSyncRegion () { return true; }
211  [[nodiscard]] inline constexpr bool setSingleStreamRegion (bool) { return false; }
212  [[nodiscard]] inline constexpr bool setNoSyncRegion (bool) { return true; }
213  struct [[nodiscard]] SingleStreamRegion {};
214  struct [[nodiscard]] NoSyncRegion {};
215 
216 #endif
217 
218 }
219 }
220 
221 #endif
bool setGraphRegion(bool graph)
Definition: AMReX_GpuControl.H:118
bool inGraphRegion()
Definition: AMReX_GpuControl.H:115
bool setNoSyncRegion(bool b) noexcept
Definition: AMReX_GpuControl.H:152
bool in_graph_region
Definition: AMReX_GpuControl.cpp:8
bool in_launch_region
Definition: AMReX_GpuControl.cpp:7
bool notInGraphRegion()
Definition: AMReX_GpuControl.H:116
bool in_single_stream_region
Definition: AMReX_GpuControl.cpp:9
bool inLaunchRegion() noexcept
Definition: AMReX_GpuControl.H:86
bool inSingleStreamRegion() noexcept
Definition: AMReX_GpuControl.H:145
bool in_nosync_region
Definition: AMReX_GpuControl.cpp:10
bool notInLaunchRegion() noexcept
Definition: AMReX_GpuControl.H:87
bool setLaunchRegion(bool launch) noexcept
Definition: AMReX_GpuControl.H:108
bool inNoSyncRegion() noexcept
Definition: AMReX_GpuControl.H:146
bool setSingleStreamRegion(bool b) noexcept
Definition: AMReX_GpuControl.H:148
Definition: AMReX_Amr.cpp:49
RunOn
Definition: AMReX_GpuControl.H:69
cudaStream_t gpuStream_t
Definition: AMReX_GpuControl.H:77
void launch(T const &n, L &&f) noexcept
Definition: AMReX_GpuLaunchFunctsC.H:120
Definition: AMReX_GpuControl.H:134
~GraphSafeGuard()
Definition: AMReX_GpuControl.H:137
bool m_old
Definition: AMReX_GpuControl.H:139
GraphSafeGuard(bool flag) noexcept
Definition: AMReX_GpuControl.H:135
Definition: AMReX_GpuControl.H:125
LaunchSafeGuard(bool flag) noexcept
Definition: AMReX_GpuControl.H:126
bool m_old
Definition: AMReX_GpuControl.H:130
~LaunchSafeGuard()
Definition: AMReX_GpuControl.H:128
Definition: AMReX_GpuControl.H:178
NoSyncRegion() noexcept
Definition: AMReX_GpuControl.H:179
bool m_prev_flag
Definition: AMReX_GpuControl.H:186
~NoSyncRegion()
Definition: AMReX_GpuControl.H:183
Definition: AMReX_GpuControl.H:161
SingleStreamRegion() noexcept
Definition: AMReX_GpuControl.H:162
~SingleStreamRegion()
Definition: AMReX_GpuControl.H:166
bool m_prev_flag
Definition: AMReX_GpuControl.H:169