Skip to content

Store any_resource in device_buffer and device_uvector#2201

Merged
rapids-bot[bot] merged 6 commits intorapidsai:mainfrom
bdice:containers-any-resource
Jan 12, 2026
Merged

Store any_resource in device_buffer and device_uvector#2201
rapids-bot[bot] merged 6 commits intorapidsai:mainfrom
bdice:containers-any-resource

Conversation

@bdice
Copy link
Copy Markdown
Collaborator

@bdice bdice commented Jan 8, 2026

Summary

Part of #2011.

This PR adopts any_resource in device_buffer and device_uvector.

  • Change device_buffer::_mr from device_async_resource_ref to cuda::mr::any_resource<cuda::mr::device_accessible>
  • Make memory_resource() non-const on device_buffer and device_uvector (required because CCCL's resource_ref can only be constructed from non-const any_resource&)
  • Use std::move for _mr in move constructor and move assignment

This ensures device_buffer owns its memory resource, preventing use-after-free if the original resource is destroyed while the buffer still exists.

Stacked PR

This PR is stacked on top of #2200. To view only the changes in this PR:
bdice/rmm@device-ref-map-any-resource...containers-any-resource

Test plan

  • RMM C++ tests pass locally
  • cuDF C++ tests pass locally

bdice added 4 commits January 7, 2026 17:29
Change the per-device resource ref map to store
`cuda::mr::any_resource<device_accessible>` instead of
`device_async_resource_ref`. This provides ownership semantics -
the global map now owns the stored resources.

Key changes:
- `get_ref_map()` now returns map with `any_resource` values
- `set_per_device_resource_ref()` reifies incoming refs to `any_resource`
- `get_per_device_resource_ref()` returns refs from stored `any_resource`
- Added conversion operator and constructor in `cccl_async_resource_ref`
  to support conversion to/from `any_resource`

Note: Refs returned from get/set operations now point to the owned
copy inside `any_resource`, not the original object. Tests updated
to verify functional correctness rather than ref identity.
This reverts commit e791a1e.
This implements the next step of the CCCL 3.2 migration plan: adopting
any_resource in custom containers, starting with device_buffer.

Changes:
- Change device_buffer::_mr from device_async_resource_ref to
  cuda::mr::any_resource<cuda::mr::device_accessible>
- Make memory_resource() non-const on device_buffer and device_uvector
  (required because CCCL's resource_ref can only be constructed from
  non-const any_resource&)
- Use std::move for _mr in move constructor and move assignment

This ensures device_buffer owns its memory resource, preventing
use-after-free if the original resource is destroyed while the
buffer still exists.
@copy-pr-bot
Copy link
Copy Markdown

copy-pr-bot bot commented Jan 8, 2026

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.

@bdice bdice self-assigned this Jan 9, 2026
@bdice bdice marked this pull request as ready for review January 10, 2026 15:04
@bdice bdice requested a review from a team as a code owner January 10, 2026 15:04
@bdice bdice added feature request New feature or request breaking Breaking change labels Jan 10, 2026
* @briefreturn{The resource used to allocate and deallocate}
*/
[[nodiscard]] rmm::device_async_resource_ref memory_resource() const noexcept { return _mr; }
[[nodiscard]] rmm::device_async_resource_ref memory_resource() noexcept { return _mr; }
Copy link
Copy Markdown
Collaborator Author

@bdice bdice Jan 10, 2026

Choose a reason for hiding this comment

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

This is a breaking change. Should we make the resource mutable or make this method non-const? I discussed a bit with the CCCL team.

The CCCL buffer returns any_resource const& which requires a copy to be made before the resource (or ref) is usable (constructing a ref and making an allocation are not possible from a const resource). I would consider that approach but I don’t want to alter the API in that way just yet. It might be better to handle that as we transition to the CCCL buffer type.

Copy link
Copy Markdown
Collaborator Author

@bdice bdice Jan 13, 2026

Choose a reason for hiding this comment

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

After merging this, I decided to go the other way and use mutable in PR #2208 to keep this const. That seems more aligned with the spirit of how we want to use memory resources. See commit here: 47d7bf4.

EXPECT_EQ(mr, rmm::device_async_resource_ref{rmm::mr::detail::initial_resource()});
}

TEST(DefaultTest, SetCurrentDeviceResourceRefFromPointer)
Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

This is an extra test I wanted to add after #2200. Not buffer related.

Copy link
Copy Markdown
Member

@PointKernel PointKernel left a comment

Choose a reason for hiding this comment

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

I have one design question for clarification and learning purposes; otherwise, the PR looks good to me.

@bdice
Copy link
Copy Markdown
Collaborator Author

bdice commented Jan 12, 2026

/merge

@rapids-bot rapids-bot bot merged commit aac8e28 into rapidsai:main Jan 12, 2026
87 checks passed
bdice added a commit to bdice/rmm that referenced this pull request Jan 12, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

breaking Breaking change feature request New feature or request

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

3 participants