Repository navigation
Migrate RMM usage to CCCL MR design - #940
Conversation
|
Auto-sync is disabled for draft pull requests in this repository. Workflows must be run manually. Contributors can view more details about this message here. |
| * @return A owning memory resource, which must be kept alive. | ||
| * @return An owning memory resource, which must be kept alive. | ||
| */ | ||
| [[nodiscard]] inline std::shared_ptr<rapidsmpf::RmmResourceAdaptor> |
There was a problem hiding this comment.
this could be a cuda::mr::shared_resource isnt it?
| class RmmResourceAdaptor | ||
| : public cuda::mr::shared_resource<detail::RmmResourceAdaptorImpl> { |
There was a problem hiding this comment.
Do all rmm adapters follow this pattern now?
As opposed to, move RmmResourceAdaptorImpl into RmmResourceAdaptor, and when ever we want to use the adapter, do,
auto adapter = cuda::mr::make_shared_resource<RmmResourceAdaptor>(mr);There was a problem hiding this comment.
Really we have two types of memory resources:
- Stateless: trivially copyable, movable, etc. Example:
cuda_memory_resource{}. - Stateful: adaptors and/or nontrivial base resources. Examples: any adaptor (statistics, logging, pool) or
cuda_async_memory_resourcewhich holds a CUDA driver pool object.
So yes, all adaptors use this pattern, but it's not just adaptors, it's anything that has shared state. The way we handle stateful resources is PIMPL-like, with some nuances.
AdaptorImplis a private/detail class that holds the upstreamany_resourceand/or adaptor state as members,Adaptoris the public API, it usescuda::mr::shared_resourcewhich forwards allocation methods to theImplclass.
With that split, copying stateful resources is cheap (just the cost of copying a shared_ptr). Crucially, we don't actually use shared_ptr, we use cuda::mr::shared_resource<Impl> and that does the magic for us. It has its own shared-pointer-like implementation.
There was a problem hiding this comment.
Thanks @bdice.
My only concern about this is, now Adapter is backed by a shared-ptr like state. Callers need to be explicitly aware of it to ensure that its moved everywhere possible (to prevent unnecessary atomic increments). What if we made resources non-copyable and non-moveable, and give a CRTP like std::enable_shared_from_this<SomeResource> that creates a shared_resource<SomeResource>, which can be used with any_resource<Properties>? This way, nothing is stashed in detail ns IMO.
There was a problem hiding this comment.
At this point I think the CCCL memory resource design is fixed. You're welcome to ask the CCCL team about non-copyable / non-movable types, or you can create your own wrapper.
If you want to avoid refcount increments, pass around ref types. Creating a ref should not increment the refcount of a shared resource. If that resource is stored by a container like rmm::device_buffer, that will refcount only when the ref is "reified" into an any_resource (when the number of owners grows).
| // Manual comparison of optionals to avoid recursive constraint satisfaction in | ||
| // CCCL 3.2. std::optional::operator== triggers infinite concept checking when the | ||
| // wrapped type (rmm::device_async_resource_ref) inherits from CCCL's concept-based | ||
| // resource_ref. | ||
| // TODO: Revert this after the RMM resource ref types are replaced with | ||
| // plain cuda::mr ref types. This depends on | ||
| // https://github.com/rapidsai/rmm/issues/2011. |
There was a problem hiding this comment.
is this comment still valid?
There was a problem hiding this comment.
Good catch, this can be removed now. I'm pretty sure this can be reduced to:
bool RmmResourceAdaptorImpl::operator==(
RmmResourceAdaptorImpl const& other
) const noexcept {
return this == std::addressof(other);
}There was a problem hiding this comment.
Although wait, wouldn't that say two adaptorimpl resources are only the same if they are the same pointer? Perhaps that is what we want.
There was a problem hiding this comment.
Seems like both the following still trigger infinite concept checking,
return primary_mr_ == other.primary_mr_ && fallback_mr_ == other.fallback_mr_;
// OR
return primary_mr_ == other.primary_mr_ && get_fallback_resource() == other.get_fallback_resource();| RmmResourceAdaptor mr(primary_mr); | ||
|
|
||
| EXPECT_THROW(mr.allocate_sync(8_MiB), rmm::out_of_memory); | ||
| EXPECT_THROW((void)mr.allocate_sync(8_MiB), rmm::out_of_memory); |
There was a problem hiding this comment.
nit
| EXPECT_THROW((void)mr.allocate_sync(8_MiB), rmm::out_of_memory); | |
| EXPECT_THROW(std::ignore = mr.allocate_sync(8_MiB), rmm::out_of_memory); |
There was a problem hiding this comment.
Nit2: Why is this marked resolved? It looks like std::ignore = is more appropriate.
There was a problem hiding this comment.
@pentschev mainly due to this #940 (comment)
But then again, seems like everyone has agreed to use std::ignore. Let me add it to #977
0b13b4b to
1d28b47
Compare
There was a problem hiding this comment.
rapidsmpf should be pinned to a specific commit of cuCascade (the tip of PR 98) until NVIDIA/cuCascade#98 is merged.
1d28b47 to
fe393cb
Compare
bdice
left a comment
There was a problem hiding this comment.
@nirandaperera Thanks for your help with this so far. I would like to ask if you can help with the remaining refactoring steps here. I will handle the cuCascade pinning soon (just need to get cuDF artifacts built and verify that the cuCascade tests pass, then I will pin to that commit).
| create_context(ProgramOptions& arguments, RmmResourceAdaptor* mr) { | ||
| rmm::mr::set_current_device_resource(mr); | ||
| rmm::mr::set_current_device_resource_ref(mr); | ||
| rmm::mr::set_current_device_resource(*mr); |
There was a problem hiding this comment.
We may be able to pass RmmResourceAdaptor by value rather than by pointer here.
| if (args.device_mem_limit_mb >= 0) { | ||
| memory_available[rapidsmpf::MemoryType::DEVICE] = rapidsmpf::LimitAvailableMemory{ | ||
| stat_enabled_mr.get(), args.device_mem_limit_mb << 20 | ||
| &stat_enabled_mr, args.device_mem_limit_mb << 20 |
There was a problem hiding this comment.
We may be able to pass by value rather than by pointer here.
| @@ -151,7 +151,7 @@ void BM_DeviceToHostCopyInclAlloc(benchmark::State& state) { | |||
| auto device_mr = std::make_unique<rmm::mr::cuda_memory_resource>(); | |||
There was a problem hiding this comment.
This probably doesn't need to be owned by unique_ptr anymore.
(Pattern repeats.)
There was a problem hiding this comment.
Let's do that in a followup, can you open an issue?
| class RmmResourceAdaptor | ||
| : public cuda::mr::shared_resource<detail::RmmResourceAdaptorImpl> { |
There was a problem hiding this comment.
Really we have two types of memory resources:
- Stateless: trivially copyable, movable, etc. Example:
cuda_memory_resource{}. - Stateful: adaptors and/or nontrivial base resources. Examples: any adaptor (statistics, logging, pool) or
cuda_async_memory_resourcewhich holds a CUDA driver pool object.
So yes, all adaptors use this pattern, but it's not just adaptors, it's anything that has shared state. The way we handle stateful resources is PIMPL-like, with some nuances.
AdaptorImplis a private/detail class that holds the upstreamany_resourceand/or adaptor state as members,Adaptoris the public API, it usescuda::mr::shared_resourcewhich forwards allocation methods to theImplclass.
With that split, copying stateful resources is cheap (just the cost of copying a shared_ptr). Crucially, we don't actually use shared_ptr, we use cuda::mr::shared_resource<Impl> and that does the magic for us. It has its own shared-pointer-like implementation.
| // Manual comparison of optionals to avoid recursive constraint satisfaction in | ||
| // CCCL 3.2. std::optional::operator== triggers infinite concept checking when the | ||
| // wrapped type (rmm::device_async_resource_ref) inherits from CCCL's concept-based | ||
| // resource_ref. | ||
| // TODO: Revert this after the RMM resource ref types are replaced with | ||
| // plain cuda::mr ref types. This depends on | ||
| // https://github.com/rapidsai/rmm/issues/2011. |
There was a problem hiding this comment.
Good catch, this can be removed now. I'm pretty sure this can be reduced to:
bool RmmResourceAdaptorImpl::operator==(
RmmResourceAdaptorImpl const& other
) const noexcept {
return this == std::addressof(other);
}| // --------------------------------------------------------------------------- | ||
| namespace detail { | ||
|
|
||
| RmmResourceAdaptorImpl::RmmResourceAdaptorImpl( |
There was a problem hiding this comment.
RMM uses a separate .cpp file for the Impl classes to avoid accidental overlap between private and public APIs. Either way is fine.
| # need to keep this alive here. | ||
| # Stored for the Python device_mr/pinned_mr property accessors. | ||
| # The C++ BufferResource owns the resource via any_resource. | ||
| self._device_mr = device_mr |
There was a problem hiding this comment.
Previously these members were for keeping the MR alive. However, now it seems like they're used for the property accessors. I updated the comment.
a8012b7 to
a871b98
Compare
1c157af to
1b8e20a
Compare
|
/ok to test |
@nirandaperera, there was an error processing your request: See the following link for more information: https://docs.gha-runners.nvidia.com/cpr/e/1/ |
|
/ok to test 1f99825 |
|
/ok to test 300eefb |
|
/ok to test |
@bdice, there was an error processing your request: See the following link for more information: https://docs.gha-runners.nvidia.com/cpr/e/1/ |
RMM 26.06 removes rmm::mr::device_memory_resource base class and adopts CCCL-native memory resource concepts. This migrates rapidsmpf accordingly: - RmmResourceAdaptor rewritten as thin shell inheriting cuda::mr::shared_resource<detail::RmmResourceAdaptorImpl>, with all mutable state in the impl class for copyable shared ownership. - Replace device_memory_resource* with rmm::device_async_resource_ref for non-owning references and cuda::mr::any_resource for owning storage. - Remove rmm::mr::owning_wrapper usage. - pool_memory_resource is no longer a template; upstream passed as device_async_resource_ref. - Replace set_current_device_resource(ptr) with set_current_device_resource_ref(ref). - Update test resources to satisfy CCCL resource concept (allocate_sync, deallocate_sync, operator==, get_property). - Update Cython bindings to use device_async_resource_ref instead of device_memory_resource. - Point get_cucascade.cmake to local cuCascade source directory.
…BufferResource Replace non-owning device_async_resource_ref with owning cuda::mr::any_resource<device_accessible> for constructor parameters and member storage in RmmResourceAdaptor, RmmResourceAdaptorImpl, and BufferResource. Getters still return device_async_resource_ref via const_cast, matching the pattern in RMM's prefetch_resource_adaptor. Forward the alignment parameter in RmmResourceAdaptorImpl::allocate and deallocate to fix CCCL deprecation warnings. Update Cython declarations to use any_resource and RMM's make_any_device_resource/make_device_async_resource_ref helpers, replacing the custom make_rapidsmpf_resource_ref inline function.
throw_at_limit_resource is split into a non-copyable impl holding the mutable allocs set, and a shared_resource<impl> wrapper. When any_resource copies the resource, copies now share the same underlying state via ref counting, so test assertions on the original object correctly observe allocations made through the adaptor's internal copy.
f39f7a2 to
f81becf
Compare
| @@ -151,7 +151,7 @@ void BM_DeviceToHostCopyInclAlloc(benchmark::State& state) { | |||
| auto device_mr = std::make_unique<rmm::mr::cuda_memory_resource>(); | |||
There was a problem hiding this comment.
Let's do that in a followup, can you open an issue?
| // Manual comparison of optionals to avoid recursive constraint satisfaction in | ||
| // CCCL 3.2. std::optional::operator== triggers infinite concept checking when the | ||
| // wrapped type (rmm::device_async_resource_ref) inherits from CCCL's concept-based | ||
| // resource_ref. | ||
| // TODO: Revert this after the RMM resource ref types are replaced with | ||
| // plain cuda::mr ref types. This depends on | ||
| // https://github.com/rapidsai/rmm/issues/2011. |
Co-authored-by: Niranda Perera <[email protected]> Co-authored-by: Lawrence Mitchell <[email protected]>
bdice
left a comment
There was a problem hiding this comment.
Thanks for the fixes @nirandaperera! I think this is fine to merge when the rapidsmpf team is ready. @wence- requested changes but it may be expedient to merge this to unblock builds rather than waiting for further review -- there are several leftover tasks, so any further changes that are needed could be combined with those.
| class RmmResourceAdaptor | ||
| : public cuda::mr::shared_resource<detail::RmmResourceAdaptorImpl> { |
There was a problem hiding this comment.
At this point I think the CCCL memory resource design is fixed. You're welcome to ask the CCCL team about non-copyable / non-movable types, or you can create your own wrapper.
If you want to avoid refcount increments, pass around ref types. Creating a ref should not increment the refcount of a shared resource. If that resource is stored by a container like rmm::device_buffer, that will refcount only when the ref is "reified" into an any_resource (when the number of owners grows).
pentschev
left a comment
There was a problem hiding this comment.
Reemphasized on nit (totally fine to slip), and giving a loose approval to be understood as "nothing looks strikingly wrong", since I haven't really followed much of the CCCL discussions and migration so far.
| RmmResourceAdaptor mr(primary_mr); | ||
|
|
||
| EXPECT_THROW(mr.allocate_sync(8_MiB), rmm::out_of_memory); | ||
| EXPECT_THROW((void)mr.allocate_sync(8_MiB), rmm::out_of_memory); |
There was a problem hiding this comment.
Nit2: Why is this marked resolved? It looks like std::ignore = is more appropriate.
|
/merge |
Summary
RmmResourceAdaptoras a thin shell inheritingcuda::mr::shared_resource<detail::RmmResourceAdaptorImpl>, with all mutable state in the impl class for copyable shared ownership.device_memory_resource*withrmm::device_async_resource_reffor non-owning references andcuda::mr::any_resourcefor owning storage.rmm::mr::owning_wrapperusage (removed in RMM 26.06).pool_memory_resourceusage (no longer a template; upstream passed asdevice_async_resource_ref).set_current_device_resource(ptr)withset_current_device_resource_ref(ref).allocate_sync,deallocate_sync,operator==,get_property)..pxd/.pyx) to usedevice_async_resource_refinstead ofdevice_memory_resource.get_cucascade.cmaketo local cuCascade source directory (companion cuCascade PR: Migrate RMM usage to CCCL MR design NVIDIA/cuCascade#98).Depends on rapidsai/rmm#2361.
Depends on rapidsai/ucxx#636.
Depends on NVIDIA/cudf#22008.
Notes
get_cucascade.cmakechange to useSOURCE_DIRis a development convenience and should be updated to point to the merged cuCascade commit before this PR is finalized.