Simplify HostBuffer memory ownership#1013
Simplify HostBuffer memory ownership#1013nirandaperera wants to merge 4 commits intorapidsai:mainfrom
HostBuffer memory ownership#1013Conversation
Signed-off-by: niranda perera <niranda.perera@gmail.com>
| : stream_{stream}, mr_{std::move(mr)} { | ||
| : stream_{stream} { | ||
| if (size > 0) { | ||
| auto owned_mr = cuda::mr::any_resource<cuda::mr::host_accessible>{mr}; |
There was a problem hiding this comment.
I don’t think owned_mr actually owns the memory resource. AFAIK, cuda::mr::any_resource<P> only owns the rmm::host_async_resource_ref mr, which is itself a non-owning reference. So owned_mr ends up owning a copy of a non-owning reference, not the underlying memory resource?
There was a problem hiding this comment.
This is a reasonable thought but incorrect. any_resource(resource_ref) magically converts the ref into an owning type.
There was a problem hiding this comment.
Thanks @wence-, good to know! @nirandaperera could you add a comment noting this?
There was a problem hiding this comment.
Yeah, any_resource takes in a copy of the underlying resource in the ref. Idea is to increment the refcount if that resource is shared.
madsbk
left a comment
There was a problem hiding this comment.
@nirandaperera, it would be good to add a test that constructs a HostBuffer from a transient memory resource, for example a PinnedMemoryResource that goes out of scope before the buffer is deallocated.
HostBuffer memory ownershipHostBuffer memory ownership
Signed-off-by: niranda perera <niranda.perera@gmail.com>
|
@madsbk done |
|
/merge |
HostBuffertracked two redundant deallocation paths (mr_+owned_storage_), wheremr_was stored unconditionally but never used whenowned_storage_was present — and as a non-owning ref it could silently outlive aPinnedMemoryResource.Solution
std::function<void(rmm::cuda_stream_view)> deallocate_fn_, invoked with the live stream at deallocation time to respectset_stream().mrincuda::mr::any_resourcebefore capturing it — deep-copying the resource and incrementing its refcount if it is aPinnedMemoryResource.mrparameter fromfrom_owned_vectorandfrom_rmm_device_buffer(it was always unused for deallocation).OwnedStorageDeleterpublic type alias.