Table of Contents

Class BaseMemoryManager

Namespace
DotCompute.Core.Memory
Assembly
DotCompute.Core.dll

Base memory manager that provides common memory management patterns for all backends. Eliminates 7,625 lines of duplicate code across 5+ implementations.

public abstract class BaseMemoryManager : IUnifiedMemoryManager, IAsyncDisposable, IDisposable
Inheritance
BaseMemoryManager
Implements
Derived
Inherited Members
Extension Methods

Constructors

BaseMemoryManager(ILogger)

Base memory manager that provides common memory management patterns for all backends. Eliminates 7,625 lines of duplicate code across 5+ implementations.

protected BaseMemoryManager(ILogger logger)

Parameters

logger ILogger

Properties

Accelerator

Gets the accelerator this memory manager is associated with.

public abstract IAccelerator Accelerator { get; }

Property Value

IAccelerator

AllocationCount

Gets the total number of allocations.

public int AllocationCount { get; }

Property Value

int

CurrentAllocatedMemory

Gets the current allocated memory in bytes.

public abstract long CurrentAllocatedMemory { get; }

Property Value

long

CurrentStatistics

Gets the current memory statistics (alias for Statistics for compatibility).

public MemoryStatistics CurrentStatistics { get; }

Property Value

MemoryStatistics

IsDisposed

Gets a value indicating whether this memory manager has been disposed.

public bool IsDisposed { get; }

Property Value

bool

Logger

Gets the logger instance for derived classes.

protected ILogger Logger { get; }

Property Value

ILogger

MaxAllocationSize

Gets the maximum memory allocation size in bytes.

public abstract long MaxAllocationSize { get; }

Property Value

long

PeakAllocatedBytes

Gets the peak allocated bytes.

public long PeakAllocatedBytes { get; }

Property Value

long

Statistics

Gets memory usage statistics.

public abstract MemoryStatistics Statistics { get; }

Property Value

MemoryStatistics

TotalAllocatedBytes

Gets the total allocated bytes across all buffers.

public long TotalAllocatedBytes { get; }

Property Value

long

TotalAvailableMemory

Gets the total available memory in bytes.

public abstract long TotalAvailableMemory { get; }

Property Value

long

Methods

AllocateAndCopyAsync<T>(ReadOnlyMemory<T>, MemoryOptions, CancellationToken)

Allocates memory and copies data from host.

public virtual ValueTask<IUnifiedMemoryBuffer<T>> AllocateAndCopyAsync<T>(ReadOnlyMemory<T> source, MemoryOptions options = MemoryOptions.None, CancellationToken cancellationToken = default) where T : unmanaged

Parameters

source ReadOnlyMemory<T>

The source data to copy.

options MemoryOptions

Memory allocation options.

cancellationToken CancellationToken

Cancellation token.

Returns

ValueTask<IUnifiedMemoryBuffer<T>>

A newly allocated and initialized memory buffer.

Type Parameters

T

The element type.

AllocateAsync(long, MemoryOptions, CancellationToken)

public virtual ValueTask<IUnifiedMemoryBuffer> AllocateAsync(long sizeInBytes, MemoryOptions options = MemoryOptions.None, CancellationToken cancellationToken = default)

Parameters

sizeInBytes long
options MemoryOptions
cancellationToken CancellationToken

Returns

ValueTask<IUnifiedMemoryBuffer>

AllocateAsync<T>(int)

public virtual ValueTask<IUnifiedMemoryBuffer<T>> AllocateAsync<T>(int count) where T : unmanaged

Parameters

count int

Returns

ValueTask<IUnifiedMemoryBuffer<T>>

Type Parameters

T

AllocateAsync<T>(int, MemoryOptions, CancellationToken)

Allocates a memory buffer for a specific number of elements.

public virtual ValueTask<IUnifiedMemoryBuffer<T>> AllocateAsync<T>(int count, MemoryOptions options = MemoryOptions.None, CancellationToken cancellationToken = default) where T : unmanaged

Parameters

count int

The number of elements to allocate.

options MemoryOptions

Memory allocation options.

cancellationToken CancellationToken

Cancellation token.

Returns

ValueTask<IUnifiedMemoryBuffer<T>>

A newly allocated memory buffer.

Type Parameters

T

The element type.

AllocateBufferCoreAsync(long, MemoryOptions, CancellationToken)

Backend-specific buffer allocation implementation.

protected virtual ValueTask<IUnifiedMemoryBuffer> AllocateBufferCoreAsync(long sizeInBytes, MemoryOptions options, CancellationToken cancellationToken)

Parameters

sizeInBytes long
options MemoryOptions
cancellationToken CancellationToken

Returns

ValueTask<IUnifiedMemoryBuffer>

AllocateDevice(long)

Allocates device-specific memory.

public virtual DeviceMemory AllocateDevice(long sizeInBytes)

Parameters

sizeInBytes long

Size in bytes to allocate.

Returns

DeviceMemory

Device memory handle.

AllocateInternalAsync(long, MemoryOptions, CancellationToken)

Allocates memory using backend-specific implementation.

protected abstract ValueTask<IUnifiedMemoryBuffer> AllocateInternalAsync(long sizeInBytes, MemoryOptions options, CancellationToken cancellationToken)

Parameters

sizeInBytes long
options MemoryOptions
cancellationToken CancellationToken

Returns

ValueTask<IUnifiedMemoryBuffer>

AllocateRawAsync(long, MemoryOptions, CancellationToken)

Allocates memory by size in bytes (for advanced scenarios).

public virtual ValueTask<IUnifiedMemoryBuffer> AllocateRawAsync(long sizeInBytes, MemoryOptions options = MemoryOptions.None, CancellationToken cancellationToken = default)

Parameters

sizeInBytes long

The size in bytes to allocate.

options MemoryOptions

Memory allocation options.

cancellationToken CancellationToken

Cancellation token.

Returns

ValueTask<IUnifiedMemoryBuffer>

A newly allocated memory buffer.

CleanupUnusedBuffers()

Performs cleanup of unused buffers.

protected virtual void CleanupUnusedBuffers()

Clear()

Clears all allocated memory and resets the manager.

public abstract void Clear()

CopyAsync<T>(IUnifiedMemoryBuffer<T>, IUnifiedMemoryBuffer<T>, CancellationToken)

Copies data between buffers.

public abstract ValueTask CopyAsync<T>(IUnifiedMemoryBuffer<T> source, IUnifiedMemoryBuffer<T> destination, CancellationToken cancellationToken = default) where T : unmanaged

Parameters

source IUnifiedMemoryBuffer<T>

The source buffer.

destination IUnifiedMemoryBuffer<T>

The destination buffer.

cancellationToken CancellationToken

Cancellation token.

Returns

ValueTask

A task representing the copy operation.

Type Parameters

T

The element type.

CopyAsync<T>(IUnifiedMemoryBuffer<T>, int, IUnifiedMemoryBuffer<T>, int, int, CancellationToken)

Copies data between buffers with specified ranges.

public abstract ValueTask CopyAsync<T>(IUnifiedMemoryBuffer<T> source, int sourceOffset, IUnifiedMemoryBuffer<T> destination, int destinationOffset, int count, CancellationToken cancellationToken = default) where T : unmanaged

Parameters

source IUnifiedMemoryBuffer<T>

The source buffer.

sourceOffset int

The offset in the source buffer.

destination IUnifiedMemoryBuffer<T>

The destination buffer.

destinationOffset int

The offset in the destination buffer.

count int

The number of elements to copy.

cancellationToken CancellationToken

Cancellation token.

Returns

ValueTask

A task representing the copy operation.

Type Parameters

T

The element type.

CopyDeviceToDevice(DeviceMemory, DeviceMemory, long)

Copies data between device memories.

public virtual void CopyDeviceToDevice(DeviceMemory sourceDevice, DeviceMemory destinationDevice, long sizeInBytes)

Parameters

sourceDevice DeviceMemory

Source device memory.

destinationDevice DeviceMemory

Destination device memory.

sizeInBytes long

Size in bytes.

CopyDeviceToHost(DeviceMemory, nint, long)

Copies data from device to host memory.

public virtual void CopyDeviceToHost(DeviceMemory deviceMemory, nint hostPointer, long sizeInBytes)

Parameters

deviceMemory DeviceMemory

Device memory.

hostPointer nint

Host memory pointer.

sizeInBytes long

Size in bytes.

CopyDeviceToHostAsync(DeviceMemory, nint, long, CancellationToken)

Asynchronously copies data from device to host memory.

public virtual ValueTask CopyDeviceToHostAsync(DeviceMemory deviceMemory, nint hostPointer, long sizeInBytes, CancellationToken cancellationToken = default)

Parameters

deviceMemory DeviceMemory

Device memory.

hostPointer nint

Host memory pointer.

sizeInBytes long

Size in bytes.

cancellationToken CancellationToken

Cancellation token.

Returns

ValueTask

A task representing the operation.

CopyFromDeviceAsync<T>(IUnifiedMemoryBuffer<T>, Memory<T>, CancellationToken)

Copies data from a device buffer to host memory.

public abstract ValueTask CopyFromDeviceAsync<T>(IUnifiedMemoryBuffer<T> source, Memory<T> destination, CancellationToken cancellationToken = default) where T : unmanaged

Parameters

source IUnifiedMemoryBuffer<T>

The source buffer.

destination Memory<T>

The destination memory.

cancellationToken CancellationToken

Cancellation token.

Returns

ValueTask

A task representing the copy operation.

Type Parameters

T

The element type.

CopyFromDevice<T>(Span<T>, IUnifiedMemoryBuffer)

public virtual void CopyFromDevice<T>(Span<T> data, IUnifiedMemoryBuffer buffer) where T : unmanaged

Parameters

data Span<T>
buffer IUnifiedMemoryBuffer

Type Parameters

T

CopyHostToDevice(nint, DeviceMemory, long)

Copies data from host to device memory.

public virtual void CopyHostToDevice(nint hostPointer, DeviceMemory deviceMemory, long sizeInBytes)

Parameters

hostPointer nint

Host memory pointer.

deviceMemory DeviceMemory

Device memory.

sizeInBytes long

Size in bytes.

CopyHostToDeviceAsync(nint, DeviceMemory, long, CancellationToken)

Asynchronously copies data from host to device memory.

public virtual ValueTask CopyHostToDeviceAsync(nint hostPointer, DeviceMemory deviceMemory, long sizeInBytes, CancellationToken cancellationToken = default)

Parameters

hostPointer nint

Host memory pointer.

deviceMemory DeviceMemory

Device memory.

sizeInBytes long

Size in bytes.

cancellationToken CancellationToken

Cancellation token.

Returns

ValueTask

A task representing the operation.

CopyToDeviceAsync<T>(ReadOnlyMemory<T>, IUnifiedMemoryBuffer<T>, CancellationToken)

Copies data from host memory to a device buffer.

public abstract ValueTask CopyToDeviceAsync<T>(ReadOnlyMemory<T> source, IUnifiedMemoryBuffer<T> destination, CancellationToken cancellationToken = default) where T : unmanaged

Parameters

source ReadOnlyMemory<T>

The source data.

destination IUnifiedMemoryBuffer<T>

The destination buffer.

cancellationToken CancellationToken

Cancellation token.

Returns

ValueTask

A task representing the copy operation.

Type Parameters

T

The element type.

CopyToDevice<T>(IUnifiedMemoryBuffer, ReadOnlySpan<T>)

public virtual void CopyToDevice<T>(IUnifiedMemoryBuffer buffer, ReadOnlySpan<T> data) where T : unmanaged

Parameters

buffer IUnifiedMemoryBuffer
data ReadOnlySpan<T>

Type Parameters

T

CreateView(IUnifiedMemoryBuffer, long, long)

public virtual IUnifiedMemoryBuffer CreateView(IUnifiedMemoryBuffer buffer, long offset, long length)

Parameters

buffer IUnifiedMemoryBuffer
offset long
length long

Returns

IUnifiedMemoryBuffer

CreateViewCore(IUnifiedMemoryBuffer, long, long)

Backend-specific view creation implementation.

protected abstract IUnifiedMemoryBuffer CreateViewCore(IUnifiedMemoryBuffer buffer, long offset, long length)

Parameters

buffer IUnifiedMemoryBuffer
offset long
length long

Returns

IUnifiedMemoryBuffer

CreateView<T>(IUnifiedMemoryBuffer<T>, int, int)

Creates a view over existing memory.

public abstract IUnifiedMemoryBuffer<T> CreateView<T>(IUnifiedMemoryBuffer<T> buffer, int offset, int length) where T : unmanaged

Parameters

buffer IUnifiedMemoryBuffer<T>

The source buffer.

offset int

The offset in elements.

length int

The length of the view in elements.

Returns

IUnifiedMemoryBuffer<T>

A view over the existing buffer.

Type Parameters

T

The element type.

Dispose()

Performs dispose.

public void Dispose()

Dispose(bool)

protected virtual void Dispose(bool disposing)

Parameters

disposing bool

DisposeAsync()

Gets dispose asynchronously.

public virtual ValueTask DisposeAsync()

Returns

ValueTask

The result of the operation.

Free(IUnifiedMemoryBuffer)

Frees a memory buffer.

public virtual void Free(IUnifiedMemoryBuffer buffer)

Parameters

buffer IUnifiedMemoryBuffer

The buffer to free.

FreeAsync(IUnifiedMemoryBuffer, CancellationToken)

Asynchronously frees a memory buffer.

public abstract ValueTask FreeAsync(IUnifiedMemoryBuffer buffer, CancellationToken cancellationToken = default)

Parameters

buffer IUnifiedMemoryBuffer

The buffer to free.

cancellationToken CancellationToken

Cancellation token.

Returns

ValueTask

A task representing the free operation.

FreeDevice(DeviceMemory)

Frees device-specific memory.

public virtual void FreeDevice(DeviceMemory deviceMemory)

Parameters

deviceMemory DeviceMemory

Device memory to free.

GetRefreshedStatistics()

Gets refreshed memory statistics for this manager after cleaning up unused buffers.

public virtual MemoryStatistics GetRefreshedStatistics()

Returns

MemoryStatistics

MemsetDevice(DeviceMemory, byte, long)

Sets device memory to a specific value.

public virtual void MemsetDevice(DeviceMemory deviceMemory, byte value, long sizeInBytes)

Parameters

deviceMemory DeviceMemory

Device memory to set.

value byte

Value to set.

sizeInBytes long

Size in bytes.

MemsetDeviceAsync(DeviceMemory, byte, long, CancellationToken)

Asynchronously sets device memory to a specific value.

public virtual ValueTask MemsetDeviceAsync(DeviceMemory deviceMemory, byte value, long sizeInBytes, CancellationToken cancellationToken = default)

Parameters

deviceMemory DeviceMemory

Device memory to set.

value byte

Value to set.

sizeInBytes long

Size in bytes.

cancellationToken CancellationToken

Cancellation token.

Returns

ValueTask

A task representing the operation.

OptimizeAsync(CancellationToken)

Optimizes memory by defragmenting and releasing unused memory.

public abstract ValueTask OptimizeAsync(CancellationToken cancellationToken = default)

Parameters

cancellationToken CancellationToken

Cancellation token.

Returns

ValueTask

A task representing the optimization operation.

ThrowIfDisposed()

protected void ThrowIfDisposed()

TrackBuffer(IUnifiedMemoryBuffer, long)

Tracks a newly allocated buffer.

protected virtual void TrackBuffer(IUnifiedMemoryBuffer buffer, long sizeInBytes)

Parameters

buffer IUnifiedMemoryBuffer
sizeInBytes long