Table of Contents

Class UnifiedBuffer<T>

Namespace
DotCompute.Memory
Assembly
DotCompute.Memory.dll

Core unified buffer implementation providing basic buffer operations and properties. Handles fundamental buffer state management and access patterns.

public sealed class UnifiedBuffer<T> : IUnifiedMemoryBuffer<T>, IUnifiedMemoryBuffer, IAsyncDisposable, IDisposable where T : unmanaged

Type Parameters

T

The element type.

Inheritance
UnifiedBuffer<T>
Implements
Inherited Members
Extension Methods

Constructors

UnifiedBuffer(IUnifiedMemoryManager, int)

Initializes a new instance of the UnifiedBuffer class.

public UnifiedBuffer(IUnifiedMemoryManager memoryManager, int length)

Parameters

memoryManager IUnifiedMemoryManager

The memory manager to use for device operations.

length int

The length of the buffer in elements.

UnifiedBuffer(IUnifiedMemoryManager, ReadOnlySpan<T>)

Initializes a new instance of the UnifiedBuffer class with initial data.

public UnifiedBuffer(IUnifiedMemoryManager memoryManager, ReadOnlySpan<T> data)

Parameters

memoryManager IUnifiedMemoryManager

The memory manager to use for device operations.

data ReadOnlySpan<T>

Initial data to populate the buffer.

Properties

Accelerator

Gets the accelerator this buffer is associated with (always null for unified buffers).

public IAccelerator Accelerator { get; }

Property Value

IAccelerator

DevicePointer

Gets the device pointer for this buffer. Returns IntPtr.Zero if buffer is not on device.

public nint DevicePointer { get; }

Property Value

nint

IsDirty

Gets whether the buffer has been modified and needs synchronization.

public bool IsDirty { get; }

Property Value

bool

IsDisposed

Gets whether this 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 length of the buffer in elements.

public int Length { get; }

Property Value

int

Options

Gets the memory options for this buffer.

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 buffer state for tracking transfers.

public BufferState State { get; }

Property Value

BufferState

Methods

AsMemory()

Gets a memory representing the buffer contents on the host.

public Memory<T> AsMemory()

Returns

Memory<T>

A memory of the buffer data.

AsReadOnlyMemory()

Gets a read-only memory representing the buffer contents on the host.

public ReadOnlyMemory<T> AsReadOnlyMemory()

Returns

ReadOnlyMemory<T>

A read-only memory of the buffer data.

AsReadOnlySpan()

Gets a read-only span representing the buffer contents on the host.

public ReadOnlySpan<T> AsReadOnlySpan()

Returns

ReadOnlySpan<T>

A read-only span of the buffer data.

AsSpan()

Gets a span representing the buffer contents on the host.

public Span<T> AsSpan()

Returns

Span<T>

A span of the buffer data.

AsType<TNew>()

Gets as type.

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

Returns

IUnifiedMemoryBuffer<TNew>

The result of the operation.

Type Parameters

TNew

The TNew type parameter.

Compact()

Compacts the buffer memory by releasing device memory if not needed.

public void Compact()

CopyFrom(IUnifiedMemoryBuffer<T>)

Copies data from another buffer to this buffer.

public void CopyFrom(IUnifiedMemoryBuffer<T> source)

Parameters

source IUnifiedMemoryBuffer<T>

Source buffer to copy from.

CopyFromAsync(ReadOnlyMemory<T>, CancellationToken)

Gets copy from asynchronously.

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

Parameters

source ReadOnlyMemory<T>

The source.

cancellationToken CancellationToken

The cancellation token.

Returns

ValueTask

The result of the operation.

CopyToAsync(IUnifiedMemoryBuffer<T>, CancellationToken)

Copies data to another buffer from this buffer.

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

Parameters

destination IUnifiedMemoryBuffer<T>

Destination buffer to copy to.

cancellationToken CancellationToken

Token to cancel the operation.

Returns

ValueTask

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

Gets copy to asynchronously.

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

Parameters

sourceOffset int

The source offset.

destination IUnifiedMemoryBuffer<T>

The destination.

destinationOffset int

The destination offset.

count int

The count.

cancellationToken CancellationToken

The cancellation token.

Returns

ValueTask

The result of the operation.

CopyToAsync(Memory<T>, CancellationToken)

Gets copy to asynchronously.

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

Parameters

destination Memory<T>

The destination.

cancellationToken CancellationToken

The cancellation token.

Returns

ValueTask

The result of the operation.

CreateSnapshot()

Creates a snapshot of the current buffer state.

public BufferSnapshot CreateSnapshot()

Returns

BufferSnapshot

A snapshot containing current buffer information.

Dispose()

Disposes the buffer and releases all resources.

public void Dispose()

DisposeAsync()

Asynchronously disposes the buffer and releases all resources.

public ValueTask DisposeAsync()

Returns

ValueTask

EnsureOnDevice()

Ensures the buffer data is available on the device.

public void EnsureOnDevice()

EnsureOnDeviceAsync()

Asynchronously ensures the buffer data is available on the device.

public ValueTask EnsureOnDeviceAsync()

Returns

ValueTask

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 data is available on the host.

public void EnsureOnHost()

EnsureOnHostAsync()

Asynchronously ensures the buffer data is available on the host.

public ValueTask EnsureOnHostAsync()

Returns

ValueTask

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(T, int, int, CancellationToken)

Gets fill asynchronously.

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

Parameters

value T

The value.

offset int

The offset.

count int

The count.

cancellationToken CancellationToken

The cancellation token.

Returns

ValueTask

The result of the operation.

FillAsync(T, CancellationToken)

Gets fill asynchronously.

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

Parameters

value T

The value.

cancellationToken CancellationToken

The cancellation token.

Returns

ValueTask

The result of the operation.

GetDeviceMemory()

Gets the device memory representation of this buffer.

public DeviceMemory GetDeviceMemory()

Returns

DeviceMemory

Device memory handle.

GetDiagnosticInfo()

Gets detailed diagnostic information about the buffer.

public BufferDiagnosticInfo GetDiagnosticInfo()

Returns

BufferDiagnosticInfo

Comprehensive buffer diagnostic information.

GetHostPointer()

Gets the host pointer for pinned memory access.

public nint GetHostPointer()

Returns

nint

Pointer to pinned host memory.

GetMemoryInfo()

Gets memory usage statistics for this buffer.

public BufferMemoryInfo GetMemoryInfo()

Returns

BufferMemoryInfo

Memory usage information.

GetTransferStats()

Gets transfer statistics for this buffer.

public BufferTransferStats GetTransferStats()

Returns

BufferTransferStats

Transfer performance statistics.

InvalidateDevice()

Invalidates the device copy, marking the host as the authoritative source.

public void InvalidateDevice()

InvalidateHost()

Invalidates the host copy, marking the device as the authoritative source.

public void InvalidateHost()

Map(MapMode)

Maps this buffer to host memory for direct access.

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

Parameters

mode MapMode

Returns

MappedMemory<T>

MapAsync(MapMode, CancellationToken)

Asynchronously maps this buffer to host memory.

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

Parameters

mode MapMode
cancellationToken CancellationToken

Returns

ValueTask<MappedMemory<T>>

MapRange(int, int, MapMode)

Maps a portion of this buffer to host memory for direct access.

public MappedMemory<T> MapRange(int offset, int length, MapMode mode = MapMode.ReadWrite)

Parameters

offset int
length int
mode MapMode

Returns

MappedMemory<T>

MarkDeviceDirty()

Performs mark device dirty.

public void MarkDeviceDirty()

MarkHostDirty()

Performs mark host dirty.

public void MarkHostDirty()

PerformDeepValidation()

Performs a deep validation of buffer consistency.

public BufferValidationResult PerformDeepValidation()

Returns

BufferValidationResult

Detailed validation results.

PrefetchToDeviceAsync()

Prefetches the buffer to device memory for optimal performance.

public Task PrefetchToDeviceAsync()

Returns

Task

PrefetchToHostAsync()

Prefetches the buffer to host memory for optimal performance.

public Task PrefetchToHostAsync()

Returns

Task

ResetTransferStats()

Resets transfer statistics.

public void ResetTransferStats()

Resize(int)

Resizes the buffer to a new length. This operation will invalidate existing data.

public void Resize(int newLength)

Parameters

newLength int

New length in elements.

Slice(int, int)

Creates a slice of this buffer.

public IUnifiedMemoryBuffer<T> Slice(int start, int length)

Parameters

start int

Start index of the slice.

length int

Length of the slice.

Returns

IUnifiedMemoryBuffer<T>

A new buffer representing the slice.

Synchronize()

Synchronizes the buffer so that both host and device have the same data.

public void Synchronize()

SynchronizeAsync(AcceleratorContext, CancellationToken)

Asynchronously synchronizes the buffer so that both host and device have the same data.

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

Parameters

context AcceleratorContext
cancellationToken CancellationToken

Returns

ValueTask

ValidateIntegrity()

Validates the integrity of the buffer data.

public bool ValidateIntegrity()

Returns

bool

True if buffer data is consistent, false otherwise.