1#ifndef AMREX_GPU_CONTAINERS_H_
2#define AMREX_GPU_CONTAINERS_H_
3#include <AMReX_Config.H>
120 template<
class InIter,
class OutIter>
123 using value_type =
typename std::iterator_traits<InIter>::value_type;
125 using out_value_type =
typename std::iterator_traits<OutIter>::value_type;
126 static_assert(std::is_same_v<value_type, out_value_type>);
127 static_assert(std::is_trivially_copyable<value_type>(),
128 "Can only copy trivially copyable types");
130 auto size = std::distance(
begin,
end);
131 if (size == 0) {
return; }
153 template<
class InIter,
class OutIter>
156 using value_type =
typename std::iterator_traits<InIter>::value_type;
158 using out_value_type =
typename std::iterator_traits<OutIter>::value_type;
159 static_assert(std::is_same_v<value_type, out_value_type>);
160 static_assert(std::is_trivially_copyable<value_type>(),
161 "Can only copy trivially copyable types");
163 auto size = std::distance(
begin,
end);
164 if (size == 0) {
return; }
186 template<
class InIter,
class OutIter>
189 using value_type =
typename std::iterator_traits<InIter>::value_type;
191 using out_value_type =
typename std::iterator_traits<OutIter>::value_type;
192 static_assert(std::is_same_v<value_type, out_value_type>);
193 static_assert(std::is_trivially_copyable<value_type>(),
194 "Can only copy trivially copyable types");
196 auto size = std::distance(
begin,
end);
197 if (size == 0) {
return; }
220 template<
class InIter,
class OutIter>
223 using value_type =
typename std::iterator_traits<InIter>::value_type;
225 using out_value_type =
typename std::iterator_traits<OutIter>::value_type;
226 static_assert(std::is_same_v<value_type, out_value_type>);
227 static_assert(std::is_trivially_copyable<value_type>(),
228 "Can only copy trivially copyable types");
230 auto size = std::distance(
begin,
end);
231 if (size == 0) {
return; }
254 template<
class InIter,
class OutIter>
257 using value_type =
typename std::iterator_traits<InIter>::value_type;
259 using out_value_type =
typename std::iterator_traits<OutIter>::value_type;
260 static_assert(std::is_same_v<value_type, out_value_type>);
261 static_assert(std::is_trivially_copyable<value_type>(),
262 "Can only copy trivially copyable types");
264 auto size = std::distance(
begin,
end);
265 if (size == 0) {
return; }
288 template<
class InIter,
class OutIter>
291 using value_type =
typename std::iterator_traits<InIter>::value_type;
293 using out_value_type =
typename std::iterator_traits<OutIter>::value_type;
294 static_assert(std::is_same_v<value_type, out_value_type>);
295 static_assert(std::is_trivially_copyable<value_type>(),
296 "Can only copy trivially copyable types");
298 auto size = std::distance(
begin,
end);
299 if (size == 0) {
return; }
312 using value_type =
typename std::iterator_traits<Iter>::value_type;
313 static_assert(std::is_trivially_copyable<value_type>(),
314 "Can only copy trivially copyable types");
316 auto size = std::distance(
begin,
end);
317 if (size == 0) {
return; }
321#if defined(AMREX_USE_CUDA) && !defined(_WIN32)
323#if defined(__CUDACC__) && (__CUDACC_VER_MAJOR__ >= 13)
324 cudaMemLocation location = {};
325 location.type = cudaMemLocationTypeDevice;
326 location.id = cudaCpuDeviceId;
328 size*
sizeof(value_type),
333 size*
sizeof(value_type),
353 using value_type =
typename std::iterator_traits<Iter>::value_type;
354 static_assert(std::is_trivially_copyable<value_type>(),
355 "Can only copy trivially copyable types");
357 auto size = std::distance(
begin,
end);
358 if (size == 0) {
return; }
362#if defined(AMREX_USE_CUDA) && !defined(_WIN32)
364#if defined(__CUDACC__) && (__CUDACC_VER_MAJOR__ >= 13)
365 cudaMemLocation location = {};
366 location.type = cudaMemLocationTypeDevice;
369 size*
sizeof(value_type),
374 size*
sizeof(value_type),
400 template <
typename IT,
typename F,
401 typename T =
typename std::iterator_traits<IT>::value_type,
402 std::enable_if_t<(
sizeof(T) <= 36*8) &&
403 std::is_trivially_copyable_v<T> &&
408 auto N =
static_cast<Long
>(std::distance(first, last));
409 if (N <= 0) {
return; }
412 for (Long i = 0; i < N; ++i) {
419 if constexpr ((
sizeof(T) <= 8)
420 || (
sizeof(T) > 36*8)
421 || ! std::is_trivially_copyable<T>()) {
427 static_assert(
sizeof(T) %
sizeof(
unsigned int) == 0);
428 using U = std::conditional_t<
sizeof(T) %
sizeof(
unsigned long long) == 0,
429 unsigned long long,
unsigned int>;
430 constexpr Long nU =
sizeof(T) /
sizeof(U);
431 auto pu =
reinterpret_cast<U*
>(p);
432 constexpr int nthreads_per_block = (
sizeof(T) <= 64) ? 256 : 128;
433 int nblocks =
static_cast<int>((N+nthreads_per_block-1)/nthreads_per_block);
434 std::size_t shared_mem_bytes = nthreads_per_block *
sizeof(T);
436 amrex::launch<nthreads_per_block>(nblocks, shared_mem_bytes,
Gpu::gpuStream(),
439 Long i = handler.globalIdx();
440 Long blockDimx = handler.blockDim();
441 Long threadIdxx = handler.threadIdx();
442 Long blockIdxx = handler.blockIdx();
443 auto const shared_U = (U*)handler.sharedMemory();
444 auto const shared_T = (T*)shared_U;
446 auto ga =
new(shared_T+threadIdxx) T;
449 handler.sharedBarrier();
450 for (Long m = threadIdxx,
451 mend = nU *
amrex::min(blockDimx, N-blockDimx*blockIdxx);
452 m < mend; m += blockDimx) {
453 pu[blockDimx*blockIdxx*nU+m] = shared_U[m];
457 amrex::launch<nthreads_per_block>(nblocks, shared_mem_bytes,
Gpu::gpuStream(),
460 Long blockDimx = blockDim.x;
461 Long threadIdxx = threadIdx.x;
462 Long blockIdxx = blockIdx.x;
463 Long i = blockDimx*blockIdxx + threadIdxx;
465 auto const shared_U = gsm.
dataPtr();
466 auto const shared_T = (T*)shared_U;
468 auto ga =
new(shared_T+threadIdxx) T;
472 for (Long m = threadIdxx,
473 mend = nU *
amrex::min(blockDimx, N-blockDimx*blockIdxx);
474 m < mend; m += blockDimx) {
475 pu[blockDimx*blockIdxx*nU+m] = shared_U[m];
#define AMREX_CUDA_SAFE_CALL(call)
Definition AMReX_GpuError.H:73
#define AMREX_GPU_DEVICE
Definition AMReX_GpuQualifiers.H:18
static int deviceId() noexcept
Definition AMReX_GpuDevice.cpp:672
static int devicePropMajor() noexcept
Definition AMReX_GpuDevice.H:163
Definition AMReX_PODVector.H:297
Definition AMReX_BaseFwd.H:52
void dtod_memcpy_async(void *p_d_dst, const void *p_d_src, const std::size_t sz) noexcept
Definition AMReX_GpuDevice.H:317
void fillAsync(IT first, IT last, F const &f) noexcept
Fill the elements in the given range using the given calllable.
Definition AMReX_GpuContainers.H:406
void copy(HostToDevice, InIter begin, InIter end, OutIter result) noexcept
A host-to-device copy routine. Note this is just a wrapper around memcpy, so it assumes contiguous st...
Definition AMReX_GpuContainers.H:121
void copyAsync(HostToDevice, InIter begin, InIter end, OutIter result) noexcept
A host-to-device copy routine. Note this is just a wrapper around memcpy, so it assumes contiguous st...
Definition AMReX_GpuContainers.H:221
void prefetchToHost(Iter begin, Iter end) noexcept
Migrate elements of a container from device to host. This is a no-op for host-only code.
Definition AMReX_GpuContainers.H:310
static constexpr DeviceToDevice deviceToDevice
Definition AMReX_GpuContainers.H:100
static constexpr DeviceToHost deviceToHost
Definition AMReX_GpuContainers.H:99
static constexpr HostToDevice hostToDevice
Definition AMReX_GpuContainers.H:98
void streamSynchronize() noexcept
Definition AMReX_GpuDevice.H:260
void dtoh_memcpy_async(void *p_h, const void *p_d, const std::size_t sz) noexcept
Definition AMReX_GpuDevice.H:303
void dtoh_memcpy(void *p_h, const void *p_d, const std::size_t sz) noexcept
Definition AMReX_GpuDevice.H:364
void htod_memcpy(void *p_d, const void *p_h, const std::size_t sz) noexcept
Definition AMReX_GpuDevice.H:356
void htod_memcpy_async(void *p_d, const void *p_h, const std::size_t sz) noexcept
Definition AMReX_GpuDevice.H:289
void dtod_memcpy(void *p_d_dst, const void *p_d_src, const std::size_t sz) noexcept
Definition AMReX_GpuDevice.H:372
gpuStream_t gpuStream() noexcept
Definition AMReX_GpuDevice.H:241
void prefetchToDevice(Iter begin, Iter end) noexcept
Migrate elements of a container from host to device. This is a no-op for host-only code.
Definition AMReX_GpuContainers.H:351
std::enable_if_t< std::is_integral_v< T > > ParallelFor(TypeList< CTOs... > ctos, std::array< int, sizeof...(CTOs)> const &runtime_options, T N, F &&f)
Definition AMReX_CTOParallelForImpl.H:191
__host__ __device__ constexpr const T & min(const T &a, const T &b) noexcept
Definition AMReX_Algorithm.H:21
__host__ __device__ Dim3 begin(BoxND< dim > const &box) noexcept
Definition AMReX_Box.H:1899
__host__ __device__ Dim3 end(BoxND< dim > const &box) noexcept
Definition AMReX_Box.H:1908
Definition AMReX_GpuContainers.H:97
Definition AMReX_GpuContainers.H:96
Definition AMReX_GpuTypes.H:86
Definition AMReX_GpuContainers.H:95
Definition AMReX_GpuMemory.H:125
__device__ T * dataPtr() noexcept
Definition AMReX_GpuMemory.H:126
Test if a given type T is callable with arguments of type Args...
Definition AMReX_TypeTraits.H:209