From 0c5ed59ad0859b0c1b64a89d97ad32feb32127c5 Mon Sep 17 00:00:00 2001 From: Michael Schellenberger Costa Date: Wed, 28 Jan 2026 10:49:51 +0100 Subject: [PATCH 1/2] Ensure that our memory pools can implicitly convert to a `resource_ref` We want to pass them to APIs that take only a `resource_ref`. However, because those are not copyable we would need to convert them to a `memory_pool_ref` first. However, that also does not work, because the constructor for `resource_ref` does not accept a prvalue. To alleviate that create a conversion operator that does the right thing. --- .../cuda/__memory_pool/device_memory_pool.h | 6 +- .../cuda/__memory_pool/managed_memory_pool.h | 6 +- .../cuda/__memory_pool/pinned_memory_pool.h | 6 +- .../resources/device_memory_resource.cu | 6 +- .../memory_resource/resources/memory_pools.cu | 81 +++++++++++++++++++ 5 files changed, 93 insertions(+), 12 deletions(-) diff --git a/libcudacxx/include/cuda/__memory_pool/device_memory_pool.h b/libcudacxx/include/cuda/__memory_pool/device_memory_pool.h index a405e8a17fd..9016109a2ba 100644 --- a/libcudacxx/include/cuda/__memory_pool/device_memory_pool.h +++ b/libcudacxx/include/cuda/__memory_pool/device_memory_pool.h @@ -137,10 +137,10 @@ struct device_memory_pool : device_memory_pool_ref } //! @brief Returns a \c device_memory_pool_ref for this \c device_memory_pool. - //! The result is the same as if this object was cast to a \c device_memory_pool_ref. - [[nodiscard]] _CCCL_HOST_API device_memory_pool_ref as_ref() noexcept + //! We return by reference to ensure that we can subsequently convert to a resource_ref + [[nodiscard]] _CCCL_HOST_API device_memory_pool_ref& as_ref() noexcept { - return device_memory_pool_ref(__pool_); + return static_cast(*this); } device_memory_pool(const device_memory_pool&) = delete; diff --git a/libcudacxx/include/cuda/__memory_pool/managed_memory_pool.h b/libcudacxx/include/cuda/__memory_pool/managed_memory_pool.h index 56d6f320c9b..8790faef7f0 100644 --- a/libcudacxx/include/cuda/__memory_pool/managed_memory_pool.h +++ b/libcudacxx/include/cuda/__memory_pool/managed_memory_pool.h @@ -131,10 +131,10 @@ struct managed_memory_pool : managed_memory_pool_ref } //! @brief Returns a \c managed_memory_pool_ref for this \c managed_memory_pool. - //! The result is the same as if this object was cast to a \c managed_memory_pool_ref. - [[nodiscard]] _CCCL_HOST_API managed_memory_pool_ref as_ref() noexcept + //! We return by reference to ensure that we can subsequently convert to a resource_ref + [[nodiscard]] _CCCL_HOST_API managed_memory_pool_ref& as_ref() noexcept { - return managed_memory_pool_ref(__pool_); + return static_cast(*this); } managed_memory_pool(const managed_memory_pool&) = delete; diff --git a/libcudacxx/include/cuda/__memory_pool/pinned_memory_pool.h b/libcudacxx/include/cuda/__memory_pool/pinned_memory_pool.h index 9125383db5b..65bda4a7aec 100644 --- a/libcudacxx/include/cuda/__memory_pool/pinned_memory_pool.h +++ b/libcudacxx/include/cuda/__memory_pool/pinned_memory_pool.h @@ -168,10 +168,10 @@ struct pinned_memory_pool : pinned_memory_pool_ref } //! @brief Returns a \c pinned_memory_pool_ref for this \c pinned_memory_pool. - //! The result is the same as if this object was cast to a \c pinned_memory_pool_ref. - _CCCL_HOST_API pinned_memory_pool_ref as_ref() noexcept + //! We return by reference to ensure that we can subsequently convert to a resource_ref + _CCCL_HOST_API pinned_memory_pool_ref& as_ref() noexcept { - return pinned_memory_pool_ref(__pool_); + return static_cast(*this); } pinned_memory_pool(const pinned_memory_pool&) = delete; diff --git a/libcudacxx/test/libcudacxx/cuda/memory_resource/resources/device_memory_resource.cu b/libcudacxx/test/libcudacxx/cuda/memory_resource/resources/device_memory_resource.cu index 1cfd2727d81..08380d925d9 100644 --- a/libcudacxx/test/libcudacxx/cuda/memory_resource/resources/device_memory_resource.cu +++ b/libcudacxx/test/libcudacxx/cuda/memory_resource/resources/device_memory_resource.cu @@ -65,7 +65,7 @@ static bool ensure_export_handle(::cudaMemPool_t pool, const ::cudaMemAllocation return allocation_handle == ::cudaMemHandleTypeNone ? status == ::cudaErrorInvalidValue : status == ::cudaSuccess; } -C2H_CCCLRT_TEST("device_memory_resource construction", "[memory_resource]") +C2H_CCCLRT_TEST("device_memory_pool construction", "[memory_resource]") { int current_device = 0; cuda::__ensure_current_context guard{cuda::device_ref{current_device}}; @@ -217,7 +217,7 @@ static void ensure_device_ptr(void* ptr) CHECK(attributes.type == cudaMemoryTypeDevice); } -C2H_CCCLRT_TEST("device_memory_resource allocation", "[memory_resource]") +C2H_CCCLRT_TEST("device_memory_pool allocation", "[memory_resource]") { cudaStream_t raw_stream; { @@ -332,7 +332,7 @@ C2H_CCCLRT_TEST("device_memory_resource allocation", "[memory_resource]") } } -C2H_CCCLRT_TEST("device_memory_resource comparison", "[memory_resource]") +C2H_CCCLRT_TEST("device_memory_pool comparison", "[memory_resource]") { int current_device = 0; cuda::__ensure_current_context guard{cuda::device_ref{current_device}}; diff --git a/libcudacxx/test/libcudacxx/cuda/memory_resource/resources/memory_pools.cu b/libcudacxx/test/libcudacxx/cuda/memory_resource/resources/memory_pools.cu index ad15960e690..08e0620f5a3 100644 --- a/libcudacxx/test/libcudacxx/cuda/memory_resource/resources/memory_pools.cu +++ b/libcudacxx/test/libcudacxx/cuda/memory_resource/resources/memory_pools.cu @@ -670,3 +670,84 @@ C2H_CCCLRT_TEST("pinned_memory_pool with allocation handle", "[memory_resource]" // managed memory pool does not support allocation handles yet. #endif // !_CCCL_OS(WINDOWS) + +C2H_CCCLRT_TEST("device_memory_pool conversion to resource_ref", "[memory_resource]") +{ + int current_device = 0; + cuda::__ensure_current_context guard{cuda::device_ref{current_device}}; + + cuda::device_memory_pool pool{cuda::device_ref{0}}; + cuda::mr::resource_ref ref1 = pool.as_ref(); + + cuda::device_memory_pool_ref pool_ref = pool.as_ref(); + cuda::mr::resource_ref ref2 = pool_ref; + CHECK((ref1 == ref2)); +} + +#if _CCCL_CTK_AT_LEAST(13, 0) && !_CCCL_OS(WINDOWS) +C2H_CCCLRT_TEST("managed_memory_pool conversion to resource_ref", "[memory_resource]") +{ + int current_device = 0; + cuda::__ensure_current_context guard{cuda::device_ref{current_device}}; + + cuda::managed_memory_pool pool{}; + + { // host device accessible + cuda::mr::resource_ref ref1 = pool.as_ref(); + + cuda::managed_memory_pool_ref pool_ref = pool.as_ref(); + cuda::mr::resource_ref ref2 = pool_ref; + CHECK((ref1 == ref2)); + } + + { // host accessible + cuda::mr::resource_ref ref1 = pool.as_ref(); + + cuda::managed_memory_pool_ref pool_ref = pool.as_ref(); + cuda::mr::resource_ref ref2 = pool_ref; + CHECK((ref1 == ref2)); + } + + { // device accessible + cuda::mr::resource_ref ref1 = pool.as_ref(); + + cuda::managed_memory_pool_ref pool_ref = pool.as_ref(); + cuda::mr::resource_ref ref2 = pool_ref; + CHECK((ref1 == ref2)); + } +} +#endif // _CCCL_CTK_AT_LEAST(13, 0) && !_CCCL_OS(WINDOWS) + +#if _CCCL_CTK_AT_LEAST(12, 6) +C2H_CCCLRT_TEST("pinned_memory_pool conversion to resource_ref", "[memory_resource]") +{ + int current_device = 0; + cuda::__ensure_current_context guard{cuda::device_ref{current_device}}; + + cuda::pinned_memory_pool pool{}; + + { // host device accessible + cuda::mr::resource_ref ref1 = pool.as_ref(); + + cuda::pinned_memory_pool_ref pool_ref = pool.as_ref(); + cuda::mr::resource_ref ref2 = pool_ref; + CHECK((ref1 == ref2)); + } + + { // host accessible + cuda::mr::resource_ref ref1 = pool.as_ref(); + + cuda::pinned_memory_pool_ref pool_ref = pool.as_ref(); + cuda::mr::resource_ref ref2 = pool_ref; + CHECK((ref1 == ref2)); + } + + { // device accessible + cuda::mr::resource_ref ref1 = pool.as_ref(); + + cuda::pinned_memory_pool_ref pool_ref = pool.as_ref(); + cuda::mr::resource_ref ref2 = pool_ref; + CHECK((ref1 == ref2)); + } +} +#endif // _CCCL_CTK_AT_LEAST(12, 6) From ba3033fe7f58619c3b7f02448b36e39e31b8fb18 Mon Sep 17 00:00:00 2001 From: pciolkosz Date: Tue, 17 Feb 2026 16:28:17 -0800 Subject: [PATCH 2/2] Update libcudacxx/test/libcudacxx/cuda/memory_resource/resources/memory_pools.cu --- .../libcudacxx/cuda/memory_resource/resources/memory_pools.cu | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libcudacxx/test/libcudacxx/cuda/memory_resource/resources/memory_pools.cu b/libcudacxx/test/libcudacxx/cuda/memory_resource/resources/memory_pools.cu index 08e0620f5a3..7b31ec02cb4 100644 --- a/libcudacxx/test/libcudacxx/cuda/memory_resource/resources/memory_pools.cu +++ b/libcudacxx/test/libcudacxx/cuda/memory_resource/resources/memory_pools.cu @@ -724,7 +724,7 @@ C2H_CCCLRT_TEST("pinned_memory_pool conversion to resource_ref", "[memory_resour int current_device = 0; cuda::__ensure_current_context guard{cuda::device_ref{current_device}}; - cuda::pinned_memory_pool pool{}; + cuda::pinned_memory_pool pool{0}; { // host device accessible cuda::mr::resource_ref ref1 = pool.as_ref();