Skip to content

Migrate RMM usage to CCCL MR design - #940

Merged
rapids-bot[bot] merged 14 commits into
rapidsai:mainfrom
bdice:rmm-cccl-migration
Apr 21, 2026
Merged

rapids-bot[bot] merged 14 commits into
rapidsai:mainfrom
bdice:rmm-cccl-migration

Conversation

@bdice

@bdice bdice commented Apr 3, 2026 •

Copy link
Copy Markdown
Contributor

Summary

  • Rewrite RmmResourceAdaptor as a 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 (removed in RMM 26.06).
  • Update pool_memory_resource usage (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 (.pxd/.pyx) to use device_async_resource_ref instead of device_memory_resource.
  • Point get_cucascade.cmake to 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

@copy-pr-bot

copy-pr-bot Bot commented Apr 3, 2026

Copy link
Copy Markdown

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.

Comment thread cpp/benchmarks/utils/rmm_stack.hpp Outdated
* @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>

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this could be a cuda::mr::shared_resource isnt it?

Comment on lines +31 to +32
class RmmResourceAdaptor
: public cuda::mr::shared_resource<detail::RmmResourceAdaptorImpl> {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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);

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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_resource which 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.

  • AdaptorImpl is a private/detail class that holds the upstream any_resource and/or adaptor state as members,
  • Adaptor is the public API, it uses cuda::mr::shared_resource which forwards allocation methods to the Impl class.

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.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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).

Comment thread cpp/src/rmm_resource_adaptor.cpp Outdated
Comment on lines +31 to +37
// 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.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this comment still valid?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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);
}

See this example pattern.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we do this please?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll fix this

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit

Suggested change
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);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit2: Why is this marked resolved? It looks like std::ignore = is more appropriate.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@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

Comment thread cpp/tests/test_rmm_resource_adaptor.cpp Resolved
@nirandaperera nirandaperera added breaking Introduces a breaking change improvement Improves an existing functionality labels Apr 16, 2026
@bdice
bdice force-pushed the rmm-cccl-migration branch from 0b13b4b to 1d28b47 Compare April 17, 2026 05:26

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

rapidsmpf should be pinned to a specific commit of cuCascade (the tip of PR 98) until NVIDIA/cuCascade#98 is merged.

@bdice
bdice force-pushed the rmm-cccl-migration branch from 1d28b47 to fe393cb Compare April 17, 2026 06:31

@bdice bdice left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@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).

Comment thread cpp/benchmarks/streaming/ndsh/utils.cpp Outdated
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);

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We may be able to pass by value rather than by pointer here.

Comment thread cpp/benchmarks/utils/rmm_stack.hpp Resolved
@@ -151,7 +151,7 @@ void BM_DeviceToHostCopyInclAlloc(benchmark::State& state) {
auto device_mr = std::make_unique<rmm::mr::cuda_memory_resource>();

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This probably doesn't need to be owned by unique_ptr anymore.

(Pattern repeats.)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's do that in a followup, can you open an issue?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment on lines +31 to +32
class RmmResourceAdaptor
: public cuda::mr::shared_resource<detail::RmmResourceAdaptorImpl> {

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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_resource which 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.

  • AdaptorImpl is a private/detail class that holds the upstream any_resource and/or adaptor state as members,
  • Adaptor is the public API, it uses cuda::mr::shared_resource which forwards allocation methods to the Impl class.

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.

Comment thread cpp/src/rmm_resource_adaptor.cpp Outdated
Comment on lines +31 to +37
// 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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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);
}

See this example pattern.

Comment thread cpp/src/rmm_resource_adaptor.cpp Outdated
// ---------------------------------------------------------------------------
namespace detail {

RmmResourceAdaptorImpl::RmmResourceAdaptorImpl(

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

RMM uses a separate .cpp file for the Impl classes to avoid accidental overlap between private and public APIs. Either way is fine.

Comment thread cpp/tests/test_rmm_resource_adaptor.cpp Resolved
# 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

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread cpp/tests/test_rmm_resource_adaptor.cpp Resolved
@bdice
bdice force-pushed the rmm-cccl-migration branch 2 times, most recently from a8012b7 to a871b98 Compare April 17, 2026 07:09
@bdice
bdice marked this pull request as ready for review April 17, 2026 07:13
@bdice
bdice requested review from a team as code owners April 17, 2026 07:13
@bdice
bdice requested a review from msarahan April 17, 2026 07:13
@bdice
bdice force-pushed the rmm-cccl-migration branch 6 times, most recently from 1c157af to 1b8e20a Compare April 18, 2026 15:52
@copy-pr-bot

copy-pr-bot Bot commented Apr 20, 2026

Copy link
Copy Markdown

This pull request requires additional validation before any workflows can run on NVIDIA's runners.

Pull request vetters can view their responsibilities here.

Contributors can view more details about this message here.

@nirandaperera

Copy link
Copy Markdown
Contributor

/ok to test

@copy-pr-bot

copy-pr-bot Bot commented Apr 20, 2026

Copy link
Copy Markdown

/ok to test

@nirandaperera, there was an error processing your request: E1

See the following link for more information: https://docs.gha-runners.nvidia.com/cpr/e/1/

@nirandaperera

Copy link
Copy Markdown
Contributor

/ok to test 1f99825

@nirandaperera

Copy link
Copy Markdown
Contributor

/ok to test 300eefb

@bdice

bdice commented Apr 21, 2026

Copy link
Copy Markdown
Contributor Author

/ok to test

@copy-pr-bot

copy-pr-bot Bot commented Apr 21, 2026

Copy link
Copy Markdown

/ok to test

@bdice, there was an error processing your request: E1

See the following link for more information: https://docs.gha-runners.nvidia.com/cpr/e/1/

bdice and others added 11 commits April 21, 2026 09:54
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.
@bdice
bdice force-pushed the rmm-cccl-migration branch from f39f7a2 to f81becf Compare April 21, 2026 15:00
wence-
wence- previously requested changes Apr 21, 2026
Comment thread cpp/benchmarks/streaming/ndsh/utils.cpp Resolved
Comment thread cpp/benchmarks/utils/rmm_utils.hpp Resolved
Comment thread cpp/benchmarks/utils/rmm_utils.hpp Resolved
@@ -151,7 +151,7 @@ void BM_DeviceToHostCopyInclAlloc(benchmark::State& state) {
auto device_mr = std::make_unique<rmm::mr::cuda_memory_resource>();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's do that in a followup, can you open an issue?

Comment thread cpp/src/rmm_resource_adaptor.cpp Outdated
Comment on lines +31 to +37
// 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.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we do this please?

Comment thread cpp/tests/test_rmm_resource_adaptor.cpp Resolved
Comment thread cpp/tests/test_rmm_resource_adaptor.cpp Resolved
Comment thread cpp/tests/test_rmm_resource_adaptor.cpp Resolved
Comment thread cpp/tests/test_rmm_resource_adaptor.cpp Resolved
Comment thread cpp/tests/test_rmm_resource_adaptor.cpp Resolved
bdice and others added 3 commits April 21, 2026 10:38
@nirandaperera
nirandaperera requested a review from wence- April 21, 2026 19:41

@bdice bdice left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment on lines +31 to +32
class RmmResourceAdaptor
: public cuda::mr::shared_resource<detail::RmmResourceAdaptorImpl> {

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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).

@nirandaperera
nirandaperera dismissed wence-’s stale review April 21, 2026 20:30

@wence- I am dismissing this review to unblock the merge. Please add any issues under #977
Let's take those up after the merge

@pentschev pentschev left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit2: Why is this marked resolved? It looks like std::ignore = is more appropriate.

@bdice
bdice removed request for a team and msarahan April 21, 2026 21:04
@nirandaperera

Copy link
Copy Markdown
Contributor

/merge

@rapids-bot
rapids-bot Bot merged commit 00a1c1b into rapidsai:main Apr 21, 2026
63 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

breaking Introduces a breaking change improvement Improves an existing functionality

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants