Skip to content

[SYCL][CUDA] Correctly free managed memory #4181

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 26, 2021
Merged
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
16 changes: 11 additions & 5 deletions sycl/plugins/cuda/pi_cuda.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4473,14 +4473,20 @@ pi_result cuda_piextUSMFree(pi_context context, void *ptr) {
pi_result result = PI_SUCCESS;
try {
ScopedContext active(context);
bool is_managed;
unsigned int type;
result = PI_CHECK_ERROR(cuPointerGetAttribute(
&type, CU_POINTER_ATTRIBUTE_MEMORY_TYPE, (CUdeviceptr)ptr));
void *attribute_values[2] = {&is_managed, &type};
CUpointer_attribute attributes[2] = {CU_POINTER_ATTRIBUTE_IS_MANAGED,
CU_POINTER_ATTRIBUTE_MEMORY_TYPE};
result = PI_CHECK_ERROR(cuPointerGetAttributes(
2, attributes, attribute_values, (CUdeviceptr)ptr));
assert(type == CU_MEMORYTYPE_DEVICE or type == CU_MEMORYTYPE_HOST);
if (type == CU_MEMORYTYPE_DEVICE) {
if (is_managed || type == CU_MEMORYTYPE_DEVICE) {
// Memory allocated with cuMemAlloc and cuMemAllocManaged must be freed
// with cuMemFree
result = PI_CHECK_ERROR(cuMemFree((CUdeviceptr)ptr));
}
if (type == CU_MEMORYTYPE_HOST) {
} else {
// Memory allocated with cuMemAllocHost must be freed with cuMemFreeHost
result = PI_CHECK_ERROR(cuMemFreeHost(ptr));
}
} catch (pi_result error) {
Expand Down