54#include <hip/hip_runtime.h>
61 unsigned char v,
size_t n)
63 const size_t idx = blockIdx.x * (size_t) blockDim.x + threadIdx.x;
64 const size_t str = (size_t) blockDim.x * gridDim.x;
65 for (
size_t i = idx;
i < n;
i += str)
74 const unsigned long long
75 * __restrict__ b,
size_t n)
77 const size_t idx = blockIdx.x * (size_t) blockDim.x + threadIdx.x;
78 const size_t str = (size_t) blockDim.x * gridDim.x;
79 for (
size_t i = idx;
i < n;
i += str)
84 const unsigned char * __restrict__ b,
87 const size_t idx = blockIdx.x * (size_t) blockDim.x + threadIdx.x;
88 const size_t str = (size_t) blockDim.x * gridDim.x;
89 for (
size_t i = idx;
i < n;
i += str)
124 static int zerocopy = -1;
126 const char *env = getenv(
"NEKO_HIP_ZEROCOPY");
128 if (env != NULL && atoi(env) != 0) {
129 int dev = 0, integrated = 0, pageable = 0;
130 if (hipGetDevice(&dev) == hipSuccess &&
131 hipDeviceGetAttribute(&integrated,
132 hipDeviceAttributeIntegrated,
133 dev) == hipSuccess &&
134 hipDeviceGetAttribute(&pageable,
135 hipDeviceAttributePageableMemoryAccess,
137 zerocopy = (integrated && pageable);
140 (void) hipGetLastError();
144 "Neko: warning: NEKO_HIP_ZEROCOPY=1 requested but this "
145 "device does not support zero-copy mapping (needs a "
146 "unified-memory APU with XNACK); using replicated "
165hipError_t
hip_map(
void **ptr_d,
void *ptr_h,
size_t s)
168 static int warned = 0;
173 if (hipGetDevice(&dev) == hipSuccess) {
174 if (hipMemAdvise(ptr_h, s, hipMemAdviseSetCoarseGrain, dev)
175 != hipSuccess && !warned) {
177 "Neko: warning: coarse-grain advise of zero-copy mapping "
178 "failed; mapped arrays stay fine-grained (slower kernels)\n");
182 (void) hipGetLastError();
188 const uintptr_t pgsz = (uintptr_t) sysconf(_SC_PAGESIZE);
189 const uintptr_t beg = ((uintptr_t) ptr_h + pgsz - 1) & ~(pgsz - 1);
190 const uintptr_t end = ((uintptr_t) ptr_h + s) & ~(pgsz - 1);
192 (void) madvise((
void *) beg, end - beg, MADV_HUGEPAGE);
198 return hipMalloc(ptr_d, s);
207 hipPointerAttribute_t attr;
208 if (hipPointerGetAttributes(&attr, ptr_d) != hipSuccess) {
209 (void) hipGetLastError();
212#if HIP_VERSION_MAJOR >= 6
213 return (attr.type == hipMemoryTypeDevice);
215 return (attr.memoryType == hipMemoryTypeDevice);
231 return hipDeviceSynchronize();
233 return hipFree(ptr_d);
246 const size_t nb = (s + 1024 - 1) / 1024;
247 const dim3 nthrds(1024, 1, 1);
248 const dim3 nblcks((
unsigned int) (nb > 65535 ? 65535 :
249 (nb == 0 ? 1 : nb)), 1, 1);
251 (hipStream_t) stream,
252 (
unsigned char *) ptr_d, (
unsigned char) value, s);
253 return hipGetLastError();
256 return hipMemsetAsync(ptr_d, value, s, (hipStream_t) stream);
270 int kind,
void *stream)
275 if ((((uintptr_t) dst | (uintptr_t) src | s) & 7) == 0) {
276 const size_t n = s / 8;
277 const size_t nb = (n + 1024 - 1) / 1024;
278 const dim3 nthrds(1024, 1, 1);
279 const dim3 nblcks((
unsigned int) (nb > 65535 ? 65535 : nb), 1, 1);
281 (hipStream_t) stream,
282 (
unsigned long long *) dst,
283 (
const unsigned long long *) src, n);
286 const size_t nb = (s + 1024 - 1) / 1024;
287 const dim3 nthrds(1024, 1, 1);
288 const dim3 nblcks((
unsigned int) (nb > 65535 ? 65535 : nb), 1, 1);
290 (hipStream_t) stream,
291 (
unsigned char *) dst,
292 (
const unsigned char *) src, s);
294 return hipGetLastError();
297 return hipMemcpyAsync(dst, src, s, (hipMemcpyKind) kind,
298 (hipStream_t) stream);
__global__ void T *__restrict__ T *__restrict__ const T *__restrict__ const T *__restrict__ v
__global__ void hip_unified_copy8_kernel(unsigned long long *__restrict__ a, const unsigned long long *__restrict__ b, size_t n)
__global__ void hip_unified_copy1_kernel(unsigned char *__restrict__ a, const unsigned char *__restrict__ b, size_t n)
hipError_t hip_map(void **ptr_d, void *ptr_h, size_t s)
static int hip_is_device_alloc(void *ptr_d)
hipError_t hip_map_memset(void *ptr_d, int value, size_t s, void *stream)
hipError_t hip_map_free(void *ptr_d)
__global__ void hip_unified_memset_kernel(unsigned char *a, unsigned char v, size_t n)
hipError_t hip_map_memcpy(void *dst, void *src, size_t s, int kind, void *stream)