Class IUnifiedMemoryBufferExtensions
- Namespace
- DotCompute.Core.Extensions
- Assembly
- DotCompute.Core.dll
Extension methods for IUnifiedMemoryBuffer to provide missing methods and backward compatibility for tests and legacy code.
public static class IUnifiedMemoryBufferExtensions
- Inheritance
-
IUnifiedMemoryBufferExtensions
- Inherited Members
Methods
CopyFromAsync<T>(IUnifiedMemoryBuffer<T>, ReadOnlyMemory<T>, long, CancellationToken)
Simplified generic copy from operation for same-type transfers. This method handles the common case where buffer and source have the same type.
public static ValueTask CopyFromAsync<T>(this IUnifiedMemoryBuffer<T> buffer, ReadOnlyMemory<T> source, long offsetInBytes, CancellationToken cancellationToken = default) where T : unmanaged
Parameters
bufferIUnifiedMemoryBuffer<T>The target buffer.
sourceReadOnlyMemory<T>The source memory to copy from.
offsetInByteslongThe byte offset in the target buffer.
cancellationTokenCancellationTokenCancellation token.
Returns
- ValueTask
A task representing the copy operation.
Type Parameters
TThe element type.
Exceptions
- ArgumentNullException
Thrown when buffer is null.
CopyFromAsync<TBuffer, TSource>(IUnifiedMemoryBuffer<TBuffer>, ReadOnlyMemory<TSource>, int, CancellationToken)
Generic copy from operation with element offset support. This overload uses element-based offset instead of byte-based offset.
public static ValueTask CopyFromAsync<TBuffer, TSource>(this IUnifiedMemoryBuffer<TBuffer> buffer, ReadOnlyMemory<TSource> source, int offsetInElements, CancellationToken cancellationToken = default) where TBuffer : unmanaged where TSource : unmanaged
Parameters
bufferIUnifiedMemoryBuffer<TBuffer>The target buffer.
sourceReadOnlyMemory<TSource>The source memory to copy from.
offsetInElementsintThe element offset in the target buffer.
cancellationTokenCancellationTokenCancellation token.
Returns
- ValueTask
A task representing the copy operation.
Type Parameters
TBufferThe buffer element type.
TSourceThe source element type.
Exceptions
- ArgumentNullException
Thrown when buffer is null.
- ArgumentException
Thrown when types don't match or parameters are invalid.
CopyFromAsync<TBuffer, TSource>(IUnifiedMemoryBuffer<TBuffer>, ReadOnlyMemory<TSource>, long, CancellationToken)
Generic copy from operation with offset support. This method provides the generic type parameter interface required by test code.
public static ValueTask CopyFromAsync<TBuffer, TSource>(this IUnifiedMemoryBuffer<TBuffer> buffer, ReadOnlyMemory<TSource> source, long offsetInBytes, CancellationToken cancellationToken = default) where TBuffer : unmanaged where TSource : unmanaged
Parameters
bufferIUnifiedMemoryBuffer<TBuffer>The target buffer.
sourceReadOnlyMemory<TSource>The source memory to copy from.
offsetInByteslongThe byte offset in the target buffer.
cancellationTokenCancellationTokenCancellation token.
Returns
- ValueTask
A task representing the copy operation.
Type Parameters
TBufferThe buffer element type.
TSourceThe source element type.
Exceptions
- ArgumentNullException
Thrown when buffer is null.
- ArgumentException
Thrown when types don't match or parameters are invalid.
ElementCount<T>(IUnifiedMemoryBuffer<T>)
Gets the element count for a typed memory buffer. Calculates based on the buffer size and element size.
public static long ElementCount<T>(this IUnifiedMemoryBuffer<T> buffer) where T : unmanaged
Parameters
bufferIUnifiedMemoryBuffer<T>The buffer to get element count for.
Returns
- long
The number of elements in the buffer.
Type Parameters
TThe unmanaged element type.
Exceptions
- ArgumentNullException
Thrown when buffer is null.
EnsureReadyForDeviceOperationAsync<T>(IUnifiedMemoryBuffer<T>, CancellationToken)
Ensures buffer is in the correct state for device operations. This method provides thread-safe synchronization commonly needed in tests.
public static ValueTask EnsureReadyForDeviceOperationAsync<T>(this IUnifiedMemoryBuffer<T> buffer, CancellationToken cancellationToken = default) where T : unmanaged
Parameters
bufferIUnifiedMemoryBuffer<T>The buffer to prepare.
cancellationTokenCancellationTokenCancellation token.
Returns
- ValueTask
A task representing the preparation operation.
Type Parameters
TThe buffer element type.
Exceptions
- ArgumentNullException
Thrown when buffer is null.
EnsureReadyForHostOperationAsync<T>(IUnifiedMemoryBuffer<T>, CancellationToken)
Ensures buffer is in the correct state for host operations. This method provides thread-safe synchronization commonly needed in tests.
public static ValueTask EnsureReadyForHostOperationAsync<T>(this IUnifiedMemoryBuffer<T> buffer, CancellationToken cancellationToken = default) where T : unmanaged
Parameters
bufferIUnifiedMemoryBuffer<T>The buffer to prepare.
cancellationTokenCancellationTokenCancellation token.
Returns
- ValueTask
A task representing the preparation operation.
Type Parameters
TThe buffer element type.
Exceptions
- ArgumentNullException
Thrown when buffer is null.
GetDevicePointer<T>(IUnifiedMemoryBuffer<T>)
Gets the device pointer if available for direct access patterns. This property is commonly used in low-level operations.
public static nint GetDevicePointer<T>(this IUnifiedMemoryBuffer<T> buffer) where T : unmanaged
Parameters
bufferIUnifiedMemoryBuffer<T>The buffer to get device pointer from.
Returns
- nint
The device pointer, or IntPtr.Zero if not available.
Type Parameters
TThe buffer element type.
Exceptions
- ArgumentNullException
Thrown when buffer is null.
GetMetrics<T>(IUnifiedMemoryBuffer<T>)
Gets performance metrics for the buffer if available. This method provides insights commonly needed in performance tests.
public static BufferMetrics? GetMetrics<T>(this IUnifiedMemoryBuffer<T> buffer) where T : unmanaged
Parameters
bufferIUnifiedMemoryBuffer<T>The buffer to get metrics for.
Returns
- BufferMetrics
Buffer metrics or null if not available.
Type Parameters
TThe buffer element type.
Exceptions
- ArgumentNullException
Thrown when buffer is null.
GetReadOnlySpan<T>(IUnifiedMemoryBuffer<T>)
Gets a read-only span to the host memory. Will trigger transfer if needed. This method is an alias for AsReadOnlySpan() for backward compatibility.
public static ReadOnlySpan<T> GetReadOnlySpan<T>(this IUnifiedMemoryBuffer<T> buffer) where T : unmanaged
Parameters
bufferIUnifiedMemoryBuffer<T>The buffer to get read-only span from.
Returns
- ReadOnlySpan<T>
A read-only span to the host memory.
Type Parameters
TThe buffer element type.
Exceptions
- ArgumentNullException
Thrown when buffer is null.
GetSpan<T>(IUnifiedMemoryBuffer<T>)
Gets a span to the host memory. Will trigger transfer if needed. This method is an alias for AsSpan() for backward compatibility.
public static Span<T> GetSpan<T>(this IUnifiedMemoryBuffer<T> buffer) where T : unmanaged
Parameters
bufferIUnifiedMemoryBuffer<T>The buffer to get span from.
Returns
- Span<T>
A span to the host memory.
Type Parameters
TThe buffer element type.
Exceptions
- ArgumentNullException
Thrown when buffer is null.
IsUnifiedMemory<T>(IUnifiedMemoryBuffer<T>)
Gets whether the buffer supports unified memory access. This property is commonly referenced in tests.
public static bool IsUnifiedMemory<T>(this IUnifiedMemoryBuffer<T> buffer) where T : unmanaged
Parameters
bufferIUnifiedMemoryBuffer<T>The buffer to check.
Returns
- bool
True if the buffer supports unified memory, false otherwise.
Type Parameters
TThe buffer element type.
Exceptions
- ArgumentNullException
Thrown when buffer is null.
ReadAsync<T>(IUnifiedMemoryBuffer<T>, Memory<T>, int, CancellationToken)
Reads data from the buffer asynchronously into a memory location. This matches the expected test signature.
public static ValueTask ReadAsync<T>(this IUnifiedMemoryBuffer<T> buffer, Memory<T> destination, int offset, CancellationToken cancellationToken = default) where T : unmanaged
Parameters
bufferIUnifiedMemoryBuffer<T>destinationMemory<T>offsetintcancellationTokenCancellationToken
Returns
Type Parameters
T
ReadAsync<T>(IUnifiedMemoryBuffer<T>, Span<T>, int)
Async wrapper for ReadAsync that delegates to synchronous span operations. This supports the test pattern of ReadAsync(array.AsSpan(), offset).
public static ValueTask ReadAsync<T>(this IUnifiedMemoryBuffer<T> buffer, Span<T> destination, int offset) where T : unmanaged
Parameters
bufferIUnifiedMemoryBuffer<T>destinationSpan<T>offsetint
Returns
Type Parameters
T
ReadAsync<T>(IUnifiedMemoryBuffer<T>, CancellationToken)
Reads data from the buffer asynchronously. This is a compatibility method that maps to existing buffer operations.
public static ValueTask<T[]> ReadAsync<T>(this IUnifiedMemoryBuffer<T> buffer, CancellationToken cancellationToken = default) where T : unmanaged
Parameters
bufferIUnifiedMemoryBuffer<T>cancellationTokenCancellationToken
Returns
- ValueTask<T[]>
Type Parameters
T
ReadAsync<T>(IUnifiedMemoryBuffer<T>, T[], int, CancellationToken)
Reads data from the buffer asynchronously into a span via array parameter. This matches the expected test signature pattern.
public static ValueTask ReadAsync<T>(this IUnifiedMemoryBuffer<T> buffer, T[] destination, int offset, CancellationToken cancellationToken = default) where T : unmanaged
Parameters
bufferIUnifiedMemoryBuffer<T>destinationT[]offsetintcancellationTokenCancellationToken
Returns
Type Parameters
T
Read<T>(IUnifiedMemoryBuffer<T>, Span<T>, int)
Reads data from the buffer into a Span at the specified offset. This handles span parameters by being non-async.
public static void Read<T>(this IUnifiedMemoryBuffer<T> buffer, Span<T> destination, int offset) where T : unmanaged
Parameters
bufferIUnifiedMemoryBuffer<T>destinationSpan<T>offsetint
Type Parameters
T
ToSafeMemory<T>(IUnifiedMemoryBuffer<T>)
Creates a safe view of the buffer that handles disposal automatically. This pattern is commonly used in test cleanup scenarios.
public static Memory<T> ToSafeMemory<T>(this IUnifiedMemoryBuffer<T> buffer) where T : unmanaged
Parameters
bufferIUnifiedMemoryBuffer<T>The source buffer.
Returns
- Memory<T>
A memory view that is safe to use beyond buffer lifetime.
Type Parameters
TThe buffer element type.
Exceptions
- ArgumentNullException
Thrown when buffer is null.
ValidateBuffer<T>(IUnifiedMemoryBuffer<T>, string)
Validates buffer state before operations. Common pattern used in test validation.
public static void ValidateBuffer<T>(this IUnifiedMemoryBuffer<T> buffer, string operationName = "operation") where T : unmanaged
Parameters
bufferIUnifiedMemoryBuffer<T>The buffer to validate.
operationNamestringThe name of the operation being performed.
Type Parameters
TThe buffer element type.
Exceptions
- ArgumentNullException
Thrown when buffer is null.
- ObjectDisposedException
Thrown when buffer is disposed.
WriteAsync<T>(IUnifiedMemoryBuffer<T>, ReadOnlyMemory<T>, int, CancellationToken)
Writes data to the buffer asynchronously with memory input. This matches the expected test signature.
public static ValueTask WriteAsync<T>(this IUnifiedMemoryBuffer<T> buffer, ReadOnlyMemory<T> data, int offset, CancellationToken cancellationToken = default) where T : unmanaged
Parameters
bufferIUnifiedMemoryBuffer<T>dataReadOnlyMemory<T>offsetintcancellationTokenCancellationToken
Returns
Type Parameters
T
WriteAsync<T>(IUnifiedMemoryBuffer<T>, ReadOnlySpan<T>, int)
Async wrapper for WriteAsync that delegates to synchronous span operations. This supports the test pattern of WriteAsync(array.AsSpan(), offset).
public static ValueTask WriteAsync<T>(this IUnifiedMemoryBuffer<T> buffer, ReadOnlySpan<T> data, int offset) where T : unmanaged
Parameters
bufferIUnifiedMemoryBuffer<T>dataReadOnlySpan<T>offsetint
Returns
Type Parameters
T
WriteAsync<T>(IUnifiedMemoryBuffer<T>, T[], int, CancellationToken)
Writes data to the buffer asynchronously from array parameter. This supports the test pattern of WriteAsync(array.AsSpan(), offset).
public static ValueTask WriteAsync<T>(this IUnifiedMemoryBuffer<T> buffer, T[] data, int offset, CancellationToken cancellationToken = default) where T : unmanaged
Parameters
bufferIUnifiedMemoryBuffer<T>dataT[]offsetintcancellationTokenCancellationToken
Returns
Type Parameters
T
WriteAsync<T>(IUnifiedMemoryBuffer<T>, T[], CancellationToken)
Writes data to the buffer asynchronously. This is a compatibility method that maps to existing buffer operations.
public static ValueTask WriteAsync<T>(this IUnifiedMemoryBuffer<T> buffer, T[] data, CancellationToken cancellationToken = default) where T : unmanaged
Parameters
bufferIUnifiedMemoryBuffer<T>dataT[]cancellationTokenCancellationToken
Returns
Type Parameters
T
Write<T>(IUnifiedMemoryBuffer<T>, ReadOnlySpan<T>, int)
Writes data to the buffer from a ReadOnlySpan at the specified offset. This handles span parameters by being non-async.
public static void Write<T>(this IUnifiedMemoryBuffer<T> buffer, ReadOnlySpan<T> data, int offset) where T : unmanaged
Parameters
bufferIUnifiedMemoryBuffer<T>dataReadOnlySpan<T>offsetint
Type Parameters
T