Table of Contents

Class UnifiedMemoryBufferExtensions

Namespace
DotCompute.Abstractions.Extensions
Assembly
DotCompute.Abstractions.dll

Extension methods for IUnifiedMemoryBuffer<T> providing convenient read/write operations.

public static class UnifiedMemoryBufferExtensions
Inheritance
UnifiedMemoryBufferExtensions
Inherited Members

Methods

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

Reads a portion of the buffer into a new array.

public static ValueTask<T[]> ReadAsync<T>(this IUnifiedMemoryBuffer<T> buffer, int offset, int count, CancellationToken cancellationToken = default) where T : unmanaged

Parameters

buffer IUnifiedMemoryBuffer<T>

The buffer to read from.

offset int

The offset to start reading from.

count int

The number of elements to read.

cancellationToken CancellationToken

Optional cancellation token.

Returns

ValueTask<T[]>

A task that returns an array containing the requested portion of the buffer's data.

Type Parameters

T

The unmanaged element type.

Exceptions

ArgumentNullException

Thrown when buffer is null.

ArgumentOutOfRangeException

Thrown when offset or count is out of range.

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

Reads a single value from the buffer at the specified index.

public static ValueTask<T> ReadAsync<T>(this IUnifiedMemoryBuffer<T> buffer, int index, CancellationToken cancellationToken = default) where T : unmanaged

Parameters

buffer IUnifiedMemoryBuffer<T>

The buffer to read from.

index int

The index to read from.

cancellationToken CancellationToken

Optional cancellation token.

Returns

ValueTask<T>

A task that returns the value at the specified index.

Type Parameters

T

The unmanaged element type.

Remarks

This method ensures the buffer is on the host and reads the single value.

Exceptions

ArgumentNullException

Thrown when buffer is null.

ArgumentOutOfRangeException

Thrown when index is out of range.

ReadAsync<T>(IUnifiedMemoryBuffer<T>, Memory<T>, CancellationToken)

Reads data from the buffer into a pre-allocated destination memory.

public static ValueTask ReadAsync<T>(this IUnifiedMemoryBuffer<T> buffer, Memory<T> destination, CancellationToken cancellationToken = default) where T : unmanaged

Parameters

buffer IUnifiedMemoryBuffer<T>

The buffer to read from.

destination Memory<T>

The destination memory to read into.

cancellationToken CancellationToken

Optional cancellation token.

Returns

ValueTask

A task representing the asynchronous read operation.

Type Parameters

T

The unmanaged element type.

Examples

var data = new float[buffer.Length];
await buffer.ReadAsync(data.AsMemory());

Remarks

This overload is more efficient than ReadAsync<T>(IUnifiedMemoryBuffer<T>, CancellationToken) when you have a pre-allocated destination array, as it avoids an additional allocation.

Exceptions

ArgumentNullException

Thrown when buffer is null.

ArgumentException

Thrown when destination is smaller than buffer length.

ReadAsync<T>(IUnifiedMemoryBuffer<T>, CancellationToken)

Reads all data from the buffer into a new array (convenience wrapper for CopyToAsync).

public static ValueTask<T[]> ReadAsync<T>(this IUnifiedMemoryBuffer<T> buffer, CancellationToken cancellationToken = default) where T : unmanaged

Parameters

buffer IUnifiedMemoryBuffer<T>

The buffer to read from.

cancellationToken CancellationToken

Optional cancellation token.

Returns

ValueTask<T[]>

A task that returns an array containing the buffer's data.

Type Parameters

T

The unmanaged element type.

Examples

var data = await buffer.ReadAsync();
Console.WriteLine($"Read {data.Length} elements");

Remarks

This is a convenience method that allocates a new array and calls CopyToAsync internally. Use this when you want clearer intent in code ("read from buffer" vs "copy to destination"). For better performance with pre-allocated arrays, use ReadAsync<T>(IUnifiedMemoryBuffer<T>, Memory<T>, CancellationToken) instead.

Exceptions

ArgumentNullException

Thrown when buffer is null.

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

Writes a single value to the buffer at the specified index.

public static ValueTask WriteAsync<T>(this IUnifiedMemoryBuffer<T> buffer, int index, T value, CancellationToken cancellationToken = default) where T : unmanaged

Parameters

buffer IUnifiedMemoryBuffer<T>

The buffer to write to.

index int

The index to write at.

value T

The value to write.

cancellationToken CancellationToken

Optional cancellation token.

Returns

ValueTask

A task representing the asynchronous write operation.

Type Parameters

T

The unmanaged element type.

Remarks

This method ensures the buffer is on the host, writes the single value, and marks the buffer as dirty.

Exceptions

ArgumentNullException

Thrown when buffer is null.

ArgumentOutOfRangeException

Thrown when index is out of range.

WriteAsync<T>(IUnifiedMemoryBuffer<T>, ReadOnlyMemory<T>, CancellationToken)

Writes data from a source memory to the buffer (convenience wrapper for CopyFromAsync(ReadOnlyMemory<T>, CancellationToken)).

public static ValueTask WriteAsync<T>(this IUnifiedMemoryBuffer<T> buffer, ReadOnlyMemory<T> source, CancellationToken cancellationToken = default) where T : unmanaged

Parameters

buffer IUnifiedMemoryBuffer<T>

The buffer to write to.

source ReadOnlyMemory<T>

The source data to write.

cancellationToken CancellationToken

Optional cancellation token.

Returns

ValueTask

A task representing the asynchronous write operation.

Type Parameters

T

The unmanaged element type.

Examples

var data = new float[] { 1.0f, 2.0f, 3.0f };
await buffer.WriteAsync(data.AsMemory());

Remarks

This is a convenience method that calls CopyFromAsync(ReadOnlyMemory<T>, CancellationToken) internally. Use this when you want clearer intent in code ("write to buffer" vs "copy from source").

Exceptions

ArgumentNullException

Thrown when buffer is null.

ArgumentException

Thrown when source length exceeds buffer capacity.