From 73ba29bfe9dfbd8ac985051f7a81a349ed4e3e08 Mon Sep 17 00:00:00 2001 From: "chedy.najjar" Date: Fri, 27 Sep 2024 11:57:40 +0100 Subject: [PATCH] [CUDA][Bindless] Fix memory leak in interop mapping * added a map to ur_device_handle_t_. * Capture the leaking Cumipmappedarray in urBindlessImagesMapExternalArrayExp into map. * Update urBindlessImagesImageFreeExp to check if the Cuarray is derived, then destroy the corresponding Cumipmappedarray. --- source/adapters/cuda/device.hpp | 3 +++ source/adapters/cuda/image.cpp | 7 +++++++ 2 files changed, 10 insertions(+) diff --git a/source/adapters/cuda/device.hpp b/source/adapters/cuda/device.hpp index 3654f2bb36..d9f6310103 100644 --- a/source/adapters/cuda/device.hpp +++ b/source/adapters/cuda/device.hpp @@ -114,6 +114,9 @@ struct ur_device_handle_t_ { bool maxLocalMemSizeChosen() { return MaxLocalMemSizeChosen; }; uint32_t getNumComputeUnits() const noexcept { return NumComputeUnits; }; + + // bookkeeping for mipmappedArray leaks in Mapping external Memory + std::map ChildCuarrayFromMipmapMap; }; int getAttribute(ur_device_handle_t Device, CUdevice_attribute Attribute); diff --git a/source/adapters/cuda/image.cpp b/source/adapters/cuda/image.cpp index 4840553cc1..c11a85b293 100644 --- a/source/adapters/cuda/image.cpp +++ b/source/adapters/cuda/image.cpp @@ -430,6 +430,11 @@ UR_APIEXPORT ur_result_t UR_APICALL urBindlessImagesImageFreeExp( ScopedContext Active(hDevice); try { UR_CHECK_ERROR(cuArrayDestroy((CUarray)hImageMem)); + if (auto it = hDevice->ChildCuarrayFromMipmapMap.find((CUarray)hImageMem); + it != hDevice->ChildCuarrayFromMipmapMap.end()) { + UR_CHECK_ERROR(cuMipmappedArrayDestroy((CUmipmappedArray)it->second)); + hDevice->ChildCuarrayFromMipmapMap.erase(it); + } } catch (ur_result_t Err) { return Err; } catch (...) { @@ -1104,6 +1109,8 @@ UR_APIEXPORT ur_result_t UR_APICALL urBindlessImagesMapExternalArrayExp( CUarray memArray; UR_CHECK_ERROR(cuMipmappedArrayGetLevel(&memArray, memMipMap, 0)); + hDevice->ChildCuarrayFromMipmapMap.emplace(memArray, memMipMap); + *phImageMem = (ur_exp_image_mem_native_handle_t)memArray; }