Class CudaMemoryManager
- Namespace
- DotCompute.Backends.CUDA.Memory
- Assembly
- DotCompute.Backends.CUDA.dll
High-performance CUDA device memory manager with automatic pooling and unified memory support.
public sealed class CudaMemoryManager : BaseMemoryManager, IUnifiedMemoryManager, IAsyncDisposable, IDisposable
- Inheritance
-
CudaMemoryManager
- Implements
- Inherited Members
- Extension Methods
Examples
var manager = new CudaMemoryManager(context, device, logger);
// Allocate device memory for GPU-only operations
var deviceBuffer = await manager.AllocateAsync<float>(1024);
// Allocate unified memory for CPU/GPU sharing
var unifiedBuffer = await manager.AllocateAsync<float>(1024, MemoryOptions.Unified);
// Copy data to device
await manager.CopyToDeviceAsync(hostData, deviceBuffer);
Remarks
The memory manager provides efficient GPU memory allocation with:
- Zero-copy unified memory (cudaMallocManaged) for CPU/GPU shared access
- Pinned host memory (cudaHostAlloc) for faster PCIe transfers
- Standard device memory (cudaMalloc) for GPU-only allocations
- Automatic memory pooling to reduce allocation overhead by ~90%
- Thread-safe allocation tracking with detailed statistics
Memory Types:
| Device Memory | GPU-only, highest performance (MemoryOptions.None) |
| Unified Memory | CPU/GPU accessible, automatic migration (MemoryOptions.Unified) |
| Pinned Memory | Host memory locked for faster DMA (MemoryOptions.Pinned) |
Constructors
CudaMemoryManager(CudaContext, CudaDevice?, ILogger)
Initializes a new instance of the CudaMemoryManager class.
public CudaMemoryManager(CudaContext context, CudaDevice? device, ILogger logger)
Parameters
contextCudaContextThe context.
deviceCudaDeviceThe device.
loggerILoggerThe logger.
CudaMemoryManager(CudaContext, ILogger)
Initializes a new instance of the CudaMemoryManager class.
public CudaMemoryManager(CudaContext context, ILogger logger)
Parameters
contextCudaContextThe context.
loggerILoggerThe logger.
Properties
Accelerator
Gets the accelerator this memory manager is associated with.
public override IAccelerator Accelerator { get; }
Property Value
CurrentAllocatedMemory
Gets the current allocated memory in bytes.
public override long CurrentAllocatedMemory { get; }
Property Value
MaxAllocationSize
Gets the maximum allocation size supported.
public override long MaxAllocationSize { get; }
Property Value
Statistics
Gets memory usage statistics.
public override MemoryStatistics Statistics { get; }
Property Value
TotalAllocated
Gets the total amount of memory currently allocated.
public long TotalAllocated { get; }
Property Value
TotalAvailableMemory
Gets the total available memory on the device for interface compatibility.
public override long TotalAvailableMemory { get; }
Property Value
TotalMemory
Gets the total available memory on the device.
public long TotalMemory { get; }
Property Value
UsedMemory
Gets the currently used memory on the device.
public long UsedMemory { get; }
Property Value
Methods
AllocateAsync(long, CancellationToken)
Allocates device memory asynchronously.
public Task<IUnifiedMemoryBuffer> AllocateAsync(long sizeInBytes, CancellationToken cancellationToken = default)
Parameters
sizeInByteslongcancellationTokenCancellationToken
Returns
AllocateAsync<T>(long, MemoryOptions, CancellationToken)
Allocates memory asynchronously with specific type and options.
public ValueTask<IUnifiedMemoryBuffer<T>> AllocateAsync<T>(long count, MemoryOptions options, CancellationToken cancellationToken = default) where T : unmanaged
Parameters
countlongoptionsMemoryOptionscancellationTokenCancellationToken
Returns
Type Parameters
T
AllocateAsync<T>(long, CancellationToken)
Allocates device memory asynchronously with specific type.
public ValueTask<IUnifiedMemoryBuffer<T>> AllocateAsync<T>(long count, CancellationToken cancellationToken = default) where T : unmanaged
Parameters
countlongcancellationTokenCancellationToken
Returns
Type Parameters
T
AllocateInternalAsync(long, MemoryOptions, CancellationToken)
Allocates memory using backend-specific implementation.
protected override ValueTask<IUnifiedMemoryBuffer> AllocateInternalAsync(long sizeInBytes, MemoryOptions options, CancellationToken cancellationToken)
Parameters
sizeInByteslongoptionsMemoryOptionscancellationTokenCancellationToken
Returns
Allocate<T>(long)
Allocates device memory synchronously.
public IUnifiedMemoryBuffer<T> Allocate<T>(long count) where T : unmanaged
Parameters
countlong
Returns
Type Parameters
T
Clear()
Clears all allocated memory and resets the manager.
public override void Clear()
CopyAsync<T>(IUnifiedMemoryBuffer<T>, IUnifiedMemoryBuffer<T>, CancellationToken)
Copies data between buffers.
public override ValueTask CopyAsync<T>(IUnifiedMemoryBuffer<T> source, IUnifiedMemoryBuffer<T> destination, CancellationToken cancellationToken = default) where T : unmanaged
Parameters
sourceIUnifiedMemoryBuffer<T>The source buffer.
destinationIUnifiedMemoryBuffer<T>The destination buffer.
cancellationTokenCancellationTokenCancellation token.
Returns
- ValueTask
A task representing the copy operation.
Type Parameters
TThe element type.
CopyAsync<T>(IUnifiedMemoryBuffer<T>, int, IUnifiedMemoryBuffer<T>, int, int, CancellationToken)
Copies data between buffers with specified ranges.
public override ValueTask CopyAsync<T>(IUnifiedMemoryBuffer<T> source, int sourceOffset, IUnifiedMemoryBuffer<T> destination, int destinationOffset, int count, CancellationToken cancellationToken = default) where T : unmanaged
Parameters
sourceIUnifiedMemoryBuffer<T>The source buffer.
sourceOffsetintThe offset in the source buffer.
destinationIUnifiedMemoryBuffer<T>The destination buffer.
destinationOffsetintThe offset in the destination buffer.
countintThe number of elements to copy.
cancellationTokenCancellationTokenCancellation token.
Returns
- ValueTask
A task representing the copy operation.
Type Parameters
TThe element type.
CopyFromDeviceAsync<T>(IUnifiedMemoryBuffer<T>, Memory<T>, CancellationToken)
Copies data from a device buffer to host memory.
public override ValueTask CopyFromDeviceAsync<T>(IUnifiedMemoryBuffer<T> source, Memory<T> destination, CancellationToken cancellationToken = default) where T : unmanaged
Parameters
sourceIUnifiedMemoryBuffer<T>The source buffer.
destinationMemory<T>The destination memory.
cancellationTokenCancellationTokenCancellation token.
Returns
- ValueTask
A task representing the copy operation.
Type Parameters
TThe element type.
CopyToDeviceAsync<T>(ReadOnlyMemory<T>, IUnifiedMemoryBuffer<T>, CancellationToken)
Copies data from host memory to a device buffer.
public override ValueTask CopyToDeviceAsync<T>(ReadOnlyMemory<T> source, IUnifiedMemoryBuffer<T> destination, CancellationToken cancellationToken = default) where T : unmanaged
Parameters
sourceReadOnlyMemory<T>The source data.
destinationIUnifiedMemoryBuffer<T>The destination buffer.
cancellationTokenCancellationTokenCancellation token.
Returns
- ValueTask
A task representing the copy operation.
Type Parameters
TThe element type.
CreateViewCore(IUnifiedMemoryBuffer, long, long)
Backend-specific view creation implementation.
protected override IUnifiedMemoryBuffer CreateViewCore(IUnifiedMemoryBuffer buffer, long offset, long length)
Parameters
bufferIUnifiedMemoryBufferoffsetlonglengthlong
Returns
CreateView<T>(IUnifiedMemoryBuffer<T>, int, int)
Creates a view over existing memory.
public override IUnifiedMemoryBuffer<T> CreateView<T>(IUnifiedMemoryBuffer<T> buffer, int offset, int length) where T : unmanaged
Parameters
bufferIUnifiedMemoryBuffer<T>The source buffer.
offsetintThe offset in elements.
lengthintThe length of the view in elements.
Returns
- IUnifiedMemoryBuffer<T>
A view over the existing buffer.
Type Parameters
TThe element type.
Dispose()
Performs dispose.
public void Dispose()
DisposeAsync()
Asynchronously disposes the memory manager.
public override ValueTask DisposeAsync()
Returns
- ValueTask
A ValueTask representing the asynchronous dispose operation.
Free(nint)
Frees device memory.
public void Free(nint devicePtr)
Parameters
devicePtrnint
FreeAsync(IUnifiedMemoryBuffer, CancellationToken)
Asynchronously frees a memory buffer.
public override ValueTask FreeAsync(IUnifiedMemoryBuffer buffer, CancellationToken cancellationToken = default)
Parameters
bufferIUnifiedMemoryBufferThe buffer to free.
cancellationTokenCancellationTokenCancellation token.
Returns
- ValueTask
A task representing the free operation.
FreeAsync(nint, CancellationToken)
Frees device memory asynchronously.
public Task FreeAsync(nint devicePtr, CancellationToken cancellationToken = default)
Parameters
devicePtrnintcancellationTokenCancellationToken
Returns
GetStatisticsAsync(CancellationToken)
Gets accurate CUDA memory statistics asynchronously.
public ValueTask<MemoryStatistics> GetStatisticsAsync(CancellationToken cancellationToken = default)
Parameters
cancellationTokenCancellationTokenCancellation token.
Returns
- ValueTask<MemoryStatistics>
A task containing accurate memory statistics from the device.
Remarks
This method queries the CUDA device using cudaMemGetInfo() which may take 1-10ms but provides the most accurate view of current device memory state. For fast synchronous access, use the Statistics property.
OptimizeAsync(CancellationToken)
Optimizes memory by defragmenting and releasing unused memory.
public override ValueTask OptimizeAsync(CancellationToken cancellationToken = default)
Parameters
cancellationTokenCancellationTokenCancellation token.
Returns
- ValueTask
A task representing the optimization operation.
Reset()
Resets the memory manager by clearing all tracking and reinitializing.
public void Reset()