Skip to content

Commit

Permalink
[CUDA] Change hint functions to return UR_SUCCESS
Browse files Browse the repository at this point in the history
- The UR spec was recently changed to make hint entryponts
always return UR_RESULT_SUCCESS. This commit changes the CUDA
adapter to be conformant with this change.
- This commit also changes the type of PointerRangeSize which
 was causing a stack corruption.
  • Loading branch information
fabiomestre committed Oct 23, 2023
1 parent 3a3aae3 commit 9c77328
Showing 1 changed file with 30 additions and 37 deletions.
67 changes: 30 additions & 37 deletions source/adapters/cuda/enqueue.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1300,36 +1300,30 @@ UR_APIEXPORT ur_result_t UR_APICALL urEnqueueUSMPrefetch(
ur_queue_handle_t hQueue, const void *pMem, size_t size,
ur_usm_migration_flags_t flags, uint32_t numEventsInWaitList,
const ur_event_handle_t *phEventWaitList, ur_event_handle_t *phEvent) {
unsigned int PointerRangeSize = 0;
std::ignore = flags;

size_t PointerRangeSize = 0;
UR_CHECK_ERROR(cuPointerGetAttribute(
&PointerRangeSize, CU_POINTER_ATTRIBUTE_RANGE_SIZE, (CUdeviceptr)pMem));
UR_ASSERT(size <= PointerRangeSize, UR_RESULT_ERROR_INVALID_SIZE);
ur_device_handle_t Device = hQueue->getContext()->getDevice();

bool Supported = true;
// Certain cuda devices and Windows do not have support for some Unified
// Memory features. cuMemPrefetchAsync requires concurrent memory access
// for managed memory. Therfore, ignore prefetch hint if concurrent managed
// for managed memory. Therefore, ignore prefetch hint if concurrent managed
// memory access is not available.
if (!getAttribute(Device, CU_DEVICE_ATTRIBUTE_CONCURRENT_MANAGED_ACCESS)) {
setErrorMessage("Prefetch hint ignored as device does not support "
"concurrent managed access",
UR_RESULT_SUCCESS);
return UR_RESULT_ERROR_ADAPTER_SPECIFIC;
Supported = false;
}

unsigned int IsManaged;
UR_CHECK_ERROR(cuPointerGetAttribute(
&IsManaged, CU_POINTER_ATTRIBUTE_IS_MANAGED, (CUdeviceptr)pMem));
if (!IsManaged) {
setErrorMessage("Prefetch hint ignored as prefetch only works with USM",
UR_RESULT_SUCCESS);
return UR_RESULT_ERROR_ADAPTER_SPECIFIC;
Supported = false;
}

// flags is currently unused so fail if set
if (flags != 0)
return UR_RESULT_ERROR_INVALID_VALUE;

ur_result_t Result = UR_RESULT_SUCCESS;
std::unique_ptr<ur_event_handle_t_> EventPtr{nullptr};

Expand All @@ -1344,8 +1338,10 @@ UR_APIEXPORT ur_result_t UR_APICALL urEnqueueUSMPrefetch(
UR_COMMAND_MEM_BUFFER_COPY, hQueue, CuStream));
UR_CHECK_ERROR(EventPtr->start());
}
UR_CHECK_ERROR(
cuMemPrefetchAsync((CUdeviceptr)pMem, size, Device->get(), CuStream));
if (Supported) {
UR_CHECK_ERROR(
cuMemPrefetchAsync((CUdeviceptr)pMem, size, Device->get(), CuStream));
}
if (phEvent) {
UR_CHECK_ERROR(EventPtr->record());
*phEvent = EventPtr.release();
Expand All @@ -1360,11 +1356,12 @@ UR_APIEXPORT ur_result_t UR_APICALL urEnqueueUSMPrefetch(
UR_APIEXPORT ur_result_t UR_APICALL
urEnqueueUSMAdvise(ur_queue_handle_t hQueue, const void *pMem, size_t size,
ur_usm_advice_flags_t advice, ur_event_handle_t *phEvent) {
unsigned int PointerRangeSize = 0;
size_t PointerRangeSize = 0;
UR_CHECK_ERROR(cuPointerGetAttribute(
&PointerRangeSize, CU_POINTER_ATTRIBUTE_RANGE_SIZE, (CUdeviceptr)pMem));
UR_ASSERT(size <= PointerRangeSize, UR_RESULT_ERROR_INVALID_SIZE);

bool Supported = true;
// Certain cuda devices and Windows do not have support for some Unified
// Memory features. Passing CU_MEM_ADVISE_SET/CLEAR_PREFERRED_LOCATION and
// to cuMemAdvise on a GPU device requires the GPU device to report a non-zero
Expand All @@ -1377,10 +1374,7 @@ urEnqueueUSMAdvise(ur_queue_handle_t hQueue, const void *pMem, size_t size,
(advice & UR_USM_ADVICE_FLAG_DEFAULT)) {
ur_device_handle_t Device = hQueue->getContext()->getDevice();
if (!getAttribute(Device, CU_DEVICE_ATTRIBUTE_CONCURRENT_MANAGED_ACCESS)) {
setErrorMessage("Mem advise ignored as device does not support "
"concurrent managed access",
UR_RESULT_SUCCESS);
return UR_RESULT_ERROR_ADAPTER_SPECIFIC;
Supported = false;
}

// TODO: If ptr points to valid system-allocated pageable memory we should
Expand All @@ -1392,10 +1386,7 @@ urEnqueueUSMAdvise(ur_queue_handle_t hQueue, const void *pMem, size_t size,
UR_CHECK_ERROR(cuPointerGetAttribute(
&IsManaged, CU_POINTER_ATTRIBUTE_IS_MANAGED, (CUdeviceptr)pMem));
if (!IsManaged) {
setErrorMessage(
"Memory advice ignored as memory advices only works with USM",
UR_RESULT_SUCCESS);
return UR_RESULT_ERROR_ADAPTER_SPECIFIC;
Supported = false;
}

ur_result_t Result = UR_RESULT_SUCCESS;
Expand All @@ -1411,19 +1402,21 @@ urEnqueueUSMAdvise(ur_queue_handle_t hQueue, const void *pMem, size_t size,
UR_CHECK_ERROR(EventPtr->start());
}

if (advice & UR_USM_ADVICE_FLAG_DEFAULT) {
UR_CHECK_ERROR(cuMemAdvise((CUdeviceptr)pMem, size,
CU_MEM_ADVISE_UNSET_READ_MOSTLY,
hQueue->getContext()->getDevice()->get()));
UR_CHECK_ERROR(cuMemAdvise((CUdeviceptr)pMem, size,
CU_MEM_ADVISE_UNSET_PREFERRED_LOCATION,
hQueue->getContext()->getDevice()->get()));
UR_CHECK_ERROR(cuMemAdvise((CUdeviceptr)pMem, size,
CU_MEM_ADVISE_UNSET_ACCESSED_BY,
hQueue->getContext()->getDevice()->get()));
} else {
Result = setCuMemAdvise((CUdeviceptr)pMem, size, advice,
hQueue->getContext()->getDevice()->get());
if (Supported) {
if (advice & UR_USM_ADVICE_FLAG_DEFAULT) {
UR_CHECK_ERROR(cuMemAdvise((CUdeviceptr)pMem, size,
CU_MEM_ADVISE_UNSET_READ_MOSTLY,
hQueue->getContext()->getDevice()->get()));
UR_CHECK_ERROR(cuMemAdvise((CUdeviceptr)pMem, size,
CU_MEM_ADVISE_UNSET_PREFERRED_LOCATION,
hQueue->getContext()->getDevice()->get()));
UR_CHECK_ERROR(cuMemAdvise((CUdeviceptr)pMem, size,
CU_MEM_ADVISE_UNSET_ACCESSED_BY,
hQueue->getContext()->getDevice()->get()));
} else {
Result = setCuMemAdvise((CUdeviceptr)pMem, size, advice,
hQueue->getContext()->getDevice()->get());
}
}

if (phEvent) {
Expand Down

0 comments on commit 9c77328

Please sign in to comment.