Table of Contents

Class OptimizedUnifiedBuffer<T>

Namespace
DotCompute.Memory
Assembly
DotCompute.Memory.dll

Performance-optimized unified buffer with advanced memory management patterns:

  • Object pooling for frequent allocations (90% reduction target)
  • Lazy initialization for expensive operations
  • Zero-copy operations using Span<T> and Memory<T>
  • Async-first design with optimized synchronization
  • Memory prefetching for improved cache performance
  • NUMA-aware memory allocation
public sealed class OptimizedUnifiedBuffer<T> : IUnifiedMemoryBuffer<T>, IUnifiedMemoryBuffer, IAsyncDisposable, IDisposable where T : unmanaged

Type Parameters

T

The element type.

Inheritance
OptimizedUnifiedBuffer<T>
Implements
Inherited Members
Extension Methods

Constructors

OptimizedUnifiedBuffer(IUnifiedMemoryManager, int, ObjectPool<T[]>?)

Initializes a new optimized unified buffer.

public OptimizedUnifiedBuffer(IUnifiedMemoryManager memoryManager, int length, ObjectPool<T[]>? arrayPool = null)

Parameters

memoryManager IUnifiedMemoryManager

The memory manager to use for device operations.

length int

The length of the buffer in elements.

arrayPool ObjectPool<T[]>

Optional array pool for reusing allocations.

OptimizedUnifiedBuffer(IUnifiedMemoryManager, ReadOnlySpan<T>, ObjectPool<T[]>?)

Initializes a new optimized unified buffer with initial data.

public OptimizedUnifiedBuffer(IUnifiedMemoryManager memoryManager, ReadOnlySpan<T> data, ObjectPool<T[]>? arrayPool = null)

Parameters

memoryManager IUnifiedMemoryManager
data ReadOnlySpan<T>
arrayPool ObjectPool<T[]>

Properties

Accelerator

Gets the accelerator this buffer is associated with.

public IAccelerator Accelerator { get; }

Property Value

IAccelerator

DevicePointer

Gets the device pointer for this buffer.

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

PerformanceMetrics

Gets performance metrics for this buffer.

public BufferPerformanceMetrics PerformanceMetrics { get; }

Property Value

BufferPerformanceMetrics

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 handle to the host memory with zero-copy semantics.

public Memory<T> AsMemory()

Returns

Memory<T>

AsReadOnlyMemory()

Gets a read-only memory handle with zero-copy semantics.

public ReadOnlyMemory<T> AsReadOnlyMemory()

Returns

ReadOnlyMemory<T>

AsReadOnlySpan()

Gets a read-only span to the host memory with zero-copy semantics.

public ReadOnlySpan<T> AsReadOnlySpan()

Returns

ReadOnlySpan<T>

AsSpan()

Gets a span to the host memory with zero-copy semantics.

public Span<T> AsSpan()

Returns

Span<T>

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.

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.

CopyFromHostAsync<TSource>(ReadOnlyMemory<TSource>, long, CancellationToken)

Gets copy from host asynchronously.

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

Parameters

source ReadOnlyMemory<TSource>

The source.

offset long

The offset.

cancellationToken CancellationToken

The cancellation token.

Returns

ValueTask

The result of the operation.

Type Parameters

TSource

The TSource type parameter.

CopyToAsync(IUnifiedMemoryBuffer<T>, CancellationToken)

Gets copy to asynchronously.

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

Parameters

destination IUnifiedMemoryBuffer<T>

The destination.

cancellationToken CancellationToken

The cancellation token.

Returns

ValueTask

The result of the operation.

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.

CopyToHostAsync<TDestination>(Memory<TDestination>, long, CancellationToken)

Gets copy to host asynchronously.

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

Parameters

destination Memory<TDestination>

The destination.

offset long

The offset.

cancellationToken CancellationToken

The cancellation token.

Returns

ValueTask

The result of the operation.

Type Parameters

TDestination

The TDestination type parameter.

Dispose()

Performs dispose.

public void Dispose()

DisposeAsync()

Gets dispose asynchronously.

public ValueTask DisposeAsync()

Returns

ValueTask

The result of the operation.

EnsureOnDevice()

Ensures the buffer is available on the device with optimized allocation.

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 with optimized state transitions.

public void EnsureOnHost()

EnsureOnHostAsync(AcceleratorContext, CancellationToken)

Asynchronously ensures the buffer is available on the host with improved performance.

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 handle with optimized transfer patterns.

public DeviceMemory GetDeviceMemory()

Returns

DeviceMemory

GetMemory()

Gets the memory.

public Memory<T> GetMemory()

Returns

Memory<T>

The memory.

Map(MapMode)

Gets map.

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

Parameters

mode MapMode

The mode.

Returns

MappedMemory<T>

The result of the operation.

MapAsync(MapMode, CancellationToken)

Gets map asynchronously.

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

Parameters

mode MapMode

The mode.

cancellationToken CancellationToken

The cancellation token.

Returns

ValueTask<MappedMemory<T>>

The result of the operation.

MapRange(int, int, MapMode)

Gets map range.

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

Parameters

offset int

The offset.

length int

The length.

mode MapMode

The mode.

Returns

MappedMemory<T>

The result of the operation.

MarkDeviceDirty()

Performs mark device dirty.

public void MarkDeviceDirty()

MarkHostDirty()

Performs mark host dirty.

public void MarkHostDirty()

ReadAsync(int, int?, CancellationToken)

Gets read asynchronously.

public ValueTask<T[]> ReadAsync(int offset = 0, int? count = null, CancellationToken cancellationToken = default)

Parameters

offset int

The offset.

count int?

The count.

cancellationToken CancellationToken

The cancellation token.

Returns

ValueTask<T[]>

The result of the operation.

Slice(int, int)

Gets slice.

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

Parameters

offset int

The offset.

length int

The length.

Returns

IUnifiedMemoryBuffer<T>

The result of the operation.

Synchronize()

Performs synchronize.

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

WriteAsync(ReadOnlyMemory<T>, int, CancellationToken)

Gets write asynchronously.

public ValueTask WriteAsync(ReadOnlyMemory<T> data, int offset = 0, CancellationToken cancellationToken = default)

Parameters

data ReadOnlyMemory<T>

The data.

offset int

The offset.

cancellationToken CancellationToken

The cancellation token.

Returns

ValueTask

The result of the operation.

WriteAsync(T[], int, CancellationToken)

Gets write asynchronously.

public ValueTask WriteAsync(T[] data, int offset = 0, CancellationToken cancellationToken = default)

Parameters

data T[]

The data.

offset int

The offset.

cancellationToken CancellationToken

The cancellation token.

Returns

ValueTask

The result of the operation.