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
bufferIUnifiedMemoryBuffer<T>The buffer to read from.
offsetintThe offset to start reading from.
countintThe number of elements to read.
cancellationTokenCancellationTokenOptional cancellation token.
Returns
- ValueTask<T[]>
A task that returns an array containing the requested portion of the buffer's data.
Type Parameters
TThe unmanaged element type.
Exceptions
- ArgumentNullException
Thrown when
bufferis 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
bufferIUnifiedMemoryBuffer<T>The buffer to read from.
indexintThe index to read from.
cancellationTokenCancellationTokenOptional cancellation token.
Returns
- ValueTask<T>
A task that returns the value at the specified index.
Type Parameters
TThe unmanaged element type.
Remarks
This method ensures the buffer is on the host and reads the single value.
Exceptions
- ArgumentNullException
Thrown when
bufferis 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
bufferIUnifiedMemoryBuffer<T>The buffer to read from.
destinationMemory<T>The destination memory to read into.
cancellationTokenCancellationTokenOptional cancellation token.
Returns
- ValueTask
A task representing the asynchronous read operation.
Type Parameters
TThe 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
bufferis 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
bufferIUnifiedMemoryBuffer<T>The buffer to read from.
cancellationTokenCancellationTokenOptional cancellation token.
Returns
- ValueTask<T[]>
A task that returns an array containing the buffer's data.
Type Parameters
TThe 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
bufferis 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
bufferIUnifiedMemoryBuffer<T>The buffer to write to.
indexintThe index to write at.
valueTThe value to write.
cancellationTokenCancellationTokenOptional cancellation token.
Returns
- ValueTask
A task representing the asynchronous write operation.
Type Parameters
TThe 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
bufferis 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
bufferIUnifiedMemoryBuffer<T>The buffer to write to.
sourceReadOnlyMemory<T>The source data to write.
cancellationTokenCancellationTokenOptional cancellation token.
Returns
- ValueTask
A task representing the asynchronous write operation.
Type Parameters
TThe 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
bufferis null.- ArgumentException
Thrown when source length exceeds buffer capacity.