Resource utilities#
The cuda::mr memory resource system includes utilities that help manage resource lifetime and adapt synchronous
resources for stream-ordered usage. These utilities complement the type-erased wrappers in resource wrappers.
synchronous_resource_adapter#
cuda::mr::synchronous_resource_adapter adapts a synchronous memory resource to work as a stream-ordered resource.
If the underlying resource already supports stream-ordered allocation, it passes through the calls. Otherwise, it uses
synchronous allocation/deallocation with proper stream synchronization.
#include <cuda/memory_resource>
#include <cuda/stream>
void adapt_sync_resource(cuda::stream_ref stream) {
// Create a synchronous resource
auto sync_mr = cuda::mr::legacy_pinned_memory_resource{};
// Adapt it to work with streams
auto adapted = cuda::mr::synchronous_resource_adapter{
sync_mr
};
// Now can use with stream (will synchronize internally)
void* ptr = adapted.allocate(stream, 1024, 16);
// Use memory...
// Deallocate (will synchronize stream before deallocation)
adapted.deallocate(stream, ptr, 1024, 16);
}