Skip to content

Commit

Permalink
[CUDA][Bindless] Fix memory leak in interop mapping
Browse files Browse the repository at this point in the history
* 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.
  • Loading branch information
cppchedy committed Nov 8, 2024
1 parent ed9fe09 commit 15ff9fe
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 0 deletions.
3 changes: 3 additions & 0 deletions source/adapters/cuda/device.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<CUarray, CUmipmappedArray> ChildCuarrayFromMipmapMap;
};

int getAttribute(ur_device_handle_t Device, CUdevice_attribute Attribute);
7 changes: 7 additions & 0 deletions source/adapters/cuda/image.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 (...) {
Expand Down Expand Up @@ -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;
}

Expand Down

0 comments on commit 15ff9fe

Please sign in to comment.