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
TThe 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
memoryManagerIUnifiedMemoryManagerThe memory manager to use for device operations.
lengthintThe length of the buffer in elements.
arrayPoolObjectPool<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
memoryManagerIUnifiedMemoryManagerdataReadOnlySpan<T>arrayPoolObjectPool<T[]>
Properties
Accelerator
Gets the accelerator this buffer is associated with.
public IAccelerator Accelerator { get; }
Property Value
DevicePointer
Gets the device pointer for this buffer.
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
PerformanceMetrics
Gets performance metrics for this buffer.
public BufferPerformanceMetrics PerformanceMetrics { 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 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
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
TNewThe TNew type parameter.
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.
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
sourceReadOnlyMemory<TSource>The source.
offsetlongThe offset.
cancellationTokenCancellationTokenThe cancellation token.
Returns
- ValueTask
The result of the operation.
Type Parameters
TSourceThe TSource type parameter.
CopyToAsync(IUnifiedMemoryBuffer<T>, CancellationToken)
Gets copy to asynchronously.
public ValueTask CopyToAsync(IUnifiedMemoryBuffer<T> destination, CancellationToken cancellationToken = default)
Parameters
destinationIUnifiedMemoryBuffer<T>The destination.
cancellationTokenCancellationTokenThe 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
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.
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
destinationMemory<TDestination>The destination.
offsetlongThe offset.
cancellationTokenCancellationTokenThe cancellation token.
Returns
- ValueTask
The result of the operation.
Type Parameters
TDestinationThe 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
contextAcceleratorContextcancellationTokenCancellationToken
Returns
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
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 handle with optimized transfer patterns.
public DeviceMemory GetDeviceMemory()
Returns
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
modeMapModeThe 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
modeMapModeThe mode.
cancellationTokenCancellationTokenThe 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
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
offsetintThe offset.
countint?The count.
cancellationTokenCancellationTokenThe cancellation token.
Returns
- ValueTask<T[]>
The result of the operation.
Slice(int, int)
Gets slice.
public IUnifiedMemoryBuffer<T> Slice(int offset, int length)
Parameters
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
contextAcceleratorContextcancellationTokenCancellationToken
Returns
WriteAsync(ReadOnlyMemory<T>, int, CancellationToken)
Gets write asynchronously.
public ValueTask WriteAsync(ReadOnlyMemory<T> data, int offset = 0, CancellationToken cancellationToken = default)
Parameters
dataReadOnlyMemory<T>The data.
offsetintThe offset.
cancellationTokenCancellationTokenThe 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
dataT[]The data.
offsetintThe offset.
cancellationTokenCancellationTokenThe cancellation token.
Returns
- ValueTask
The result of the operation.