Skip to content

Commit

Permalink
Explicitly specify USMFree as a non-blocking operation.
Browse files Browse the repository at this point in the history
Also update CL adapter to reflect this.
  • Loading branch information
aarongreig committed Nov 8, 2024
1 parent ed9fe09 commit 3bef74f
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 17 deletions.
7 changes: 7 additions & 0 deletions include/ur_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -3656,6 +3656,13 @@ urUSMSharedAlloc(
///////////////////////////////////////////////////////////////////////////////
/// @brief Free the USM memory object
///
/// @details
/// - Note that implementations are not obliged to wait for previously
/// enqueued operations that may be using `hMem` to finish before freeing
/// `hMem`.
/// - Care should be taken to properly synchronize calling this entry point
/// with any prior uses of `hMem`, especially kernel executions.
///
/// @returns
/// - ::UR_RESULT_SUCCESS
/// - ::UR_RESULT_ERROR_UNINITIALIZED
Expand Down
3 changes: 3 additions & 0 deletions scripts/core/usm.yml
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,9 @@ desc: "Free the USM memory object"
class: $xUSM
name: Free
ordinal: "0"
details:
- "Note that implementations are not obliged to wait for previously enqueued operations that may be using `hMem` to finish before freeing `hMem`."
- "Care should be taken to properly synchronize calling this entry point with any prior uses of `hMem`, especially kernel executions."
params:
- type: $x_context_handle_t
name: hContext
Expand Down
2 changes: 1 addition & 1 deletion source/adapters/opencl/common.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ namespace cl_ext {
CONSTFIX char HostMemAllocName[] = "clHostMemAllocINTEL";
CONSTFIX char DeviceMemAllocName[] = "clDeviceMemAllocINTEL";
CONSTFIX char SharedMemAllocName[] = "clSharedMemAllocINTEL";
CONSTFIX char MemBlockingFreeName[] = "clMemBlockingFreeINTEL";
CONSTFIX char MemFreeName[] = "clMemFreeINTEL";
CONSTFIX char CreateBufferWithPropertiesName[] =
"clCreateBufferWithPropertiesINTEL";
CONSTFIX char SetKernelArgMemPointerName[] = "clSetKernelArgMemPointerINTEL";
Expand Down
2 changes: 1 addition & 1 deletion source/adapters/opencl/extension_functions.def
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ CL_EXTENSION_FUNC(clSharedMemAllocINTEL)
CL_EXTENSION_FUNC(clGetDeviceFunctionPointer)
CL_EXTENSION_FUNC(clGetDeviceGlobalVariablePointer)
CL_EXTENSION_FUNC(clCreateBufferWithPropertiesINTEL)
CL_EXTENSION_FUNC(clMemBlockingFreeINTEL)
CL_EXTENSION_FUNC(clMemFreeINTEL)
CL_EXTENSION_FUNC(clSetKernelArgMemPointerINTEL)
CL_EXTENSION_FUNC(clEnqueueMemFillINTEL)
CL_EXTENSION_FUNC(clEnqueueMemcpyINTEL)
Expand Down
27 changes: 12 additions & 15 deletions source/adapters/opencl/usm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -239,16 +239,13 @@ urUSMSharedAlloc(ur_context_handle_t hContext, ur_device_handle_t hDevice,

UR_APIEXPORT ur_result_t UR_APICALL urUSMFree(ur_context_handle_t hContext,
void *pMem) {

// Use a blocking free to avoid issues with indirect access from kernels that
// might be still running.
clMemBlockingFreeINTEL_fn FuncPtr = nullptr;
clMemFreeINTEL_fn FuncPtr = nullptr;

cl_context CLContext = cl_adapter::cast<cl_context>(hContext);
ur_result_t RetVal = UR_RESULT_ERROR_INVALID_OPERATION;
RetVal = cl_ext::getExtFuncFromContext<clMemBlockingFreeINTEL_fn>(
CLContext, cl_ext::ExtFuncPtrCache->clMemBlockingFreeINTELCache,
cl_ext::MemBlockingFreeName, &FuncPtr);
RetVal = cl_ext::getExtFuncFromContext<clMemFreeINTEL_fn>(
CLContext, cl_ext::ExtFuncPtrCache->clMemFreeINTELCache,
cl_ext::MemFreeName, &FuncPtr);

if (FuncPtr) {
RetVal = mapCLErrorToUR(FuncPtr(CLContext, pMem));
Expand Down Expand Up @@ -299,10 +296,10 @@ UR_APIEXPORT ur_result_t UR_APICALL urEnqueueUSMFill(
CLContext, cl_ext::ExtFuncPtrCache->clEnqueueMemcpyINTELCache,
cl_ext::EnqueueMemcpyName, &USMMemcpy));

clMemBlockingFreeINTEL_fn USMFree = nullptr;
UR_RETURN_ON_FAILURE(cl_ext::getExtFuncFromContext<clMemBlockingFreeINTEL_fn>(
CLContext, cl_ext::ExtFuncPtrCache->clMemBlockingFreeINTELCache,
cl_ext::MemBlockingFreeName, &USMFree));
clMemFreeINTEL_fn USMFree = nullptr;
UR_RETURN_ON_FAILURE(cl_ext::getExtFuncFromContext<clMemFreeINTEL_fn>(
CLContext, cl_ext::ExtFuncPtrCache->clMemFreeINTELCache,
cl_ext::MemFreeName, &USMFree));

cl_int ClErr = CL_SUCCESS;
auto HostBuffer =
Expand Down Expand Up @@ -369,10 +366,10 @@ UR_APIEXPORT ur_result_t UR_APICALL urEnqueueUSMMemcpy(
CLContext, cl_ext::ExtFuncPtrCache->clEnqueueMemcpyINTELCache,
cl_ext::EnqueueMemcpyName, &USMMemcpy));

clMemBlockingFreeINTEL_fn USMFree = nullptr;
UR_RETURN_ON_FAILURE(cl_ext::getExtFuncFromContext<clMemBlockingFreeINTEL_fn>(
CLContext, cl_ext::ExtFuncPtrCache->clMemBlockingFreeINTELCache,
cl_ext::MemBlockingFreeName, &USMFree));
clMemFreeINTEL_fn USMFree = nullptr;
UR_RETURN_ON_FAILURE(cl_ext::getExtFuncFromContext<clMemFreeINTEL_fn>(
CLContext, cl_ext::ExtFuncPtrCache->clMemFreeINTELCache,
cl_ext::MemFreeName, &USMFree));

// Check if the two allocations are DEVICE allocations from different
// devices, if they are we need to do the copy indirectly via a host
Expand Down
7 changes: 7 additions & 0 deletions source/loader/ur_libapi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2404,6 +2404,13 @@ ur_result_t UR_APICALL urUSMSharedAlloc(
///////////////////////////////////////////////////////////////////////////////
/// @brief Free the USM memory object
///
/// @details
/// - Note that implementations are not obliged to wait for previously
/// enqueued operations that may be using `hMem` to finish before freeing
/// `hMem`.
/// - Care should be taken to properly synchronize calling this entry point
/// with any prior uses of `hMem`, especially kernel executions.
///
/// @returns
/// - ::UR_RESULT_SUCCESS
/// - ::UR_RESULT_ERROR_UNINITIALIZED
Expand Down
7 changes: 7 additions & 0 deletions source/ur_api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2080,6 +2080,13 @@ ur_result_t UR_APICALL urUSMSharedAlloc(
///////////////////////////////////////////////////////////////////////////////
/// @brief Free the USM memory object
///
/// @details
/// - Note that implementations are not obliged to wait for previously
/// enqueued operations that may be using `hMem` to finish before freeing
/// `hMem`.
/// - Care should be taken to properly synchronize calling this entry point
/// with any prior uses of `hMem`, especially kernel executions.
///
/// @returns
/// - ::UR_RESULT_SUCCESS
/// - ::UR_RESULT_ERROR_UNINITIALIZED
Expand Down

0 comments on commit 3bef74f

Please sign in to comment.