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
TThe 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
memoryManagerIUnifiedMemoryManagerThe memory manager to use for device operations.
lengthintThe 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
memoryManagerIUnifiedMemoryManagerThe memory manager to use for device operations.
dataReadOnlySpan<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
DevicePointer
Gets the device pointer for this buffer. Returns IntPtr.Zero if buffer is not on device.
public nint DevicePointer { get; }
Property Value
IsDirty
Gets whether the buffer has been modified and needs synchronization.
public bool IsDirty { get; }
Property Value
IsDisposed
Gets whether this buffer has been disposed.
public bool IsDisposed { get; }
Property Value
IsOnDevice
Gets whether the buffer is currently available on the device.
public bool IsOnDevice { get; }
Property Value
IsOnHost
Gets whether the buffer is currently available on the host.
public bool IsOnHost { get; }
Property Value
Length
Gets the length of the buffer in elements.
public int Length { get; }
Property Value
Options
Gets the memory options for this buffer.
public MemoryOptions Options { get; }
Property Value
SizeInBytes
Gets the size of the buffer in bytes.
public long SizeInBytes { get; }
Property Value
State
Gets the buffer state for tracking transfers.
public BufferState State { get; }
Property Value
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
TNewThe 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
sourceIUnifiedMemoryBuffer<T>Source buffer to copy from.
CopyFromAsync(ReadOnlyMemory<T>, CancellationToken)
Gets copy from asynchronously.
public ValueTask CopyFromAsync(ReadOnlyMemory<T> source, CancellationToken cancellationToken = default)
Parameters
sourceReadOnlyMemory<T>The source.
cancellationTokenCancellationTokenThe 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
destinationIUnifiedMemoryBuffer<T>Destination buffer to copy to.
cancellationTokenCancellationTokenToken to cancel the operation.
Returns
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
sourceOffsetintThe source offset.
destinationIUnifiedMemoryBuffer<T>The destination.
destinationOffsetintThe destination offset.
countintThe count.
cancellationTokenCancellationTokenThe 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
destinationMemory<T>The destination.
cancellationTokenCancellationTokenThe 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
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
EnsureOnDeviceAsync(AcceleratorContext, CancellationToken)
Asynchronously ensures the buffer is available on the device.
public ValueTask EnsureOnDeviceAsync(AcceleratorContext context = default, CancellationToken cancellationToken = default)
Parameters
contextAcceleratorContextcancellationTokenCancellationToken
Returns
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
EnsureOnHostAsync(AcceleratorContext, CancellationToken)
Asynchronously ensures the buffer is available on the host.
public ValueTask EnsureOnHostAsync(AcceleratorContext context = default, CancellationToken cancellationToken = default)
Parameters
contextAcceleratorContextcancellationTokenCancellationToken
Returns
FillAsync(T, int, int, CancellationToken)
Gets fill asynchronously.
public ValueTask FillAsync(T value, int offset, int count, CancellationToken cancellationToken = default)
Parameters
valueTThe value.
offsetintThe offset.
countintThe count.
cancellationTokenCancellationTokenThe cancellation token.
Returns
- ValueTask
The result of the operation.
FillAsync(T, CancellationToken)
Gets fill asynchronously.
public ValueTask FillAsync(T value, CancellationToken cancellationToken = default)
Parameters
valueTThe value.
cancellationTokenCancellationTokenThe 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
modeMapMode
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
modeMapModecancellationTokenCancellationToken
Returns
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
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
PrefetchToHostAsync()
Prefetches the buffer to host memory for optimal performance.
public Task PrefetchToHostAsync()
Returns
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
newLengthintNew length in elements.
Slice(int, int)
Creates a slice of this buffer.
public IUnifiedMemoryBuffer<T> Slice(int start, int length)
Parameters
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
contextAcceleratorContextcancellationTokenCancellationToken
Returns
ValidateIntegrity()
Validates the integrity of the buffer data.
public bool ValidateIntegrity()
Returns
- bool
True if buffer data is consistent, false otherwise.