Table of Contents

Class UnifiedBufferView<TOriginal, TView>

Namespace
DotCompute.Memory
Assembly
DotCompute.Memory.dll

Represents a type-cast view of a UnifiedBuffer that provides access to the same memory with a different element type. This is a lightweight wrapper that doesn't own the underlying memory but reinterprets it as a different type.

public sealed class UnifiedBufferView<TOriginal, TView> : IUnifiedMemoryBuffer<TView>, IUnifiedMemoryBuffer, IAsyncDisposable, IDisposable where TOriginal : unmanaged where TView : unmanaged

Type Parameters

TOriginal

The original element type of the parent buffer.

TView

The view element type.

Inheritance
UnifiedBufferView<TOriginal, TView>
Implements
Inherited Members
Extension Methods

Constructors

UnifiedBufferView(UnifiedBuffer<TOriginal>, int)

Initializes a new instance of the UnifiedBufferView class.

public UnifiedBufferView(UnifiedBuffer<TOriginal> parentBuffer, int viewLength)

Parameters

parentBuffer UnifiedBuffer<TOriginal>

The parent buffer to create a view of.

viewLength int

The length of the view in elements of type TView.

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

Options

Gets the memory allocation options.

public MemoryOptions Options { get; }

Property Value

MemoryOptions

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 view data by casting the parent buffer's memory.

public Memory<TView> AsMemory()

Returns

Memory<TView>

A memory handle to the view data.

AsReadOnlyMemory()

Gets a read-only memory handle to the view data by casting the parent buffer's memory.

public ReadOnlyMemory<TView> AsReadOnlyMemory()

Returns

ReadOnlyMemory<TView>

A read-only memory handle to the view data.

AsReadOnlySpan()

Gets a read-only span to the view data by casting the parent buffer's memory.

public ReadOnlySpan<TView> AsReadOnlySpan()

Returns

ReadOnlySpan<TView>

A read-only span to the view data.

AsSpan()

Gets a span to the view data by casting the parent buffer's memory.

public Span<TView> AsSpan()

Returns

Span<TView>

A span to the view data.

AsType<TNew>()

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

public 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(ReadOnlyMemory<TView>, CancellationToken)

Copies data from a source memory into this buffer.

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

Parameters

source ReadOnlyMemory<TView>
cancellationToken CancellationToken

Returns

ValueTask

CopyFromAsync<U>(ReadOnlyMemory<U>, long, CancellationToken)

Copies data from a source memory into this buffer.

public ValueTask CopyFromAsync<U>(ReadOnlyMemory<U> source, long offset = 0, CancellationToken cancellationToken = default) where U : unmanaged

Parameters

source ReadOnlyMemory<U>

The source memory to copy from.

offset long

The offset in bytes where to start copying.

cancellationToken CancellationToken

Cancellation token.

Returns

ValueTask

A task representing the copy operation.

Type Parameters

U

CopyToAsync(IUnifiedMemoryBuffer<TView>, CancellationToken)

Copies data from this buffer to another buffer.

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

Parameters

destination IUnifiedMemoryBuffer<TView>
cancellationToken CancellationToken

Returns

ValueTask

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

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

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

Parameters

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

Returns

ValueTask

CopyToAsync(Memory<TView>, CancellationToken)

Copies data from this buffer to a destination memory.

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

Parameters

destination Memory<TView>
cancellationToken CancellationToken

Returns

ValueTask

CopyToAsync<U>(Memory<U>, long, CancellationToken)

Copies data from this buffer to a destination memory.

public ValueTask CopyToAsync<U>(Memory<U> destination, long offset = 0, CancellationToken cancellationToken = default) where U : unmanaged

Parameters

destination Memory<U>

The destination memory to copy to.

offset long

The offset in bytes where to start copying from.

cancellationToken CancellationToken

Cancellation token.

Returns

ValueTask

A task representing the copy operation.

Type Parameters

U

Dispose()

Disposes the view. This doesn't dispose the parent buffer, just invalidates this view.

public void Dispose()

DisposeAsync()

Asynchronously disposes the view. This doesn't dispose the parent buffer, just invalidates this view.

public ValueTask DisposeAsync()

Returns

ValueTask

EnsureOnDevice()

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

public void EnsureOnDevice()

EnsureOnDeviceAsync(AcceleratorContext, CancellationToken)

Asynchronously ensures the buffer is available on the device.

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

EnsureOnHostAsync(AcceleratorContext, CancellationToken)

Asynchronously ensures the buffer is available on the host.

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

Parameters

context AcceleratorContext
cancellationToken CancellationToken

Returns

ValueTask

FillAsync(TView, int, int, CancellationToken)

Fills a portion of this buffer with a specified value.

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

Parameters

value TView
offset int
count int
cancellationToken CancellationToken

Returns

ValueTask

FillAsync(TView, CancellationToken)

Fills this buffer with a specified value.

public ValueTask FillAsync(TView value, CancellationToken cancellationToken = default)

Parameters

value TView
cancellationToken CancellationToken

Returns

ValueTask

GetDeviceMemory()

Gets the device memory handle for this view.

public DeviceMemory GetDeviceMemory()

Returns

DeviceMemory

The device memory handle with size adjusted for the view type.

Map(MapMode)

Maps this buffer to host memory for direct access.

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

Parameters

mode MapMode

The mapping mode.

Returns

MappedMemory<TView>

A mapped memory region.

MapAsync(MapMode, CancellationToken)

Asynchronously maps this buffer to host memory.

public ValueTask<MappedMemory<TView>> MapAsync(MapMode mode = MapMode.ReadWrite, CancellationToken cancellationToken = default)

Parameters

mode MapMode

The mapping mode.

cancellationToken CancellationToken

Cancellation token.

Returns

ValueTask<MappedMemory<TView>>

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 MappedMemory<TView> 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<TView>

A mapped memory region.

MarkDeviceDirty()

Marks the buffer as modified on the device.

public void MarkDeviceDirty()

MarkHostDirty()

Marks the buffer as modified on the host.

public void MarkHostDirty()

Slice(int, int)

Creates a slice of this buffer.

public IUnifiedMemoryBuffer<TView> Slice(int offset, int length)

Parameters

offset int

The offset in elements.

length int

The length of the slice in elements.

Returns

IUnifiedMemoryBuffer<TView>

A slice of this buffer.

Synchronize()

Synchronizes the buffer state between host and device.

public void Synchronize()

SynchronizeAsync(AcceleratorContext, CancellationToken)

Asynchronously synchronizes the buffer state between host and device.

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

Parameters

context AcceleratorContext
cancellationToken CancellationToken

Returns

ValueTask