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
58
59 template <typename... Lambdas>
60 AMREX_GPU_DEVICE void call_device (Lambdas&&... fs) noexcept
61 {
62 (std::forward<Lambdas>(fs)(), ...);
63 }
64
66
67 template<class L, class... Lambdas>
68 AMREX_GPU_GLOBAL void launch_global (L f0, Lambdas... fs) { f0(); call_device(fs...); }
69
70#endif
71
72// CPU variation
73
74 template<class L>
75 void launch_host (L&& f0) noexcept { std::forward<L>(f0)(); }
76
77 template<class L, class... Lambdas>
78 void launch_host (L&& f0, Lambdas&&... fs) noexcept {
79 std::forward<L>(f0)();
80 launch_host(std::forward<Lambdas>(fs)...);
81 }
82
83
84 template <class T> class LayoutData;
85 class FabArrayBase;
86
87namespace Gpu {
88
89#ifdef AMREX_USE_GPU
90 constexpr std::size_t numThreadsPerBlockParallelFor () {
91 return AMREX_GPU_MAX_THREADS;
92 }
93#else
94 constexpr std::size_t numThreadsPerBlockParallelFor () { return 0; }
95#endif
96
98 inline
99 Box getThreadBox (const Box& bx, Long offset) noexcept
100 {
102 const auto len = bx.length3d();
103 Long k = offset / (len[0]*len[1]);
104 Long j = (offset - k*(len[0]*len[1])) / len[0];
105 Long i = (offset - k*(len[0]*len[1])) - j*len[0];
106 IntVect iv{AMREX_D_DECL(static_cast<int>(i),
107 static_cast<int>(j),
108 static_cast<int>(k))};
109 iv += bx.smallEnd();
110 return (bx & Box(iv,iv,bx.type()));
111 ))
114 return bx;
115 ))
116 }
117
118// ************************************************
119
120#ifdef AMREX_USE_GPU
125 ExecutionConfig (const Box& box) noexcept {
126 // If we change this, we must make sure it doesn't break FabArrayUtility Reduce*,
127 // which assumes the decomposition is 1D.
129#if 0
131 b -= box.smallEnd();
134#endif
135 }
136 ExecutionConfig (const Box& box, int comps) noexcept {
137 const Box& b = amrex::surroundingNodes(box);
139 }
143 ExecutionConfig (dim3 nb, dim3 nt, std::size_t sm=0) noexcept
144 : numBlocks(nb), numThreads(nt), sharedMem(sm) {}
145
148 std::size_t sharedMem = 0;
149 };
150
151 template <int MT>
154 {
155 ExecutionConfig ec(dim3{}, dim3{});
156 Long numBlocks = (std::max(N,Long(1)) + MT - 1) / MT;
157 // ensure that blockDim.x*gridDim.x does not overflow
158 numBlocks = std::min(numBlocks, Long(std::numeric_limits<unsigned int>::max()/MT));
159 // ensure that the maximum grid size of 2^31-1 won't be exceeded
160 numBlocks = std::min(numBlocks, Long(std::numeric_limits<int>::max()));
161 ec.numBlocks.x = numBlocks;
162 ec.numThreads.x = MT;
164 return ec;
165 }
166
167 template <int MT>
168 ExecutionConfig
169 makeExecutionConfig (const Box& box) noexcept
170 {
171 return makeExecutionConfig<MT>(box.numPts());
172 }
173
175 {
178 };
179
180 template <int MT>
182 {
183 // Max # of blocks in a kernel launch
184 int numblocks_max = std::numeric_limits<int>::max();
185 // Max # of threads in a kernel launch
186 Long nmax = Long(MT) * numblocks_max;
187 // # of launches needed for N elements without using grid-stride
188 // loops inside GPU kernels.
189 auto nlaunches = int((N+nmax-1)/nmax);
190 Vector<ExecConfig> r(nlaunches);
191 Long ndone = 0;
192 for (int i = 0; i < nlaunches; ++i) {
193 int nblocks;
194 if (N > nmax) {
195 nblocks = numblocks_max;
196 N -= nmax;
197 } else {
198 nblocks = int((N+MT-1)/MT);
199 }
200 // At which element ID the kernel should start
201 r[i].start_idx = ndone;
202 ndone += Long(nblocks) * MT;
203 // # of blocks in this launch
204 r[i].nblocks = nblocks;
205 }
206 return r;
207 }
208
209 template <int MT, int dim>
211 {
212 return makeNExecutionConfigs<MT>(box.numPts());
213 }
214#endif
215
216}
217}
218
219#ifdef AMREX_USE_SYCL
221namespace amrex::detail {
222
223 template <typename... L> constexpr bool is_big_kernel () {
224 return (sizeof(L) + ... + 0) > 1792;
225 }
226
227 template <typename... L>
228 struct SyclKernelDevPtr
229 {
230 using Ls = GpuTuple<L...>;
231 Ls* m_dp = nullptr;
232 Ls* m_hp = nullptr;
233 gpuStream_t m_stream;
234
235 SyclKernelDevPtr (L const&... f, gpuStream_t const& stream)
236 : m_stream(stream)
237 {
238 if constexpr (is_big_kernel<L...>()) {
239 std::size_t sz = sizeof(Ls);
240 m_dp = (Ls*)The_Arena()->alloc(sz);
241 m_hp = (Ls*)The_Pinned_Arena()->alloc(sz);
242 new (m_hp) Ls(f...);
243 auto* l_hp = (void const*)m_hp;
244 auto* l_dp = (void*)m_dp;
245 stream.queue->submit([&] (sycl::handler& h) {
246 h.memcpy(l_dp, l_hp, sz);
247 });
248 } else {
250 }
251 }
252
253 ~SyclKernelDevPtr ()
254 {
255 if constexpr (is_big_kernel<Ls>()) {
256 try {
257 m_stream.queue->wait_and_throw();
258 } catch (sycl::exception const& ex) {
259 if (m_dp) {
260 The_Arena()->free((void*)m_dp);
261 m_dp = nullptr;
262 }
263 if (m_hp) {
264 m_hp->~Ls();
265 The_Pinned_Arena()->free((void*)m_hp);
266 m_hp = nullptr;
267 }
268 amrex::Abort(std::string("~SyclKernelDevPtr: ")+ex.what());
269 }
270 if (m_dp) {
271 The_Arena()->free((void*)m_dp);
272 m_dp = nullptr;
273 }
274 if (m_hp) {
275 m_hp->~Ls();
276 The_Pinned_Arena()->free((void*)m_hp);
277 m_hp = nullptr;
278 }
279 }
280 }
281
282 template <int N>
283 auto get () const
284 {
285 using pointer_t = std::add_pointer_t<
286 std::add_const_t<typename std::tuple_element<N, Ls>::type>>;
287 if (m_hp && m_dp) {
288 std::ptrdiff_t offset = (char*)(&(amrex::get<N>(*m_hp))) - (char*)m_hp;
289 return pointer_t((char const*)m_dp + offset);
290 } else {
291 return pointer_t(nullptr);
292 }
293 }
294 };
295
296}
298#endif
299
300
301#ifdef AMREX_USE_GPU
304#else
307#include <AMReX_SIMD.H>
308#endif
310
312
314
315namespace amrex {
316
317#if defined(AMREX_USE_GPU) || !defined(AMREX_USE_OMP)
318
327template <std::integral T, typename L >
328void ParallelForOMP (T n, L const& f) noexcept
329{
330 ParallelFor(n, f);
331}
332
341template <typename L>
342void ParallelForOMP (Box const& box, L const& f) noexcept
343{
344 ParallelFor(box, f);
345}
346
355template <std::integral T, typename L >
356void ParallelForOMP (Box const& box, T ncomp, L const& f) noexcept
357{
358 ParallelFor(box, ncomp, f);
359}
360
361#else /* !defined(AMREX_USE_GPU) && defined(AMREX_USE_OMP) */
362
363template <std::integral T, typename L >
365void ParallelForOMP (T n, L const& f) noexcept
366{
367#pragma omp parallel for
368 for (T i = 0; i < n; ++i) {
369 f(i);
370 }
371}
372
373template <typename L>
375void ParallelForOMP (Box const& box, L const& f) noexcept
376{
377 auto lo = amrex::lbound(box);
378 auto hi = amrex::ubound(box);
379#if (AMREX_SPACEDIM == 1)
380#pragma omp parallel for
381 for (int i = lo.x; i <= hi.x; ++i) {
382 f(i,0,0);
383 }
384#elif (AMREX_SPACEDIM == 2)
385#pragma omp parallel for
386 for (int j = lo.y; j <= hi.y; ++j) {
388 for (int i = lo.x; i <= hi.x; ++i) {
389 f(i,j,0);
390 }
391 }
392#else
393#pragma omp parallel for collapse(2)
394 for (int k = lo.z; k <= hi.z; ++k) {
395 for (int j = lo.y; j <= hi.y; ++j) {
397 for (int i = lo.x; i <= hi.x; ++i) {
398 f(i,j,k);
399 }
400 }
401 }
402#endif
403}
404
405template <std::integral T, typename L >
407void ParallelForOMP (Box const& box, T ncomp, L const& f) noexcept
408{
409 auto lo = amrex::lbound(box);
410 auto hi = amrex::ubound(box);
411#if (AMREX_SPACEDIM == 1)
412#pragma omp parallel for collapse(2)
413 for (T n = 0; n < ncomp; ++n) {
414 for (int i = lo.x; i <= hi.x; ++i) {
415 f(i,0,0,n);
416 }
417 }
418#elif (AMREX_SPACEDIM == 2)
419#pragma omp parallel for collapse(2)
420 for (T n = 0; n < ncomp; ++n) {
421 for (int j = lo.y; j <= hi.y; ++j) {
423 for (int i = lo.x; i <= hi.x; ++i) {
424 f(i,j,0,n);
425 }
426 }
427 }
428#else
429#pragma omp parallel for collapse(3)
430 for (T n = 0; n < ncomp; ++n) {
431 for (int k = lo.z; k <= hi.z; ++k) {
432 for (int j = lo.y; j <= hi.y; ++j) {
434 for (int i = lo.x; i <= hi.x; ++i) {
435 f(i,j,k,n);
436 }
437 }
438 }
439 }
440#endif
441}
442
443#endif
444
445}
446
447#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:328
__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:99
Vector< ExecConfig > makeNExecutionConfigs(Long N) noexcept
Definition AMReX_GpuLaunch.H:181
constexpr std::size_t numThreadsPerBlockParallelFor()
Definition AMReX_GpuLaunch.H:90
ExecutionConfig makeExecutionConfig(Long N) noexcept
Definition AMReX_GpuLaunch.H:153
Definition AMReX_Amr.cpp:50
void launch_host(L &&f0) noexcept
Definition AMReX_GpuLaunch.H:75
__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:68
__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:175
int nblocks
Definition AMReX_GpuLaunch.H:177
Long start_idx
Definition AMReX_GpuLaunch.H:176
Definition AMReX_GpuLaunch.H:121
ExecutionConfig(dim3 nb, dim3 nt, std::size_t sm=0) noexcept
Definition AMReX_GpuLaunch.H:143
ExecutionConfig(Long N) noexcept
Definition AMReX_GpuLaunch.H:140
dim3 numBlocks
Definition AMReX_GpuLaunch.H:146
dim3 numThreads
Definition AMReX_GpuLaunch.H:147
ExecutionConfig(const Box &box, int comps) noexcept
Definition AMReX_GpuLaunch.H:136
ExecutionConfig(const Box &box) noexcept
Definition AMReX_GpuLaunch.H:125
ExecutionConfig() noexcept
Definition AMReX_GpuLaunch.H:122
std::size_t sharedMem
Definition AMReX_GpuLaunch.H:148