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.
get_memory_resource#
cuda::mr::get_memory_resource is a customization point object that retrieves a memory resource from an object or
execution environment.
cuda::mr::memory_resource_base<Derived> is a CRTP helper that makes a resource queryable by
cuda::mr::get_memory_resource when the resource is used as part of a composed execution environment.
It supports three cases:
If the object is itself a synchronous resource, it returns that object.
If the object has a
get_memory_resource()member, it calls that member.Otherwise, if an execution environment supports
query(cuda::mr::get_memory_resource), it calls that query.
#include <cuda/memory_resource>
#include <cuda/stream>
#include <cuda/std/type_traits>
template <class Env>
void* allocate_from_env(Env& env, cuda::stream_ref stream, std::size_t size, std::size_t align) {
auto&& resource = cuda::mr::get_memory_resource(env);
using resource_type = cuda::std::remove_reference_t<decltype(resource)>;
if constexpr (cuda::mr::resource<resource_type>) {
return resource.allocate(stream, size, align);
} else {
return resource.allocate_sync(size, align);
}
}
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);
}