Skip to content

Commit e825916

Browse files
[SYCL][CUDA] Correctly free managed memory (#4181)
Depending on the host system's CUDA implementation, CU_POINTER_ATTRIBUTE_MEMORY_TYPE queries may report different values for managed pointers. Managed memory must however always be freed using cuMemFree. This patch adds an additional case for the cuMemFree path in cuda_piextUSMFree. Signed-off-by: Steffen Larsen <[email protected]>
1 parent 18c80fa commit e825916

File tree

1 file changed

+11
-5
lines changed

1 file changed

+11
-5
lines changed

sycl/plugins/cuda/pi_cuda.cpp

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4473,14 +4473,20 @@ pi_result cuda_piextUSMFree(pi_context context, void *ptr) {
44734473
pi_result result = PI_SUCCESS;
44744474
try {
44754475
ScopedContext active(context);
4476+
bool is_managed;
44764477
unsigned int type;
4477-
result = PI_CHECK_ERROR(cuPointerGetAttribute(
4478-
&type, CU_POINTER_ATTRIBUTE_MEMORY_TYPE, (CUdeviceptr)ptr));
4478+
void *attribute_values[2] = {&is_managed, &type};
4479+
CUpointer_attribute attributes[2] = {CU_POINTER_ATTRIBUTE_IS_MANAGED,
4480+
CU_POINTER_ATTRIBUTE_MEMORY_TYPE};
4481+
result = PI_CHECK_ERROR(cuPointerGetAttributes(
4482+
2, attributes, attribute_values, (CUdeviceptr)ptr));
44794483
assert(type == CU_MEMORYTYPE_DEVICE or type == CU_MEMORYTYPE_HOST);
4480-
if (type == CU_MEMORYTYPE_DEVICE) {
4484+
if (is_managed || type == CU_MEMORYTYPE_DEVICE) {
4485+
// Memory allocated with cuMemAlloc and cuMemAllocManaged must be freed
4486+
// with cuMemFree
44814487
result = PI_CHECK_ERROR(cuMemFree((CUdeviceptr)ptr));
4482-
}
4483-
if (type == CU_MEMORYTYPE_HOST) {
4488+
} else {
4489+
// Memory allocated with cuMemAllocHost must be freed with cuMemFreeHost
44844490
result = PI_CHECK_ERROR(cuMemFreeHost(ptr));
44854491
}
44864492
} catch (pi_result error) {

0 commit comments

Comments
 (0)