Skip to content

Commit aee2d6c

Browse files
authored
[SYCL][CUDA] Add support for ALLOC_HOST_PTR (#1997)
Signed-off-by: Ruyman Reyes <[email protected]>
1 parent d0a148a commit aee2d6c

File tree

4 files changed

+26
-1
lines changed

4 files changed

+26
-1
lines changed

sycl/include/CL/sycl/detail/pi.h

+1
Original file line numberDiff line numberDiff line change
@@ -508,6 +508,7 @@ constexpr pi_mem_flags PI_MEM_FLAGS_ACCESS_RW = CL_MEM_READ_WRITE;
508508
// Host pointer
509509
constexpr pi_mem_flags PI_MEM_FLAGS_HOST_PTR_USE = CL_MEM_USE_HOST_PTR;
510510
constexpr pi_mem_flags PI_MEM_FLAGS_HOST_PTR_COPY = CL_MEM_COPY_HOST_PTR;
511+
constexpr pi_mem_flags PI_MEM_FLAGS_HOST_PTR_ALLOC = CL_MEM_ALLOC_HOST_PTR;
511512

512513
// NOTE: queue properties are implemented this way to better support bit
513514
// manipulations

sycl/plugins/cuda/pi_cuda.cpp

+6
Original file line numberDiff line numberDiff line change
@@ -1650,6 +1650,10 @@ pi_result cuda_piMemBufferCreate(pi_context context, pi_mem_flags flags,
16501650
cuMemHostRegister(host_ptr, size, CU_MEMHOSTREGISTER_DEVICEMAP));
16511651
retErr = PI_CHECK_ERROR(cuMemHostGetDevicePointer(&ptr, host_ptr, 0));
16521652
allocMode = _pi_mem::alloc_mode::use_host_ptr;
1653+
} else if (flags & PI_MEM_FLAGS_HOST_PTR_ALLOC) {
1654+
retErr = PI_CHECK_ERROR(cuMemAllocHost(&host_ptr, size));
1655+
retErr = PI_CHECK_ERROR(cuMemHostGetDevicePointer(&ptr, host_ptr, 0));
1656+
allocMode = _pi_mem::alloc_mode::alloc_host_ptr;
16531657
} else {
16541658
retErr = PI_CHECK_ERROR(cuMemAlloc(&ptr, size));
16551659
if (flags & PI_MEM_FLAGS_HOST_PTR_COPY) {
@@ -1721,6 +1725,8 @@ pi_result cuda_piMemRelease(pi_mem memObj) {
17211725
case _pi_mem::alloc_mode::use_host_ptr:
17221726
ret = PI_CHECK_ERROR(cuMemHostUnregister(uniqueMemObj->hostPtr_));
17231727
break;
1728+
case _pi_mem::alloc_mode::alloc_host_ptr:
1729+
ret = PI_CHECK_ERROR(cuMemFreeHost(uniqueMemObj->hostPtr_));
17241730
};
17251731
}
17261732

sycl/plugins/cuda/pi_cuda.hpp

+7-1
Original file line numberDiff line numberDiff line change
@@ -197,8 +197,14 @@ struct _pi_mem {
197197
* use_host_ptr: Use an address on the host for the device
198198
* copy_in: The data for the device comes from the host but the host pointer
199199
is not available later for re-use
200+
* alloc_host_ptr: Uses pinned-memory allocation
200201
*/
201-
enum class alloc_mode { classic, use_host_ptr, copy_in } allocMode_;
202+
enum class alloc_mode {
203+
classic,
204+
use_host_ptr,
205+
copy_in,
206+
alloc_host_ptr
207+
} allocMode_;
202208

203209
_pi_mem(pi_context ctxt, pi_mem parent, alloc_mode mode, CUdeviceptr ptr, void *host_ptr,
204210
size_t size)

sycl/unittests/pi/cuda/test_mem_obj.cpp

+12
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,18 @@ TEST_F(CudaTestMemObj, piMemBufferCreateSimple) {
7373
PI_SUCCESS);
7474
}
7575

76+
TEST_F(CudaTestMemObj, piMemBufferAllocHost) {
77+
const size_t memSize = 1024u;
78+
pi_mem memObj;
79+
ASSERT_EQ((plugin.call_nocheck<detail::PiApiKind::piMemBufferCreate>(
80+
context_, PI_MEM_FLAGS_ACCESS_RW | PI_MEM_FLAGS_HOST_PTR_ALLOC,
81+
memSize, nullptr, &memObj)),
82+
PI_SUCCESS);
83+
84+
ASSERT_EQ((plugin.call_nocheck<detail::PiApiKind::piMemRelease>(memObj)),
85+
PI_SUCCESS);
86+
}
87+
7688
TEST_F(CudaTestMemObj, piMemBufferCreateNoActiveContext) {
7789
const size_t memSize = 1024u;
7890
// Context has been destroyed

0 commit comments

Comments
 (0)