Skip to content

[SYCL][CUDA] Add support for ALLOC_HOST_PTR #1997

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 6, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions sycl/include/CL/sycl/detail/pi.h
Original file line number Diff line number Diff line change
Expand Up @@ -508,6 +508,7 @@ constexpr pi_mem_flags PI_MEM_FLAGS_ACCESS_RW = CL_MEM_READ_WRITE;
// Host pointer
constexpr pi_mem_flags PI_MEM_FLAGS_HOST_PTR_USE = CL_MEM_USE_HOST_PTR;
constexpr pi_mem_flags PI_MEM_FLAGS_HOST_PTR_COPY = CL_MEM_COPY_HOST_PTR;
constexpr pi_mem_flags PI_MEM_FLAGS_HOST_PTR_ALLOC = CL_MEM_ALLOC_HOST_PTR;

// NOTE: queue properties are implemented this way to better support bit
// manipulations
Expand Down
6 changes: 6 additions & 0 deletions sycl/plugins/cuda/pi_cuda.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1511,6 +1511,10 @@ pi_result cuda_piMemBufferCreate(pi_context context, pi_mem_flags flags,
cuMemHostRegister(host_ptr, size, CU_MEMHOSTREGISTER_DEVICEMAP));
retErr = PI_CHECK_ERROR(cuMemHostGetDevicePointer(&ptr, host_ptr, 0));
allocMode = _pi_mem::alloc_mode::use_host_ptr;
} else if (flags & PI_MEM_FLAGS_HOST_PTR_ALLOC) {
retErr = PI_CHECK_ERROR(cuMemAllocHost(&host_ptr, size));
retErr = PI_CHECK_ERROR(cuMemHostGetDevicePointer(&ptr, host_ptr, 0));
allocMode = _pi_mem::alloc_mode::alloc_host_ptr;
} else {
retErr = PI_CHECK_ERROR(cuMemAlloc(&ptr, size));
if (flags & PI_MEM_FLAGS_HOST_PTR_COPY) {
Expand Down Expand Up @@ -1582,6 +1586,8 @@ pi_result cuda_piMemRelease(pi_mem memObj) {
case _pi_mem::alloc_mode::use_host_ptr:
ret = PI_CHECK_ERROR(cuMemHostUnregister(uniqueMemObj->hostPtr_));
break;
case _pi_mem::alloc_mode::alloc_host_ptr:
ret = PI_CHECK_ERROR(cuMemFreeHost(uniqueMemObj->hostPtr_));
};
}

Expand Down
8 changes: 7 additions & 1 deletion sycl/plugins/cuda/pi_cuda.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -197,8 +197,14 @@ struct _pi_mem {
* use_host_ptr: Use an address on the host for the device
* copy_in: The data for the device comes from the host but the host pointer
is not available later for re-use
* alloc_host_ptr: Uses pinned-memory allocation
*/
enum class alloc_mode { classic, use_host_ptr, copy_in } allocMode_;
enum class alloc_mode {
classic,
use_host_ptr,
copy_in,
alloc_host_ptr
} allocMode_;

_pi_mem(pi_context ctxt, pi_mem parent, alloc_mode mode, CUdeviceptr ptr, void *host_ptr,
size_t size)
Expand Down
12 changes: 12 additions & 0 deletions sycl/unittests/pi/cuda/test_mem_obj.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,18 @@ TEST_F(CudaTestMemObj, piMemBufferCreateSimple) {
PI_SUCCESS);
}

TEST_F(CudaTestMemObj, piMemBufferAllocHost) {
const size_t memSize = 1024u;
pi_mem memObj;
ASSERT_EQ((plugin.call_nocheck<detail::PiApiKind::piMemBufferCreate>(
context_, PI_MEM_FLAGS_ACCESS_RW | PI_MEM_FLAGS_HOST_PTR_ALLOC,
memSize, nullptr, &memObj)),
PI_SUCCESS);

ASSERT_EQ((plugin.call_nocheck<detail::PiApiKind::piMemRelease>(memObj)),
PI_SUCCESS);
}

TEST_F(CudaTestMemObj, piMemBufferCreateNoActiveContext) {
const size_t memSize = 1024u;
// Context has been destroyed
Expand Down