Skip to content
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

[SYCL][CUDA] Change hint functions to return UR_SUCCESS #11296

Closed
Closed
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
72 changes: 30 additions & 42 deletions sycl/plugins/unified_runtime/ur/adapters/cuda/enqueue.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1298,36 +1298,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 @@ -1342,8 +1336,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 @@ -1358,11 +1354,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 @@ -1375,53 +1372,44 @@ 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
// check that the device also has the
// CU_DEVICE_ATTRIBUTE_PAGEABLE_MEMORY_ACCESS property.
}

unsigned int IsManaged;
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;
std::unique_ptr<ur_event_handle_t_> EventPtr{nullptr};

try {
ScopedContext Active(hQueue->getContext());

if (phEvent) {
EventPtr =
std::unique_ptr<ur_event_handle_t_>(ur_event_handle_t_::makeNative(
UR_COMMAND_USM_ADVISE, hQueue, hQueue->getNextTransferStream()));
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