Class MetalMemoryManager
- Namespace
- DotCompute.Backends.Metal.Memory
- Assembly
- DotCompute.Backends.Metal.dll
Metal-specific memory manager implementation with real Metal API integration. Consolidated using BaseMemoryManager to eliminate duplicate patterns.
public sealed class MetalMemoryManager : BaseMemoryManager, IUnifiedMemoryManager, IAsyncDisposable, IDisposable
- Inheritance
-
MetalMemoryManager
- Implements
- Inherited Members
- Extension Methods
Constructors
MetalMemoryManager(ILogger<MetalMemoryManager>, IAccelerator?, bool)
Initializes a new instance of the MetalMemoryManager class.
public MetalMemoryManager(ILogger<MetalMemoryManager> logger, IAccelerator? accelerator = null, bool enablePooling = true)
Parameters
loggerILogger<MetalMemoryManager>The logger instance.
acceleratorIAcceleratorOptional accelerator reference.
enablePoolingboolWhether to enable memory pooling (default: true for 90% allocation reduction).
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
Device
Gets the Metal device used by this manager.
public nint Device { get; }
Property Value
IsAppleSilicon
Gets whether the system is running on Apple Silicon with unified memory.
public bool IsAppleSilicon { get; }
Property Value
IsUnifiedMemory
Gets whether the Metal device has unified memory architecture.
public bool IsUnifiedMemory { get; }
Property Value
MaxAllocationSize
Gets the maximum memory allocation size in bytes.
public override long MaxAllocationSize { get; }
Property Value
MemoryOptimizer
Gets the unified memory optimizer.
public MetalUnifiedMemoryOptimizer MemoryOptimizer { get; }
Property Value
PoolStatistics
Gets pool statistics if pooling is enabled.
public MemoryPoolManagerStatistics? PoolStatistics { get; }
Property Value
Statistics
Gets memory usage statistics.
public override MemoryStatistics Statistics { get; }
Property Value
TotalAvailableMemory
Gets the total available memory in bytes.
public override long TotalAvailableMemory { get; }
Property Value
Methods
AllocateInternalAsync(long, MemoryOptions, CancellationToken)
Allocates memory using backend-specific implementation.
protected override ValueTask<IUnifiedMemoryBuffer> AllocateInternalAsync(long sizeInBytes, MemoryOptions options, CancellationToken cancellationToken)
Parameters
sizeInByteslongoptionsMemoryOptionscancellationTokenCancellationToken
Returns
Clear()
Clears all allocated memory and resets the manager.
public override void Clear()
ClearPools()
Clears all memory pools if pooling is enabled.
public void ClearPools()
CopyAsync<T>(IUnifiedMemoryBuffer<T>, IUnifiedMemoryBuffer<T>, CancellationToken)
Copies data between buffers.
public override ValueTask CopyAsync<T>(IUnifiedMemoryBuffer<T> source, IUnifiedMemoryBuffer<T> destination, CancellationToken cancellationToken) 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) 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) 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) 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(bool)
Disposes the memory manager and all resources.
protected override void Dispose(bool disposing)
Parameters
disposingbool
FreeAsync(IUnifiedMemoryBuffer, CancellationToken)
Asynchronously frees a memory buffer.
public override ValueTask FreeAsync(IUnifiedMemoryBuffer buffer, CancellationToken cancellationToken)
Parameters
bufferIUnifiedMemoryBufferThe buffer to free.
cancellationTokenCancellationTokenCancellation token.
Returns
- ValueTask
A task representing the free operation.
GeneratePoolReport()
Generates a detailed pool performance report.
public string? GeneratePoolReport()
Returns
GetOptimalStorageMode(MemoryUsagePattern)
Gets the optimal storage mode for a memory usage pattern.
public MetalStorageMode GetOptimalStorageMode(MemoryUsagePattern pattern)
Parameters
patternMemoryUsagePatternThe memory usage pattern.
Returns
- MetalStorageMode
The optimal Metal storage mode.
GetStatisticsAsync(CancellationToken)
Gets accurate Metal 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 Metal device for accurate memory information. On Apple Silicon, this includes unified memory statistics. For fast synchronous access, use the Statistics property.
OptimizeAsync(CancellationToken)
Optimizes memory by defragmenting and releasing unused memory.
public override ValueTask OptimizeAsync(CancellationToken cancellationToken)
Parameters
cancellationTokenCancellationTokenCancellation token.
Returns
- ValueTask
A task representing the optimization operation.
PreAllocatePoolAsync(int, int, MemoryOptions, CancellationToken)
Pre-allocates buffers to warm up the pool.
public Task PreAllocatePoolAsync(int poolSize, int count, MemoryOptions options, CancellationToken cancellationToken = default)
Parameters
poolSizeintcountintoptionsMemoryOptionscancellationTokenCancellationToken
Returns
SetAcceleratorReference(IAccelerator)
Sets the accelerator reference after construction.
public void SetAcceleratorReference(IAccelerator accelerator)
Parameters
acceleratorIAcceleratorThe accelerator to reference.