1#ifndef AMREX_GPU_LAUNCH_H_
2#define AMREX_GPU_LAUNCH_H_
3#include <AMReX_Config.H>
32#define AMREX_GPU_NCELLS_PER_THREAD 3
33#define AMREX_GPU_Y_STRIDE 1
34#define AMREX_GPU_Z_STRIDE 1
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__)
55#if defined(AMREX_USE_CUDA) || defined(AMREX_USE_HIP)
56 template<
class L,
class... Lambdas>
63 template<
class L,
class... Lambdas>
66 call_device(std::forward<Lambdas>(fs)...);
76 template<
class L,
class... Lambdas>
78 std::forward<L>(f0)();
83 template <
class T>
class LayoutData;
90 return AMREX_GPU_MAX_THREADS;
101 const auto len = bx.length3d();
103 Long j = (
offset - k*(len[0]*len[1])) / len[0];
104 Long i = (
offset - k*(len[0]*len[1])) - j*len[0];
107 static_cast<int>(k))};
109 return (bx &
Box(iv,iv,bx.type()));
155 Long numBlocks = (std::max(N,
Long(1)) + MT - 1) / MT;
157 numBlocks = std::min(numBlocks,
Long(std::numeric_limits<unsigned int>::max()/MT));
159 numBlocks = std::min(numBlocks,
Long(std::numeric_limits<int>::max()));
160 ec.numBlocks.x = numBlocks;
161 ec.numThreads.x = MT;
170 return makeExecutionConfig<MT>(box.numPts());
183 int numblocks_max = std::numeric_limits<int>::max();
185 Long nmax =
Long(MT) * numblocks_max;
188 auto nlaunches =
int((N+nmax-1)/nmax);
191 for (
int i = 0; i < nlaunches; ++i) {
194 nblocks = numblocks_max;
197 nblocks =
int((N+MT-1)/MT);
200 r[i].start_idx = ndone;
201 ndone +=
Long(nblocks) * MT;
203 r[i].nblocks = nblocks;
208 template <
int MT,
int dim>
211 return makeNExecutionConfigs<MT>(box.numPts());
220namespace amrex::detail {
222 template <
typename... L>
constexpr bool is_big_kernel () {
223 return (
sizeof(L) + ... + 0) > 1792;
226 template <
typename... L>
227 struct SyclKernelDevPtr
229 using Ls = GpuTuple<L...>;
234 SyclKernelDevPtr (L
const&... f, gpuStream_t
const& stream)
237 if constexpr (is_big_kernel<L...>()) {
238 std::size_t sz =
sizeof(Ls);
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);
254 if constexpr (is_big_kernel<Ls>()) {
256 m_stream.queue->wait_and_throw();
257 }
catch (sycl::exception
const& ex) {
267 amrex::Abort(std::string(
"~SyclKernelDevPtr: ")+ex.what());
284 using pointer_t = std::add_pointer_t<
285 std::add_const_t<typename std::tuple_element<N, Ls>::type>>;
287 std::ptrdiff_t
offset = (
char*)(&(amrex::get<N>(*m_hp))) - (
char*)m_hp;
288 return pointer_t((
char const*)m_dp +
offset);
290 return pointer_t(
nullptr);
316#if defined(AMREX_USE_GPU) || !defined(AMREX_USE_OMP)
326template <std::
integral T,
typename L >
354template <std::
integral T,
typename L >
362template <std::
integral T,
typename L >
366#pragma omp parallel for
367 for (T i = 0; i < n; ++i) {
378#if (AMREX_SPACEDIM == 1)
379#pragma omp parallel for
380 for (
int i = lo.x; i <= hi.x; ++i) {
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) {
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) {
404template <std::
integral T,
typename L >
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) {
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) {
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) {
#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