Class BaseMemoryBuffer<T>
- Namespace
- DotCompute.Memory
- Assembly
- DotCompute.Memory.dll
Base abstract class for memory buffer implementations, consolidating common patterns. This addresses the critical issue of 15+ duplicate buffer implementations.
public abstract class BaseMemoryBuffer<T> : IUnifiedMemoryBuffer<T>, IUnifiedMemoryBuffer, IAsyncDisposable, IDisposable where T : unmanaged
Type Parameters
T
- Inheritance
-
BaseMemoryBuffer<T>
- Implements
- Derived
- Inherited Members
- Extension Methods
Constructors
BaseMemoryBuffer(long)
Initializes a new instance of the BaseMemoryBuffer<T> class.
protected BaseMemoryBuffer(long sizeInBytes)
Parameters
sizeInByteslong
Properties
Accelerator
Gets the accelerator this buffer is associated with.
public abstract IAccelerator Accelerator { get; }
Property Value
DevicePointer
public abstract nint DevicePointer { get; }
Property Value
IsDirty
Gets whether the buffer has been modified and needs synchronization.
public abstract bool IsDirty { get; }
Property Value
IsDisposed
Gets whether the buffer has been disposed.
public abstract bool IsDisposed { get; }
Property Value
IsOnDevice
Gets whether the buffer is currently available on the device.
public abstract bool IsOnDevice { get; }
Property Value
IsOnHost
Gets whether the buffer is currently available on the host.
public abstract bool IsOnHost { get; }
Property Value
Length
Gets the number of elements in the buffer.
public int Length { get; }
Property Value
MemoryType
public abstract MemoryType MemoryType { get; }
Property Value
Options
Gets the memory allocation options.
public abstract MemoryOptions Options { get; }
Property Value
SizeInBytes
Gets the size of the buffer in bytes.
public long SizeInBytes { get; }
Property Value
State
Gets the current state of the buffer.
public abstract BufferState State { get; }
Property Value
Methods
AsMemory()
Gets a memory handle to the host memory. Will trigger transfer if needed.
public abstract Memory<T> AsMemory()
Returns
- Memory<T>
AsReadOnlyMemory()
Gets a read-only memory handle to the host memory. Will trigger transfer if needed.
public abstract ReadOnlyMemory<T> AsReadOnlyMemory()
Returns
AsReadOnlySpan()
Gets a read-only span to the host memory. Will trigger transfer if needed.
public abstract ReadOnlySpan<T> AsReadOnlySpan()
Returns
- ReadOnlySpan<T>
AsSpan()
Gets a span to the host memory. Will trigger transfer if needed.
public abstract Span<T> AsSpan()
Returns
- Span<T>
AsType<TNew>()
Creates a view of this buffer with a different element type.
public abstract IUnifiedMemoryBuffer<TNew> AsType<TNew>() where TNew : unmanaged
Returns
- IUnifiedMemoryBuffer<TNew>
A view of this buffer as the new type.
Type Parameters
TNewThe new element type.
CopyFromAsync(IUnifiedMemoryBuffer<T>, long, long, long, CancellationToken)
public abstract ValueTask CopyFromAsync(IUnifiedMemoryBuffer<T> source, long sourceOffset = 0, long destinationOffset = 0, long count = -1, CancellationToken cancellationToken = default)
Parameters
sourceIUnifiedMemoryBuffer<T>sourceOffsetlongdestinationOffsetlongcountlongcancellationTokenCancellationToken
Returns
CopyFromAsync(ReadOnlyMemory<T>, long, CancellationToken)
public abstract ValueTask CopyFromAsync(ReadOnlyMemory<T> source, long offset = 0, CancellationToken cancellationToken = default)
Parameters
sourceReadOnlyMemory<T>offsetlongcancellationTokenCancellationToken
Returns
CopyFromAsync(ReadOnlyMemory<T>, CancellationToken)
Copies data from a source memory into this buffer.
public abstract ValueTask CopyFromAsync(ReadOnlyMemory<T> source, CancellationToken cancellationToken = default)
Parameters
sourceReadOnlyMemory<T>cancellationTokenCancellationToken
Returns
CopyToAsync(IUnifiedMemoryBuffer<T>, CancellationToken)
Copies data from this buffer to another buffer.
public abstract ValueTask CopyToAsync(IUnifiedMemoryBuffer<T> destination, CancellationToken cancellationToken = default)
Parameters
destinationIUnifiedMemoryBuffer<T>cancellationTokenCancellationToken
Returns
CopyToAsync(int, IUnifiedMemoryBuffer<T>, int, int, CancellationToken)
Copies data from this buffer to another buffer with specified ranges.
public abstract ValueTask CopyToAsync(int sourceOffset, IUnifiedMemoryBuffer<T> destination, int destinationOffset, int count, CancellationToken cancellationToken = default)
Parameters
sourceOffsetintdestinationIUnifiedMemoryBuffer<T>destinationOffsetintcountintcancellationTokenCancellationToken
Returns
CopyToAsync(Memory<T>, long, CancellationToken)
public abstract ValueTask CopyToAsync(Memory<T> destination, long offset = 0, CancellationToken cancellationToken = default)
Parameters
destinationMemory<T>offsetlongcancellationTokenCancellationToken
Returns
CopyToAsync(Memory<T>, CancellationToken)
Copies data from this buffer to a destination memory.
public abstract ValueTask CopyToAsync(Memory<T> destination, CancellationToken cancellationToken = default)
Parameters
destinationMemory<T>cancellationTokenCancellationToken
Returns
Dispose()
Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
public abstract void Dispose()
DisposeAsync()
Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources asynchronously.
public abstract ValueTask DisposeAsync()
Returns
- ValueTask
A task that represents the asynchronous dispose operation.
EnsureOnDevice()
Ensures the buffer is available on the device. Triggers transfer if needed.
public abstract void EnsureOnDevice()
EnsureOnDeviceAsync(AcceleratorContext, CancellationToken)
Asynchronously ensures the buffer is available on the device.
public abstract ValueTask EnsureOnDeviceAsync(AcceleratorContext context = default, CancellationToken cancellationToken = default)
Parameters
contextAcceleratorContextcancellationTokenCancellationToken
Returns
EnsureOnHost()
Ensures the buffer is available on the host. Triggers transfer if needed.
public abstract void EnsureOnHost()
EnsureOnHostAsync(AcceleratorContext, CancellationToken)
Asynchronously ensures the buffer is available on the host.
public abstract ValueTask EnsureOnHostAsync(AcceleratorContext context = default, CancellationToken cancellationToken = default)
Parameters
contextAcceleratorContextcancellationTokenCancellationToken
Returns
FillAsync(T, int, int, CancellationToken)
Fills a portion of this buffer with a specified value.
public abstract ValueTask FillAsync(T value, int offset, int count, CancellationToken cancellationToken = default)
Parameters
valueToffsetintcountintcancellationTokenCancellationToken
Returns
FillAsync(T, CancellationToken)
Fills this buffer with a specified value.
public abstract ValueTask FillAsync(T value, CancellationToken cancellationToken = default)
Parameters
valueTcancellationTokenCancellationToken
Returns
GetDeviceMemory()
Gets the device memory handle. Will trigger transfer if needed.
public abstract DeviceMemory GetDeviceMemory()
Returns
Map(MapMode)
Maps this buffer to host memory for direct access.
public virtual MappedMemory<T> Map(MapMode mode = MapMode.ReadWrite)
Parameters
modeMapModeThe mapping mode.
Returns
- MappedMemory<T>
A mapped memory region.
MapAsync(MapMode, CancellationToken)
Asynchronously maps this buffer to host memory.
public virtual ValueTask<MappedMemory<T>> MapAsync(MapMode mode = MapMode.ReadWrite, CancellationToken cancellationToken = default)
Parameters
modeMapModeThe mapping mode.
cancellationTokenCancellationTokenCancellation token.
Returns
- ValueTask<MappedMemory<T>>
A task that returns the mapped memory region.
MapRange(int, int, MapMode)
Maps a portion of this buffer to host memory for direct access.
public virtual MappedMemory<T> MapRange(int offset, int length, MapMode mode = MapMode.ReadWrite)
Parameters
offsetintThe offset to start mapping at.
lengthintThe number of elements to map.
modeMapModeThe mapping mode.
Returns
- MappedMemory<T>
A mapped memory region.
MarkDeviceDirty()
Marks the buffer as modified on the device.
public abstract void MarkDeviceDirty()
MarkHostDirty()
Marks the buffer as modified on the host.
public abstract void MarkHostDirty()
Slice(int, int)
Creates a slice of this buffer.
public abstract IUnifiedMemoryBuffer<T> Slice(int offset, int length)
Parameters
Returns
- IUnifiedMemoryBuffer<T>
A slice of this buffer.
Synchronize()
Synchronizes the buffer state between host and device.
public abstract void Synchronize()
SynchronizeAsync(AcceleratorContext, CancellationToken)
Asynchronously synchronizes the buffer state between host and device.
public abstract ValueTask SynchronizeAsync(AcceleratorContext context = default, CancellationToken cancellationToken = default)
Parameters
contextAcceleratorContextcancellationTokenCancellationToken
Returns
ThrowIfDisposed()
Throws if the buffer has been disposed.
protected void ThrowIfDisposed()
ValidateCopyParameters(long, long, long, long, long)
Validates copy parameters.
protected void ValidateCopyParameters(long sourceLength, long sourceOffset, long destinationLength, long destinationOffset, long count)