Refactor memory resource ownership and type safety - #985
Conversation
Signed-off-by: niranda perera <niranda.perera@gmail.com>
Signed-off-by: niranda perera <niranda.perera@gmail.com>
Signed-off-by: niranda perera <niranda.perera@gmail.com>
Signed-off-by: niranda perera <niranda.perera@gmail.com>
Signed-off-by: niranda perera <niranda.perera@gmail.com>
Signed-off-by: niranda perera <niranda.perera@gmail.com>
Signed-off-by: niranda perera <niranda.perera@gmail.com>
madsbk
left a comment
There was a problem hiding this comment.
Thanks @nirandaperera, looks good. I only have one change request
| auto* ptr = allocate(cuda::stream_ref{cudaStream_t{nullptr}}, bytes, alignment); | ||
| RAPIDSMPF_CUDA_TRY(cudaStreamSynchronize(cudaStream_t{nullptr})); |
There was a problem hiding this comment.
I think we should use an internal stream (stored in the adaptor) rather than the null stream.
| * holds the pool directly inside the shared control block — no extra heap allocation for | ||
| * the pool itself. Copies share the same underlying pool and memory statistics. |
There was a problem hiding this comment.
This aspect seems like it is just not worth it.
Co-authored-by: Mads R. B. Kristensen <madsbk@gmail.com>
Signed-off-by: niranda perera <niranda.perera@gmail.com>
…mpf into host_mrs_to_cccl_mr
Signed-off-by: niranda perera <niranda.perera@gmail.com>
Signed-off-by: niranda perera <niranda.perera@gmail.com>
Signed-off-by: niranda perera <niranda.perera@gmail.com>
…mpf into host_mrs_to_cccl_mr
Co-authored-by: Lawrence Mitchell <wence@gmx.li>
Signed-off-by: niranda perera <niranda.perera@gmail.com>
|
@wence- Could you please go through this again? I am inclined to keep the templates in the |
wence-
left a comment
There was a problem hiding this comment.
I have some minor questions/suggestions, but I don't think they are blocking. I am just about convinced that the added complexity of templating here is worth it.
| class PinnedMemoryResource final | ||
| : public cuda::mr::shared_resource< | ||
| detail::RmmResourceAdaptorImpl<cuda::pinned_memory_pool>> { | ||
| using shared_base = cuda::mr::shared_resource< | ||
| detail::RmmResourceAdaptorImpl<cuda::pinned_memory_pool>>; |
There was a problem hiding this comment.
OK, so the only question I now have here is that effectively this bakes that allocation with the pinned pool always uses the RMMResourceAdaptor. I suppose we're going to be relying on that, and it's not different to the device MR which also has to have this wrapping?
There was a problem hiding this comment.
Yes, I think so. We can provide a mem-tracking-enabled device memory pool inside rapidsmpf that bakes in cuda mempool into the adapter. Do you think we should provide that?
| auto tracker = *this; // shared ownership — same underlying pool | ||
| return [tracker, limit]() { return limit - tracker.get().current_allocated(); }; | ||
| } |
There was a problem hiding this comment.
What does dereferencing this buy us here over return [this, limit]() {...};?
There was a problem hiding this comment.
this would simply pass by ptr, isnt it? We want to increment the refcount before passing it in IINM.
Signed-off-by: niranda perera <niranda.perera@gmail.com>
|
/merge |
## Remove memory resources from `Statistics` construction `Statistics` held `RmmResourceAdaptor` and `PinnedMemoryResource` as instance fields, tying resource lifetime to the stats object and preventing a single `Statistics` instance from being used with different resources at report time. ### Solution - Remove `mr_` / `pinned_mr_` fields and the `Statistics(RmmResourceAdaptor, ...)` constructor; `report()` and `create_memory_recorder()` now accept explicit `std::optional<any_device_resource>` / `std::optional<any_host_device_resource>` parameters — callers supply resources at use time. - Add `try_pinned_mr()` returning `std::optional<any_host_device_resource>` non-throwingly; correct `pinned_mr()` return type to `host_device_async_resource_ref`. - Add `memory/resource_types.hpp` with `any_device_resource` / `any_host_device_resource` aliases; update `RAPIDSMPF_MEMORY_PROFILE` macro to require an explicit `mr` argument. Closes #979 Depends on rapidsai/rapids-cmake#1008 and #985 Authors: - Niranda Perera (https://github.com/nirandaperera) Approvers: - Lawrence Mitchell (https://github.com/wence-) URL: #1003
RmmResourceAdaptorImpl— templated on primary and fallback resourcesPrimaryMRandFallbackMRtemplate parameters (both constrained tocuda::mr::resource_with<device_accessible>;FallbackMRdefaults toany_resource<device_accessible>)explicit RmmResourceAdaptorImpl(std::in_place_t, Args&&...)constructor tosupport in-place construction of non-movable types (e.g.
cuda::pinned_memory_pool)PinnedMemoryResource— eliminate redundant heap allocationcuda::mr::shared_resource<RmmResourceAdaptorImpl<cuda::pinned_memory_pool>>directly, storing the pool inside the shared control block rather than through a
separate
RmmResourceAdaptormemberHostMemoryResource, making it a standalone value-semantic typeallocate/deallocateoverloads so call sites omitting thealignment argument continue to work
Value-semantic factory methods
PinnedMemoryResource::make_if_availableandfrom_optionsnow returnstd::optional<PinnedMemoryResource>instead ofstd::shared_ptr<PinnedMemoryResource>PinnedMemoryResource::Disabledchanged fromnullptrtostd::nullopt_t— existing== Disabledcomparisons remain valid viaoptional::operator==(nullopt_t)BufferResourceandStatisticsmembers/parameters updated fromshared_ptrtostd::optionalBufferResource::host_mr_reverted to the concreteHostMemoryResourcetypeCython bindings
PinnedMemoryResource._handlechanged fromshared_ptr[cpp_PinnedMemoryResource]tooptional[cpp_PinnedMemoryResource], matching the C++ value semantics throughoutbuffer_resource.pyxandstatistics.pyxupdated accordinglyCloses #978