Block-Structured AMR Software Framework
Loading...
Searching...
No Matches
AMReX_GpuLaunch.H
Go to the documentation of this file.
1#ifndef AMREX_GPU_LAUNCH_H_
2#define AMREX_GPU_LAUNCH_H_
3#include <AMReX_Config.H>
4
7#include <AMReX_GpuControl.H>
8#include <AMReX_GpuTypes.H>
9#include <AMReX_GpuError.H>
10#include <AMReX_GpuRange.H>
11#include <AMReX_GpuDevice.H>
12#include <AMReX_GpuMemory.H>
13#include <AMReX_GpuReduce.H>
14#include <AMReX_Arena.H>
15#include <AMReX_Tuple.H>
16#include <AMReX_Box.H>
17#include <AMReX_Loop.H>
18#include <AMReX_Extension.H>
19#include <AMReX_BLassert.H>
20#include <AMReX_TypeTraits.H>
22#include <AMReX_RandomEngine.H>
23#include <AMReX_Algorithm.H>
24#include <AMReX_Math.H>
25#include <AMReX_Vector.H>
26#include <concepts>
27#include <cstddef>
28#include <limits>
29#include <algorithm>
30#include <utility>
31
32#define AMREX_GPU_NCELLS_PER_THREAD 3
33#define AMREX_GPU_Y_STRIDE 1
34#define AMREX_GPU_Z_STRIDE 1
35
36#ifdef AMREX_USE_CUDA
37# define AMREX_LAUNCH_KERNEL(MT, blocks, threads, sharedMem, stream, ... ) \
38 amrex::launch_global<MT><<<blocks, threads, sharedMem, stream>>>(__VA_ARGS__)
39# define AMREX_LAUNCH_KERNEL_NOBOUND(blocks, threads, sharedMem, stream, ... ) \
40 amrex::launch_global <<<blocks, threads, sharedMem, stream>>>(__VA_ARGS__)
41#elif defined(AMREX_USE_HIP)
42# define AMREX_LAUNCH_KERNEL(MT, blocks, threads, sharedMem, stream, ... ) \
43 hipLaunchKernelGGL(amrex::launch_global<MT>, blocks, threads, sharedMem, stream, __VA_ARGS__)
44# define AMREX_LAUNCH_KERNEL_NOBOUND(blocks, threads, sharedMem, stream, ... ) \
45 hipLaunchKernelGGL(amrex::launch_global , blocks, threads, sharedMem, stream, __VA_ARGS__)
46#endif
47
48
49namespace amrex {
50
51// We cannot take rvalue lambdas.
52// ************************************************
53// Variadic lambda function wrappers for C++ CUDA/HIP Kernel calls.
54
55#if defined(AMREX_USE_CUDA) || defined(AMREX_USE_HIP)
56 template<class L, class... Lambdas>
57 AMREX_GPU_GLOBAL void launch_global (L f0, Lambdas... fs) { f0(); call_device(fs...); }
58
60 template<class L>
61 AMREX_GPU_DEVICE void call_device (L&& f0) noexcept { f0(); }
62
63 template<class L, class... Lambdas>
64 AMREX_GPU_DEVICE void call_device (L&& f0, Lambdas&&... fs) noexcept {
65 f0();
66 call_device(std::forward<Lambdas>(fs)...);
67 }
69#endif
70
71// CPU variation
72
73 template<class L>
74 void launch_host (L&& f0) noexcept { std::forward<L>(f0)(); }
75
76 template<class L, class... Lambdas>
77 void launch_host (L&& f0, Lambdas&&... fs) noexcept {
78 std::forward<L>(f0)();
79 launch_host(std::forward<Lambdas>(fs)...);
80 }
81
82
83 template <class T> class LayoutData;
84 class FabArrayBase;
85
86namespace Gpu {
87
88#ifdef AMREX_USE_GPU
89 constexpr std::size_t numThreadsPerBlockParallelFor () {
90 return AMREX_GPU_MAX_THREADS;
91 }
92#else
93 constexpr std::size_t numThreadsPerBlockParallelFor () { return 0; }
94#endif
95
97 inline
98 Box getThreadBox (const Box& bx, Long offset) noexcept
99 {
101 const auto len = bx.length3d();
102 Long k = offset / (len[0]*len[1]);
103 Long j = (offset - k*(len[0]*len[1])) / len[0];
104 Long i = (offset - k*(len[0]*len[1])) - j*len[0];
105 IntVect iv{AMREX_D_DECL(static_cast<int>(i),
106 static_cast<int>(j),
107 static_cast<int>(k))};
108 iv += bx.smallEnd();
109 return (bx & Box(iv,iv,bx.type()));
110 ))
113 return bx;
114 ))
115 }
116
117// ************************************************
118
119#ifdef AMREX_USE_GPU
124 ExecutionConfig (const Box& box) noexcept {
125 // If we change this, we must make sure it doesn't break FabArrayUtility Reduce*,
126 // which assumes the decomposition is 1D.
128#if 0
130 b -= box.smallEnd();
133#endif
134 }
135 ExecutionConfig (const Box& box, int comps) noexcept {
136 const Box& b = amrex::surroundingNodes(box);
138 }
142 ExecutionConfig (dim3 nb, dim3 nt, std::size_t sm=0) noexcept
143 : numBlocks(nb), numThreads(nt), sharedMem(sm) {}
144
147 std::size_t sharedMem = 0;
148 };
149
150 template <int MT>
153 {
154 ExecutionConfig ec(dim3{}, dim3{});
155 Long numBlocks = (std::max(N,Long(1)) + MT - 1) / MT;
156 // ensure that blockDim.x*gridDim.x does not overflow
157 numBlocks = std::min(numBlocks, Long(std::numeric_limits<unsigned int>::max()/MT));
158 // ensure that the maximum grid size of 2^31-1 won't be exceeded
159 numBlocks = std::min(numBlocks, Long(std::numeric_limits<int>::max()));
160 ec.numBlocks.x = numBlocks;
161 ec.numThreads.x = MT;
163 return ec;
164 }
165
166 template <int MT>
167 ExecutionConfig
168 makeExecutionConfig (const Box& box) noexcept
169 {
170 return makeExecutionConfig<MT>(box.numPts());
171 }
172
174 {
177 };
178
179 template <int MT>
181 {
182 // Max # of blocks in a kernel launch
183 int numblocks_max = std::numeric_limits<int>::max();
184 // Max # of threads in a kernel launch
185 Long nmax = Long(MT) * numblocks_max;
186 // # of launches needed for N elements without using grid-stride
187 // loops inside GPU kernels.
188 auto nlaunches = int((N+nmax-1)/nmax);
189 Vector<ExecConfig> r(nlaunches);
190 Long ndone = 0;
191 for (int i = 0; i < nlaunches; ++i) {
192 int nblocks;
193 if (N > nmax) {
194 nblocks = numblocks_max;
195 N -= nmax;
196 } else {
197 nblocks = int((N+MT-1)/MT);
198 }
199 // At which element ID the kernel should start
200 r[i].start_idx = ndone;
201 ndone += Long(nblocks) * MT;
202 // # of blocks in this launch
203 r[i].nblocks = nblocks;
204 }
205 return r;
206 }
207
208 template <int MT, int dim>
210 {
211 return makeNExecutionConfigs<MT>(box.numPts());
212 }
213#endif
214
215}
216}
217
218#ifdef AMREX_USE_SYCL
220namespace amrex::detail {
221
222 template <typename... L> constexpr bool is_big_kernel () {
223 return (sizeof(L) + ... + 0) > 1792;
224 }
225
226 template <typename... L>
227 struct SyclKernelDevPtr
228 {
229 using Ls = GpuTuple<L...>;
230 Ls* m_dp = nullptr;
231 Ls* m_hp = nullptr;
232 gpuStream_t m_stream;
233
234 SyclKernelDevPtr (L const&... f, gpuStream_t const& stream)
235 : m_stream(stream)
236 {
237 if constexpr (is_big_kernel<L...>()) {
238 std::size_t sz = sizeof(Ls);
239 m_dp = (Ls*)The_Arena()->alloc(sz);
240 m_hp = (Ls*)The_Pinned_Arena()->alloc(sz);
241 new (m_hp) Ls(f...);
242 auto* l_hp = (void const*)m_hp;
243 auto* l_dp = (void*)m_dp;
244 stream.queue->submit([&] (sycl::handler& h) {
245 h.memcpy(l_dp, l_hp, sz);
246 });
247 } else {
249 }
250 }
251
252 ~SyclKernelDevPtr ()
253 {
254 if constexpr (is_big_kernel<Ls>()) {
255 try {
256 m_stream.queue->wait_and_throw();
257 } catch (sycl::exception const& ex) {
258 if (m_dp) {
259 The_Arena()->free((void*)m_dp);
260 m_dp = nullptr;
261 }
262 if (m_hp) {
263 m_hp->~Ls();
264 The_Pinned_Arena()->free((void*)m_hp);
265 m_hp = nullptr;
266 }
267 amrex::Abort(std::string("~SyclKernelDevPtr: ")+ex.what());
268 }
269 if (m_dp) {
270 The_Arena()->free((void*)m_dp);
271 m_dp = nullptr;
272 }
273 if (m_hp) {
274 m_hp->~Ls();
275 The_Pinned_Arena()->free((void*)m_hp);
276 m_hp = nullptr;
277 }
278 }
279 }
280
281 template <int N>
282 auto get () const
283 {
284 using pointer_t = std::add_pointer_t<
285 std::add_const_t<typename std::tuple_element<N, Ls>::type>>;
286 if (m_hp && m_dp) {
287 std::ptrdiff_t offset = (char*)(&(amrex::get<N>(*m_hp))) - (char*)m_hp;
288 return pointer_t((char const*)m_dp + offset);
289 } else {
290 return pointer_t(nullptr);
291 }
292 }
293 };
294
295}
297#endif
298
299
300#ifdef AMREX_USE_GPU
303#else
306#include <AMReX_SIMD.H>
307#endif
309
311
313
314namespace amrex {
315
316#if defined(AMREX_USE_GPU) || !defined(AMREX_USE_OMP)
317
326template <std::integral T, typename L >
327void ParallelForOMP (T n, L const& f) noexcept
328{
329 ParallelFor(n, f);
330}
331
340template <typename L>
341void ParallelForOMP (Box const& box, L const& f) noexcept
342{
343 ParallelFor(box, f);
344}
345
354template <std::integral T, typename L >
355void ParallelForOMP (Box const& box, T ncomp, L const& f) noexcept
356{
357 ParallelFor(box, ncomp, f);
358}
359
360#else /* !defined(AMREX_USE_GPU) && defined(AMREX_USE_OMP) */
361
362template <std::integral T, typename L >
364void ParallelForOMP (T n, L const& f) noexcept
365{
366#pragma omp parallel for
367 for (T i = 0; i < n; ++i) {
368 f(i);
369 }
370}
371
372template <typename L>
374void ParallelForOMP (Box const& box, L const& f) noexcept
375{
376 auto lo = amrex::lbound(box);
377 auto hi = amrex::ubound(box);
378#if (AMREX_SPACEDIM == 1)
379#pragma omp parallel for
380 for (int i = lo.x; i <= hi.x; ++i) {
381 f(i,0,0);
382 }
383#elif (AMREX_SPACEDIM == 2)
384#pragma omp parallel for
385 for (int j = lo.y; j <= hi.y; ++j) {
387 for (int i = lo.x; i <= hi.x; ++i) {
388 f(i,j,0);
389 }
390 }
391#else
392#pragma omp parallel for collapse(2)
393 for (int k = lo.z; k <= hi.z; ++k) {
394 for (int j = lo.y; j <= hi.y; ++j) {
396 for (int i = lo.x; i <= hi.x; ++i) {
397 f(i,j,k);
398 }
399 }
400 }
401#endif
402}
403
404template <std::integral T, typename L >
406void ParallelForOMP (Box const& box, T ncomp, L const& f) noexcept
407{
408 auto lo = amrex::lbound(box);
409 auto hi = amrex::ubound(box);
410#if (AMREX_SPACEDIM == 1)
411#pragma omp parallel for collapse(2)
412 for (T n = 0; n < ncomp; ++n) {
413 for (int i = lo.x; i <= hi.x; ++i) {
414 f(i,0,0,n);
415 }
416 }
417#elif (AMREX_SPACEDIM == 2)
418#pragma omp parallel for collapse(2)
419 for (T n = 0; n < ncomp; ++n) {
420 for (int j = lo.y; j <= hi.y; ++j) {
422 for (int i = lo.x; i <= hi.x; ++i) {
423 f(i,j,0,n);
424 }
425 }
426 }
427#else
428#pragma omp parallel for collapse(3)
429 for (T n = 0; n < ncomp; ++n) {
430 for (int k = lo.z; k <= hi.z; ++k) {
431 for (int j = lo.y; j <= hi.y; ++j) {
433 for (int i = lo.x; i <= hi.x; ++i) {
434 f(i,j,k,n);
435 }
436 }
437 }
438 }
439#endif
440}
441
442#endif
443
444}
445
446#endif
#define AMREX_ASSERT(EX)
Definition AMReX_BLassert.H:38
#define AMREX_PRAGMA_SIMD
Definition AMReX_Extension.H:80
#define AMREX_ATTRIBUTE_FLATTEN_FOR
Definition AMReX_Extension.H:151
#define AMREX_GPU_Z_STRIDE
Definition AMReX_GpuLaunch.H:34
#define AMREX_GPU_NCELLS_PER_THREAD
Definition AMReX_GpuLaunch.H:32
#define AMREX_GPU_Y_STRIDE
Definition AMReX_GpuLaunch.H:33
#define AMREX_IF_ON_DEVICE(CODE)
Definition AMReX_GpuQualifiers.H:56
#define AMREX_GPU_GLOBAL
Definition AMReX_GpuQualifiers.H:19
#define AMREX_IF_ON_HOST(CODE)
Definition AMReX_GpuQualifiers.H:58
#define AMREX_GPU_DEVICE
Definition AMReX_GpuQualifiers.H:18
#define AMREX_GPU_HOST_DEVICE
Definition AMReX_GpuQualifiers.H:20
Array4< int const > offset
Definition AMReX_HypreMLABecLap.cpp:1139
#define AMREX_D_DECL(a, b, c)
Definition AMReX_SPACE.H:171
virtual void free(void *pt)=0
A pure virtual function for deleting the arena pointed to by pt.
virtual void * alloc(std::size_t sz)=0
__host__ __device__ const int * hiVect() const &noexcept
Return a constant pointer the array of high end coordinates. Useful for calls to FORTRAN.
Definition AMReX_Box.H:195
__host__ __device__ const int * loVect() const &noexcept
Return a constant pointer the array of low end coordinates. Useful for calls to FORTRAN.
Definition AMReX_Box.H:190
__host__ __device__ BoxND & coarsen(int ref_ratio) noexcept
Coarsen BoxND by given (positive) refinement ratio. NOTE: if type(dir) = CELL centered: lo <- lo/rati...
Definition AMReX_Box.H:730
__host__ __device__ const IntVectND< dim > & smallEnd() const &noexcept
Return the inclusive lower bound of the box.
Definition AMReX_Box.H:112
static void c_threads_and_blocks(const int *lo, const int *hi, dim3 &numBlocks, dim3 &numThreads) noexcept
Definition AMReX_GpuDevice.cpp:1201
static void n_threads_and_blocks(const Long N, dim3 &numBlocks, dim3 &numThreads) noexcept
Definition AMReX_GpuDevice.cpp:1186
static constexpr int warp_size
Definition AMReX_GpuDevice.H:236
static void c_comps_threads_and_blocks(const int *lo, const int *hi, const int comps, dim3 &numBlocks, dim3 &numThreads) noexcept
Definition AMReX_GpuDevice.cpp:1193
static void grid_stride_threads_and_blocks(dim3 &numBlocks, dim3 &numThreads) noexcept
Definition AMReX_GpuDevice.cpp:1259
This class is a thin wrapper around std::vector. Unlike vector, Vector::operator[] provides bound che...
Definition AMReX_Vector.H:29
amrex_long Long
Definition AMReX_INT.H:30
void ParallelForOMP(T n, L const &f) noexcept
Performance-portable kernel launch function with optional OpenMP threading.
Definition AMReX_GpuLaunch.H:327
__host__ __device__ Dim3 ubound(Array4< T > const &a) noexcept
Return the inclusive upper bounds of an Array4 in Dim3 form.
Definition AMReX_Array4.H:1359
__host__ __device__ Dim3 lbound(Array4< T > const &a) noexcept
Return the inclusive lower bounds of an Array4 in Dim3 form.
Definition AMReX_Array4.H:1345
Arena * The_Pinned_Arena()
Definition AMReX_Arena.cpp:860
Arena * The_Arena()
Definition AMReX_Arena.cpp:820
__host__ __device__ Box getThreadBox(const Box &bx, Long offset) noexcept
Definition AMReX_GpuLaunch.H:98
Vector< ExecConfig > makeNExecutionConfigs(Long N) noexcept
Definition AMReX_GpuLaunch.H:180
constexpr std::size_t numThreadsPerBlockParallelFor()
Definition AMReX_GpuLaunch.H:89
ExecutionConfig makeExecutionConfig(Long N) noexcept
Definition AMReX_GpuLaunch.H:152
Definition AMReX_Amr.cpp:50
void launch_host(L &&f0) noexcept
Definition AMReX_GpuLaunch.H:74
__host__ __device__ void ignore_unused(const Ts &...)
This shuts up the compiler about unused variables.
Definition AMReX.H:139
__host__ __device__ BoxND< dim > surroundingNodes(const BoxND< dim > &b, int dir) noexcept
Return a BoxND with NODE based coordinates in direction dir that encloses BoxND b....
Definition AMReX_Box.H:1531
cudaStream_t gpuStream_t
Definition AMReX_GpuControl.H:79
void ParallelFor(TypeList< CTOs... > ctos, std::array< int, sizeof...(CTOs)> const &runtime_options, T N, F &&f)
Definition AMReX_CTOParallelForImpl.H:202
BoxND< 3 > Box
Box is an alias for amrex::BoxND instantiated with AMREX_SPACEDIM.
Definition AMReX_BaseFwd.H:30
IntVectND< 3 > IntVect
IntVect is an alias for amrex::IntVectND instantiated with AMREX_SPACEDIM.
Definition AMReX_BaseFwd.H:33
void Abort(const std::string &msg)
Print out message to cerr and exit via abort().
Definition AMReX.cpp:241
const int[]
Definition AMReX_BLProfiler.cpp:1664
__global__ void launch_global(L f0, Lambdas... fs)
Definition AMReX_GpuLaunch.H:57
__host__ __device__ constexpr int get(IntVectND< dim > const &iv) noexcept
Get I'th element of IntVectND<dim>
Definition AMReX_IntVect.H:1334
Definition AMReX_GpuLaunch.H:174
int nblocks
Definition AMReX_GpuLaunch.H:176
Long start_idx
Definition AMReX_GpuLaunch.H:175
Definition AMReX_GpuLaunch.H:120
ExecutionConfig(dim3 nb, dim3 nt, std::size_t sm=0) noexcept
Definition AMReX_GpuLaunch.H:142
ExecutionConfig(Long N) noexcept
Definition AMReX_GpuLaunch.H:139
dim3 numBlocks
Definition AMReX_GpuLaunch.H:145
dim3 numThreads
Definition AMReX_GpuLaunch.H:146
ExecutionConfig(const Box &box, int comps) noexcept
Definition AMReX_GpuLaunch.H:135
ExecutionConfig(const Box &box) noexcept
Definition AMReX_GpuLaunch.H:124
ExecutionConfig() noexcept
Definition AMReX_GpuLaunch.H:121
std::size_t sharedMem
Definition AMReX_GpuLaunch.H:147