Repository navigation
Conversation
Change all RMM memory resource adaptors to store cuda::mr::any_resource instead of device_async_resource_ref. This ensures adaptors maintain ownership of their upstream resources through "reification" - converting resource references to owning copies. Problem: Adaptors previously stored non-owning references (device_async_resource_ref) to upstream resources, leading to undefined behavior when the upstream resource was destroyed before the adaptor. This was particularly dangerous when setting global device resources with local upstream resources. Solution: - Store cuda::mr::any_resource<cuda::mr::device_accessible> (owning) instead of device_async_resource_ref (non-owning) - Reify resource references in constructors to create owning copies - Mark members mutable to allow conversion to resource_ref in const methods - Add #include <cuda/memory_resource> to all adaptor headers Updated adaptors: - statistics_resource_adaptor - tracking_resource_adaptor - logging_resource_adaptor - failure_callback_resource_adaptor - limiting_resource_adaptor - prefetch_resource_adaptor - aligned_resource_adaptor - thread_safe_resource_adaptor - pool_memory_resource - arena_memory_resource - binning_memory_resource - fixed_size_memory_resource Benefits: - Safe by default: adaptors automatically own their upstream resources - Zero API breakage: existing code continues to work unchanged - Simpler lifetime management: users only manage adaptor lifetime - Minimal overhead: adds ~2-3 pointers per adaptor instance Tests: - Added comprehensive test suite (cccl_mr_adaptors_tests.cpp) - All 87 existing RMM C++ tests pass - Includes test validating the UB issue is resolved
|
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. |
Apply the same pattern used in memory resource adaptors - mark the any_resource member as mutable to allow conversion to resource_ref in const methods. This allows memory_resource() to remain const, maintaining const- correctness while still enabling the required conversion from any_resource to device_async_resource_ref. Changes: - Mark device_buffer::_mr as mutable - Restore const qualifier on device_buffer::memory_resource() - Restore const qualifier on device_uvector::memory_resource()
Apply the same pattern used in memory resource adaptors - mark the any_resource member as mutable to allow conversion to resource_ref in const methods. This allows memory_resource() to remain const, maintaining const- correctness while still enabling the required conversion from any_resource to device_async_resource_ref. Changes: - Mark device_buffer::_mr as mutable - Restore const qualifier on device_buffer::memory_resource() - Restore const qualifier on device_uvector::memory_resource()
47d7bf4 to
c8ee9ee
Compare
|
I changed this to be a breaking change, because it is massively breaking. Consider the (common) case where the "upstream" resource passed into the adaptor is stateful. Previously, we passed a Now we pass a The same goes for construction from a raw pointer. |
wence-
left a comment
There was a problem hiding this comment.
As noted, I think we need to think through how callers will now provide resources to adaptors.
| std::size_t alignment = rmm::CUDA_ALLOCATION_ALIGNMENT, | ||
| std::size_t alignment_threshold = default_alignment_threshold) | ||
| : upstream_{upstream}, | ||
| : upstream_{cuda::mr::any_resource<cuda::mr::device_accessible>{upstream}}, |
There was a problem hiding this comment.
Throughout:
| : upstream_{cuda::mr::any_resource<cuda::mr::device_accessible>{upstream}}, | |
| : upstream_{upstream}, |
An any_resource is constructable from a resource_ref (as the PR shows by constructing one). No need to construct twice.
| : upstream_{cuda::mr::any_resource<cuda::mr::device_accessible>{ | ||
| to_device_async_resource_ref_checked(upstream)}}, |
There was a problem hiding this comment.
| : upstream_{cuda::mr::any_resource<cuda::mr::device_accessible>{ | |
| to_device_async_resource_ref_checked(upstream)}}, | |
| : upstream_{to_device_async_resource_ref_checked(upstream)}, |
| return upstream_; | ||
| return device_async_resource_ref{upstream_}; |
There was a problem hiding this comment.
A cuda::mr::any_resource is assignable to a cuda::mr::resource_ref is that not the case here? That is, why does return upstream_; not convert?
| TYPED_TEST(AdaptorTest, ConstructFromAnyResource) | ||
| { | ||
| if constexpr (std::is_same_v<TypeParam, owning_wrapper>) { | ||
| GTEST_SKIP() << "owning_wrapper has different construction pattern"; | ||
| } else { | ||
| cuda::mr::any_resource<cuda::mr::device_accessible> cuda_mr = rmm::mr::cuda_memory_resource(); | ||
| rmm::device_async_resource_ref any_mr{cuda_mr}; | ||
|
|
||
| auto mr = this->make_adaptor(any_mr); | ||
|
|
||
| void* ptr = mr->allocate_sync(1024); | ||
| ASSERT_NE(ptr, nullptr); | ||
| EXPECT_NO_THROW(mr->deallocate_sync(ptr, 1024)); | ||
| } | ||
| } | ||
|
|
||
| TYPED_TEST(AdaptorTest, UpstreamResourceLifetime) | ||
| { | ||
| if constexpr (std::is_same_v<TypeParam, owning_wrapper>) { | ||
| GTEST_SKIP() << "owning_wrapper has different construction pattern"; | ||
| } else { | ||
| std::shared_ptr<TypeParam> mr; | ||
|
|
||
| { | ||
| cuda::mr::any_resource<cuda::mr::device_accessible> cuda_mr = rmm::mr::cuda_memory_resource(); | ||
| rmm::device_async_resource_ref any_mr{cuda_mr}; | ||
|
|
||
| mr = this->make_adaptor(any_mr); | ||
|
|
||
| // cuda_mr goes out of scope - adaptor owns a copy via reification | ||
| } | ||
|
|
||
| // Adaptor continues to work after upstream owner is destroyed | ||
| void* ptr = mr->allocate_sync(1024); | ||
| EXPECT_NE(ptr, nullptr); | ||
| EXPECT_NO_THROW(mr->deallocate_sync(ptr, 1024)); | ||
| } | ||
| } |
There was a problem hiding this comment.
Let us please add a test that demonstrates this works with a stateful adaptor.
|
#2246 explores a better design for this. I'll need to merge that and do similar changes for all the other stateful adaptors/resources. I'll close this PR. |
Fixes #2207
Summary
Changes all RMM memory resource adaptors to store
cuda::mr::any_resourceinstead ofdevice_async_resource_ref, ensuring adaptors maintain ownership of their upstream resources through reification.Changes
cuda::mr::any_resource<cuda::mr::device_accessible>(owning) instead ofdevice_async_resource_ref(non-owning)mutableto allow conversion toresource_refin const methods#include <cuda/memory_resource>to all adaptor headersKey Benefit
Users only need to manage the adaptor's lifetime, not the upstream resource's lifetime. This eliminates a common source of undefined behavior when using adaptors with temporary resources.
Testing
any_resourceand upstream resource lifetime