Add RAII destructors to CUDA driver objects#106
Open
badnikhil wants to merge 1 commit into
Open
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Cleanup of driver objects is fully manual (
Buffer.release(),Context.detach(),Program.unload()), andQueue/Eventhave no cleanup path at all - a forgottenrelease()leaks device memory, streams, or modules.Changes
Buffer,Queue,Context,ProgramandEventfree their CUDA handle when the last owner goes out of scope. Copies share a malloc'd atomic refcount via postblit (refcounting over@disable this(this)because the public API copies these structs by value —Queue.enqueue'sCallstruct,Context.pop/current, kernel args).release()/unload()/detach()now drop one owner and only the last owner frees — releasing one copy no longer invalidates surviving copies, and double release or release-then-destructor is a no-op.Context.pop/currentandUnifiedBuffer.asBufferare non-owning views (null refcount, never destroy).Eventgains acuEventCreateconstructor so it participates in RAII.Tests
New hardware tests in
tests/main.d(RTX 2050, sm_86, all passing with the existing suite): refcount survives copies; last-owner-frees with no double free; release-then-destructor safety; non-owning views;cuMemGetInfo-verified checks that a scoped 128 MiB Buffer's destructor really returns device memory and copies free exactly once; 1000-iteration Queue+Event create/destroy churn.Note: the memory-verification tests use
cuMemGetInfo, added to bindbc-cuda