@@ -237,6 +237,36 @@ void format_cuda_error(char* buffer, size_t size, const char* operation, CUresul
237237 }
238238}
239239
240+ // Bits of a resource handle for diagnostics: a pointer as its address, an
241+ // integer handle (CUdeviceptr, CUtexObject, ...) as its value, and a pointer
242+ // to a handle (nvrtcDestroyProgram(&prog) and friends) as the handle it points
243+ // to.
244+ template <typename T>
245+ unsigned long long handle_bits (const T& value) noexcept {
246+ using U = std::remove_cv_t <std::remove_reference_t <T>>;
247+ if constexpr (std::is_pointer_v<U>) {
248+ using P = std::remove_cv_t <std::remove_pointer_t <U>>;
249+ if constexpr (std::is_pointer_v<P>) {
250+ return value ? handle_bits (*value) : 0ull ;
251+ } else {
252+ return static_cast <unsigned long long >(reinterpret_cast <uintptr_t >(value));
253+ }
254+ } else if constexpr (std::is_integral_v<U> || std::is_enum_v<U>) {
255+ return static_cast <unsigned long long >(value);
256+ } else {
257+ return 0ull ;
258+ }
259+ }
260+
261+ // "<operation>(<handle>)": naming the resource keeps independent failures of
262+ // the same call distinct. Python's warning registry collapses repeated warnings
263+ // with identical text from one call site, so without the handle only the first
264+ // of several leaked resources would be reported.
265+ void format_operation (char * buffer, size_t size, const char * operation,
266+ unsigned long long handle) noexcept {
267+ std::snprintf (buffer, size, " %s(%#llx)" , operation, handle);
268+ }
269+
240270} // namespace
241271
242272// Report a message that could not be raised. Emits cuda.core.CUDAWarning via
@@ -538,8 +568,11 @@ CUresult invoke_in_context_or_undo(const ContextHandle& h_context, Fn&& operatio
538568// operation or activation status; restoration never changes the return value.
539569template <typename Fn, typename ... Args>
540570CUresult cleanup_in_context (const ContextHandle& h_context, const char * name,
541- Fn&& operation, Args&&... args) noexcept {
571+ unsigned long long handle, Fn&& operation,
572+ Args&&... args) noexcept {
542573 ASSERT_NOTHROW_INVOCABLE (Fn&&, Args&&...);
574+ char operation_name[160 ];
575+ format_operation (operation_name, sizeof (operation_name), name, handle);
543576 CUcontext previous = nullptr ;
544577 int changed = 0 ;
545578 const char * detail = nullptr ;
@@ -551,12 +584,12 @@ CUresult cleanup_in_context(const ContextHandle& h_context, const char* name,
551584 }
552585 CUresult restore = exit_context (previous, changed, CUDA_SUCCESS );
553586 if (status != CUDA_SUCCESS ) {
554- report_cuda_error (name , status, detail);
587+ report_cuda_error (operation_name , status, detail);
555588 }
556589 if (restore != CUDA_SUCCESS ) {
557590 // Nothing is raised here, so the detail exit_context recorded has no
558591 // exception to attach to: report it and drop the detail.
559- report_cuda_error (name , restore, " failed while restoring the caller's context" );
592+ report_cuda_error (operation_name , restore, " failed while restoring the caller's context" );
560593 clear_last_error_detail ();
561594 }
562595 return status;
@@ -572,22 +605,33 @@ class WarnOnFailure {
572605public:
573606 explicit WarnOnFailure (const char * operation) noexcept : operation_(operation) {}
574607
575- template <typename ... Args>
576- auto operator ()(Args&&... args) const noexcept {
577- auto status = Function (std::forward<Args>(args)...);
578- report (status);
608+ // The first argument is the resource being released; it is named in the
609+ // report so that independent failures are not collapsed by the warning
610+ // registry (see format_operation).
611+ template <typename First, typename ... Rest>
612+ auto operator ()(First&& first, Rest&&... rest) const noexcept {
613+ const unsigned long long handle = handle_bits (first);
614+ auto status = Function (std::forward<First>(first), std::forward<Rest>(rest)...);
615+ report (status, handle);
579616 return status;
580617 }
581618
582619private:
583- void report (CUresult status) const noexcept {
584- report_cuda_error (operation_, status);
620+ void report (CUresult status, unsigned long long handle) const noexcept {
621+ if (status == CUDA_SUCCESS || status == CUDA_ERROR_DEINITIALIZED ) {
622+ return ;
623+ }
624+ char operation[160 ];
625+ format_operation (operation, sizeof (operation), operation_, handle);
626+ report_cuda_error (operation, status);
585627 }
586628
587629 template <typename Status>
588- void report (Status status) const noexcept {
630+ void report (Status status, unsigned long long handle ) const noexcept {
589631 if (static_cast <long >(status) != 0 ) {
590- report_status_code (operation_, static_cast <long >(status));
632+ char operation[160 ];
633+ format_operation (operation, sizeof (operation), operation_, handle);
634+ report_status_code (operation, static_cast <long >(status));
591635 }
592636 }
593637
@@ -1544,7 +1588,7 @@ DevicePtrHandle deviceptr_alloc_from_pool(size_t size, const MemoryPoolHandle& h
15441588 GILReleaseGuard gil;
15451589 const DeallocationStream& stream = b->deallocation ;
15461590 cleanup_in_context (
1547- deallocation_context (stream), " cuMemFreeAsync" ,
1591+ deallocation_context (stream), " cuMemFreeAsync" , handle_bits (b-> resource ),
15481592 [&]() noexcept {
15491593 return p_cuMemFreeAsync (
15501594 b->resource , as_cu (stream.h_stream ));
@@ -1574,7 +1618,7 @@ DevicePtrHandle deviceptr_alloc_async(size_t size, const StreamHandle& h_stream)
15741618 GILReleaseGuard gil;
15751619 const DeallocationStream& stream = b->deallocation ;
15761620 cleanup_in_context (
1577- deallocation_context (stream), " cuMemFreeAsync" ,
1621+ deallocation_context (stream), " cuMemFreeAsync" , handle_bits (b-> resource ),
15781622 [&]() noexcept {
15791623 return p_cuMemFreeAsync (
15801624 b->resource , as_cu (stream.h_stream ));
@@ -1659,7 +1703,7 @@ DevicePtrHandle deviceptr_create_mapped_graphics(
16591703 CUgraphicsResource resource = as_cu (h_resource);
16601704 const DeallocationStream& stream = b->deallocation ;
16611705 cleanup_in_context (
1662- deallocation_context (stream), " cuGraphicsUnmapResources" ,
1706+ deallocation_context (stream), " cuGraphicsUnmapResources" , handle_bits (resource),
16631707 [&]() noexcept {
16641708 return p_cuGraphicsUnmapResources (
16651709 1 , &resource, as_cu (stream.h_stream ));
@@ -1698,7 +1742,7 @@ DevicePtrHandle deviceptr_create_with_mr(CUdeviceptr ptr, size_t size, PyObject*
16981742 if (mr_dealloc_cb) {
16991743 const DeallocationStream& stream = b->deallocation ;
17001744 cleanup_in_context (
1701- deallocation_context (stream), " MemoryResource.deallocate" ,
1745+ deallocation_context (stream), " MemoryResource.deallocate" , handle_bits (b-> resource ),
17021746 [&]() noexcept {
17031747 mr_dealloc_cb (mr, b->resource , size, stream.h_stream );
17041748 return CUDA_SUCCESS ;
@@ -1803,7 +1847,7 @@ DevicePtrHandle deviceptr_import_ipc(const MemoryPoolHandle& h_pool, const void*
18031847 GILReleaseGuard gil;
18041848 const DeallocationStream& stream = b->deallocation ;
18051849 cleanup_in_context (
1806- deallocation_context (stream), " cuMemFreeAsync" ,
1850+ deallocation_context (stream), " cuMemFreeAsync" , handle_bits (b-> resource ),
18071851 [&]() noexcept {
18081852 return p_cuMemFreeAsync (
18091853 b->resource , as_cu (stream.h_stream ));
@@ -1834,7 +1878,7 @@ DevicePtrHandle deviceptr_import_ipc(const MemoryPoolHandle& h_pool, const void*
18341878 GILReleaseGuard gil;
18351879 const DeallocationStream& stream = b->deallocation ;
18361880 cleanup_in_context (
1837- deallocation_context (stream), " cuMemFreeAsync" ,
1881+ deallocation_context (stream), " cuMemFreeAsync" , handle_bits (b-> resource ),
18381882 [&]() noexcept {
18391883 return p_cuMemFreeAsync (
18401884 b->resource , as_cu (stream.h_stream ));
@@ -3258,7 +3302,7 @@ TexObjectHandle make_tex_object_handle(const CUDA_RESOURCE_DESC& res,
32583302 new TexObjectBox{TexObjectValue{obj}, std::move (h_backing), h_context},
32593303 [](const TexObjectBox* b) {
32603304 GILReleaseGuard gil;
3261- cleanup_in_context (b->h_context , " cuTexObjectDestroy" , [&]() noexcept {
3305+ cleanup_in_context (b->h_context , " cuTexObjectDestroy" , handle_bits (b-> resource . raw ), [&]() noexcept {
32623306 return p_cuTexObjectDestroy (b->resource .raw );
32633307 });
32643308 delete b;
@@ -3306,7 +3350,7 @@ SurfObjectHandle create_surf_object_handle(const ContextHandle& h_context,
33063350 new SurfObjectBox{SurfObjectValue{obj}, h_backing, h_context},
33073351 [](const SurfObjectBox* b) {
33083352 GILReleaseGuard gil;
3309- cleanup_in_context (b->h_context , " cuSurfObjectDestroy" , [&]() noexcept {
3353+ cleanup_in_context (b->h_context , " cuSurfObjectDestroy" , handle_bits (b-> resource . raw ), [&]() noexcept {
33103354 return p_cuSurfObjectDestroy (b->resource .raw );
33113355 });
33123356 delete b;
0 commit comments