Mila 0.13.48
Deep Neural Network Library
Loading...
Searching...
No Matches
Mila::Dnn::Compute::MemoryResource Class Referenceabstractexport

Clean memory resource abstraction for device-specific memory allocation. More...

Inheritance diagram for Mila::Dnn::Compute::MemoryResource:
Collaboration diagram for Mila::Dnn::Compute::MemoryResource:

Public Member Functions

virtual ~MemoryResource ()=default
 Virtual destructor for proper cleanup of derived classes.

Static Public Attributes

static constexpr bool is_device_accessible = false
 Checks if the memory is accessible from device code.
static constexpr bool is_host_accessible = true
 Checks if the memory is accessible from host code.

Protected Member Functions

virtual void * do_allocate (std::size_t bytes, std::size_t alignment) override=0
 Allocates memory with specified size and alignment.
virtual void do_deallocate (void *ptr, std::size_t bytes, std::size_t alignment) override=0
 Deallocates previously allocated memory.
virtual bool do_is_equal (const std::pmr::memory_resource &other) const noexcept override=0
 Compares this memory resource with another for equality.

Detailed Description

Clean memory resource abstraction for device-specific memory allocation.

This class extends std::pmr::memory_resource with device-awareness while maintaining clear separation of concerns. Memory resources are responsible only for allocation, deallocation, and basic memory operations. Tensor-specific operations like type conversion and fill operations are handled by separate tensor operation classes.

Device-specific implementations (CPU, CUDA) override these methods with optimized implementations appropriate for their memory types and access patterns.

Constructor & Destructor Documentation

◆ ~MemoryResource()

virtual Mila::Dnn::Compute::MemoryResource::~MemoryResource ( )
virtualdefault

Virtual destructor for proper cleanup of derived classes.

Member Function Documentation

◆ do_allocate()

virtual void * Mila::Dnn::Compute::MemoryResource::do_allocate ( std::size_t bytes,
std::size_t alignment )
overrideprotectedpure virtual

Allocates memory with specified size and alignment.

Pure virtual function that must be implemented by derived classes to provide device-specific memory allocation. Implementation should handle device-specific allocation strategies and error conditions.

Parameters
bytesNumber of bytes to allocate
alignmentMemory alignment requirement
Returns
Pointer to allocated memory
Exceptions
std::bad_allocIf allocation fails

Implemented in Mila::Dnn::Compute::CpuMemoryResource, Mila::Dnn::Compute::CudaDeviceMemoryResource, Mila::Dnn::Compute::CudaManagedMemoryResource, Mila::Dnn::Compute::CudaPinnedMemoryResource, Mila::Dnn::Compute::MetalMemoryResource, Mila::Dnn::Compute::TrackedMemoryResource, and Mila::Dnn::Compute::VulkanMemoryResource.

◆ do_deallocate()

virtual void Mila::Dnn::Compute::MemoryResource::do_deallocate ( void * ptr,
std::size_t bytes,
std::size_t alignment )
overrideprotectedpure virtual

Deallocates previously allocated memory.

Pure virtual function that must be implemented by derived classes to provide device-specific memory deallocation. Implementation should handle proper cleanup and device synchronization if necessary.

Parameters
ptrPointer to memory to deallocate
bytesSize of memory block (may be used for debugging/validation)
alignmentAlignment that was used during allocation

Implemented in Mila::Dnn::Compute::CpuMemoryResource, Mila::Dnn::Compute::CudaDeviceMemoryResource, Mila::Dnn::Compute::CudaManagedMemoryResource, Mila::Dnn::Compute::CudaPinnedMemoryResource, Mila::Dnn::Compute::MetalMemoryResource, Mila::Dnn::Compute::TrackedMemoryResource, and Mila::Dnn::Compute::VulkanMemoryResource.

◆ do_is_equal()

virtual bool Mila::Dnn::Compute::MemoryResource::do_is_equal ( const std::pmr::memory_resource & other) const
overrideprotectedpure virtualnoexcept

Compares this memory resource with another for equality.

Two memory resources are equal if they can be used interchangeably for allocation and deallocation. Typically this means they are the same type and manage the same underlying memory pool or device.

Parameters
otherThe other memory resource to compare with
Returns
true if the memory resources are equivalent

Implemented in Mila::Dnn::Compute::CpuMemoryResource, Mila::Dnn::Compute::CudaDeviceMemoryResource, Mila::Dnn::Compute::CudaManagedMemoryResource, Mila::Dnn::Compute::CudaPinnedMemoryResource, Mila::Dnn::Compute::MetalMemoryResource, Mila::Dnn::Compute::TrackedMemoryResource, and Mila::Dnn::Compute::VulkanMemoryResource.

Member Data Documentation

◆ is_device_accessible

bool Mila::Dnn::Compute::MemoryResource::is_device_accessible = false
staticconstexpr

Checks if the memory is accessible from device code.

Derived classes should override this to reflect their accessibility characteristics.

◆ is_host_accessible

bool Mila::Dnn::Compute::MemoryResource::is_host_accessible = true
staticconstexpr

Checks if the memory is accessible from host code.

Derived classes should override this to reflect their accessibility characteristics.


The documentation for this class was generated from the following file: