Table of Contents

Class BaseMemoryBufferView<T>

Namespace
DotCompute.Memory
Assembly
DotCompute.Memory.dll

Base class for memory buffer views that represent a slice of a parent buffer. Provides common implementation for views across all backends (CPU, CUDA, Metal, OpenCL).

public abstract class BaseMemoryBufferView<T> : IUnifiedMemoryBuffer<T>, IUnifiedMemoryBuffer, IAsyncDisposable, IDisposable where T : unmanaged

Type Parameters

T

The element type of the buffer.

Inheritance
BaseMemoryBufferView<T>
Implements
Inherited Members
Extension Methods

Remarks

Views do not own the underlying memory - the parent buffer manages allocation and disposal. Backends should extend this class to implement backend-specific behavior.

Constructors

BaseMemoryBufferView(IUnifiedMemoryBuffer<T>, int, int)

Initializes a new instance of the BaseMemoryBufferView<T> class.

protected BaseMemoryBufferView(IUnifiedMemoryBuffer<T> parent, int offset, int length)

Parameters

parent IUnifiedMemoryBuffer<T>

The parent buffer to create a view into.

offset int

The offset in elements from the start of the parent buffer.

length int

The number of elements in the view.

Exceptions

ArgumentNullException

Thrown if parent is null.

ArgumentOutOfRangeException

Thrown if offset or length are invalid.

Properties

Accelerator

Gets the accelerator this buffer is associated with.

public IAccelerator Accelerator { get; }

Property Value

IAccelerator

IsDirty

Gets whether the buffer has been modified and needs synchronization.

public bool IsDirty { get; }

Property Value

bool

IsDisposed

Gets whether the buffer has been disposed.

public bool IsDisposed { get; }

Property Value

bool

IsOnDevice

Gets whether the buffer is currently available on the device.

public bool IsOnDevice { get; }

Property Value

bool

IsOnHost

Gets whether the buffer is currently available on the host.

public bool IsOnHost { get; }

Property Value

bool

Length

Gets the number of elements in the buffer.

public int Length { get; }

Property Value

int

Offset

Gets the offset in elements from the parent buffer start.

protected int Offset { get; }

Property Value

int

Options

Gets the memory allocation options.

public MemoryOptions Options { get; }

Property Value

MemoryOptions

Parent

Gets the parent buffer this view references.

protected IUnifiedMemoryBuffer<T> Parent { get; }

Property Value

IUnifiedMemoryBuffer<T>

SizeInBytes

Gets the size of the buffer in bytes.

public long SizeInBytes { get; }

Property Value

long

State

Gets the current state of the buffer.

public BufferState State { get; }

Property Value

BufferState

Methods

AsMemory()

Gets a memory handle to the host memory. Will trigger transfer if needed.

public virtual Memory<T> AsMemory()

Returns

Memory<T>

AsReadOnlyMemory()

Gets a read-only memory handle to the host memory. Will trigger transfer if needed.

public virtual ReadOnlyMemory<T> AsReadOnlyMemory()

Returns

ReadOnlyMemory<T>

AsReadOnlySpan()

Gets a read-only span to the host memory. Will trigger transfer if needed.

public virtual ReadOnlySpan<T> AsReadOnlySpan()

Returns

ReadOnlySpan<T>

AsSpan()

Gets a span to the host memory. Will trigger transfer if needed.

public virtual Span<T> AsSpan()

Returns

Span<T>

AsType<TNew>()

Creates a view of this buffer with a different element type.

public virtual IUnifiedMemoryBuffer<TNew> AsType<TNew>() where TNew : unmanaged

Returns

IUnifiedMemoryBuffer<TNew>

A view of this buffer as the new type.

Type Parameters

TNew

The new element type.

CopyFromAsync(IUnifiedMemoryBuffer<T>, long, long, long, CancellationToken)

public virtual ValueTask CopyFromAsync(IUnifiedMemoryBuffer<T> source, long sourceOffset = 0, long destinationOffset = 0, long count = -1, CancellationToken cancellationToken = default)

Parameters

source IUnifiedMemoryBuffer<T>
sourceOffset long
destinationOffset long
count long
cancellationToken CancellationToken

Returns

ValueTask

CopyFromAsync(ReadOnlyMemory<T>, long, CancellationToken)

public virtual ValueTask CopyFromAsync(ReadOnlyMemory<T> source, long offset = 0, CancellationToken cancellationToken = default)

Parameters

source ReadOnlyMemory<T>
offset long
cancellationToken CancellationToken

Returns

ValueTask

CopyFromAsync(ReadOnlyMemory<T>, CancellationToken)

Copies data from a source memory into this buffer.

public virtual ValueTask CopyFromAsync(ReadOnlyMemory<T> source, CancellationToken cancellationToken = default)

Parameters

source ReadOnlyMemory<T>
cancellationToken CancellationToken

Returns

ValueTask

CopyToAsync(IUnifiedMemoryBuffer<T>, CancellationToken)

Copies data from this buffer to another buffer.

public virtual ValueTask CopyToAsync(IUnifiedMemoryBuffer<T> destination, CancellationToken cancellationToken = default)

Parameters

destination IUnifiedMemoryBuffer<T>
cancellationToken CancellationToken

Returns

ValueTask

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

Copies data from this buffer to another buffer with specified ranges.

public virtual ValueTask CopyToAsync(int sourceOffset, IUnifiedMemoryBuffer<T> destination, int destinationOffset, int count, CancellationToken cancellationToken = default)

Parameters

sourceOffset int
destination IUnifiedMemoryBuffer<T>
destinationOffset int
count int
cancellationToken CancellationToken

Returns

ValueTask

CopyToAsync(Memory<T>, long, CancellationToken)

public virtual ValueTask CopyToAsync(Memory<T> destination, long offset = 0, CancellationToken cancellationToken = default)

Parameters

destination Memory<T>
offset long
cancellationToken CancellationToken

Returns

ValueTask

CopyToAsync(Memory<T>, CancellationToken)

Copies data from this buffer to a destination memory.

public virtual ValueTask CopyToAsync(Memory<T> destination, CancellationToken cancellationToken = default)

Parameters

destination Memory<T>
cancellationToken CancellationToken

Returns

ValueTask

CreateSlice(int, int)

Creates a new slice view. Override in derived classes to return the appropriate type.

protected abstract IUnifiedMemoryBuffer<T> CreateSlice(int absoluteOffset, int length)

Parameters

absoluteOffset int

The absolute offset from the parent buffer start.

length int

The length of the slice.

Returns

IUnifiedMemoryBuffer<T>

A new view instance.

Dispose()

Views do not own memory - disposal only marks the view as unusable.

public void Dispose()

DisposeAsync()

Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources asynchronously.

public 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 virtual void EnsureOnDevice()

EnsureOnDeviceAsync(AcceleratorContext, CancellationToken)

Asynchronously ensures the buffer is available on the device.

public virtual ValueTask EnsureOnDeviceAsync(AcceleratorContext context = default, CancellationToken cancellationToken = default)

Parameters

context AcceleratorContext
cancellationToken CancellationToken

Returns

ValueTask

EnsureOnHost()

Ensures the buffer is available on the host. Triggers transfer if needed.

public virtual void EnsureOnHost()

EnsureOnHostAsync(AcceleratorContext, CancellationToken)

Asynchronously ensures the buffer is available on the host.

public virtual ValueTask EnsureOnHostAsync(AcceleratorContext context = default, CancellationToken cancellationToken = default)

Parameters

context AcceleratorContext
cancellationToken CancellationToken

Returns

ValueTask

FillAsync(T, int, int, CancellationToken)

Fills a portion of this buffer with a specified value.

public virtual ValueTask FillAsync(T value, int offset, int count, CancellationToken cancellationToken = default)

Parameters

value T
offset int
count int
cancellationToken CancellationToken

Returns

ValueTask

FillAsync(T, CancellationToken)

Fills this buffer with a specified value.

public virtual ValueTask FillAsync(T value, CancellationToken cancellationToken = default)

Parameters

value T
cancellationToken CancellationToken

Returns

ValueTask

GetDeviceMemory()

Gets the device memory handle. Will trigger transfer if needed.

public virtual DeviceMemory GetDeviceMemory()

Returns

DeviceMemory

Map(MapMode)

Maps this buffer to host memory for direct access.

public virtual MappedMemory<T> Map(MapMode mode = MapMode.ReadWrite)

Parameters

mode MapMode

The 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

mode MapMode

The mapping mode.

cancellationToken CancellationToken

Cancellation 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

offset int

The offset to start mapping at.

length int

The number of elements to map.

mode MapMode

The mapping mode.

Returns

MappedMemory<T>

A mapped memory region.

MarkDeviceDirty()

Marks the buffer as modified on the device.

public virtual void MarkDeviceDirty()

MarkHostDirty()

Marks the buffer as modified on the host.

public virtual void MarkHostDirty()

Slice(int, int)

Creates a slice of this buffer.

public virtual IUnifiedMemoryBuffer<T> Slice(int offset, int length)

Parameters

offset int

The offset in elements.

length int

The length of the slice in elements.

Returns

IUnifiedMemoryBuffer<T>

A slice of this buffer.

Synchronize()

Synchronizes the buffer state between host and device.

public virtual void Synchronize()

SynchronizeAsync(AcceleratorContext, CancellationToken)

Asynchronously synchronizes the buffer state between host and device.

public virtual ValueTask SynchronizeAsync(AcceleratorContext context = default, CancellationToken cancellationToken = default)

Parameters

context AcceleratorContext
cancellationToken CancellationToken

Returns

ValueTask

ThrowIfDisposed()

Throws if the view or parent buffer has been disposed.

protected void ThrowIfDisposed()