Class UnifiedBufferSlice<T>
- Namespace
- DotCompute.Memory
- Assembly
- DotCompute.Memory.dll
Represents a slice view of a UnifiedBuffer that provides access to a contiguous subset of elements. This is a lightweight wrapper that doesn't own the underlying memory.
public sealed class UnifiedBufferSlice<T> : IUnifiedMemoryBuffer<T>, IUnifiedMemoryBuffer, IAsyncDisposable, IDisposable where T : unmanaged
Type Parameters
TThe element type.
- Inheritance
-
UnifiedBufferSlice<T>
- Implements
- Inherited Members
- Extension Methods
Constructors
UnifiedBufferSlice(UnifiedBuffer<T>, int, int)
Initializes a new instance of the UnifiedBufferSlice class.
public UnifiedBufferSlice(UnifiedBuffer<T> parentBuffer, int offset, int length)
Parameters
parentBufferUnifiedBuffer<T>The parent buffer to slice.
offsetintThe offset in elements from the start of the parent buffer.
lengthintThe length of the slice in elements.
Properties
Accelerator
Gets the accelerator this buffer is associated with.
public IAccelerator Accelerator { get; }
Property Value
IsDirty
Gets whether the buffer has been modified and needs synchronization.
public bool IsDirty { get; }
Property Value
IsDisposed
Gets whether the 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 number of elements in the buffer.
public int Length { get; }
Property Value
Options
Gets the memory allocation options.
public MemoryOptions Options { get; }
Property Value
SizeInBytes
Gets the size of the buffer in bytes.
public long SizeInBytes { get; }
Property Value
State
Gets the current state of the buffer.
public BufferState State { get; }
Property Value
Methods
AsMemory()
Gets a memory handle to the slice portion of the parent buffer's host memory.
public Memory<T> AsMemory()
Returns
- Memory<T>
A memory handle to the slice data.
AsReadOnlyMemory()
Gets a read-only memory handle to the slice portion of the parent buffer's host memory.
public ReadOnlyMemory<T> AsReadOnlyMemory()
Returns
- ReadOnlyMemory<T>
A read-only memory handle to the slice data.
AsReadOnlySpan()
Gets a read-only span to the slice portion of the parent buffer's host memory.
public ReadOnlySpan<T> AsReadOnlySpan()
Returns
- ReadOnlySpan<T>
A read-only span to the slice data.
AsSpan()
Gets a span to the slice portion of the parent buffer's host memory.
public Span<T> AsSpan()
Returns
- Span<T>
A span to the slice data.
AsType<TNew>()
Creates a view of this buffer with a different element type.
public IUnifiedMemoryBuffer<TNew> AsType<TNew>() where TNew : unmanaged
Returns
- IUnifiedMemoryBuffer<TNew>
A view of this buffer as the new type.
Type Parameters
TNewThe new element type.
CopyFromAsync(ReadOnlyMemory<T>, CancellationToken)
Copies data from a source memory into this buffer.
public ValueTask CopyFromAsync(ReadOnlyMemory<T> source, CancellationToken cancellationToken = default)
Parameters
sourceReadOnlyMemory<T>cancellationTokenCancellationToken
Returns
CopyFromAsync<U>(ReadOnlyMemory<U>, long, CancellationToken)
Copies data from a source memory into this buffer.
public ValueTask CopyFromAsync<U>(ReadOnlyMemory<U> source, long offset = 0, CancellationToken cancellationToken = default) where U : unmanaged
Parameters
sourceReadOnlyMemory<U>The source memory to copy from.
offsetlongThe offset in bytes where to start copying.
cancellationTokenCancellationTokenCancellation token.
Returns
- ValueTask
A task representing the copy operation.
Type Parameters
U
CopyToAsync(IUnifiedMemoryBuffer<T>, CancellationToken)
Copies data from this buffer to another buffer.
public ValueTask CopyToAsync(IUnifiedMemoryBuffer<T> destination, CancellationToken cancellationToken = default)
Parameters
destinationIUnifiedMemoryBuffer<T>cancellationTokenCancellationToken
Returns
CopyToAsync(int, IUnifiedMemoryBuffer<T>, int, int, CancellationToken)
Copies data from this buffer to another buffer with specified ranges.
public ValueTask CopyToAsync(int sourceOffset, IUnifiedMemoryBuffer<T> destination, int destinationOffset, int count, CancellationToken cancellationToken = default)
Parameters
sourceOffsetintdestinationIUnifiedMemoryBuffer<T>destinationOffsetintcountintcancellationTokenCancellationToken
Returns
CopyToAsync(Memory<T>, CancellationToken)
Copies data from this buffer to a destination memory.
public ValueTask CopyToAsync(Memory<T> destination, CancellationToken cancellationToken = default)
Parameters
destinationMemory<T>cancellationTokenCancellationToken
Returns
CopyToAsync<U>(Memory<U>, long, CancellationToken)
Copies data from this buffer to a destination memory.
public ValueTask CopyToAsync<U>(Memory<U> destination, long offset = 0, CancellationToken cancellationToken = default) where U : unmanaged
Parameters
destinationMemory<U>The destination memory to copy to.
offsetlongThe offset in bytes where to start copying from.
cancellationTokenCancellationTokenCancellation token.
Returns
- ValueTask
A task representing the copy operation.
Type Parameters
U
Dispose()
Disposes the slice. This doesn't dispose the parent buffer, just invalidates this slice.
public void Dispose()
DisposeAsync()
Asynchronously disposes the slice. This doesn't dispose the parent buffer, just invalidates this slice.
public ValueTask DisposeAsync()
Returns
EnsureOnDevice()
Ensures the buffer is available on the device. Triggers transfer if needed.
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. Triggers transfer if needed.
public void EnsureOnHost()
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)
Fills a portion of this buffer with a specified value.
public ValueTask FillAsync(T value, int offset, int count, CancellationToken cancellationToken = default)
Parameters
valueToffsetintcountintcancellationTokenCancellationToken
Returns
FillAsync(T, CancellationToken)
Fills this buffer with a specified value.
public ValueTask FillAsync(T value, CancellationToken cancellationToken = default)
Parameters
valueTcancellationTokenCancellationToken
Returns
GetDeviceMemory()
Gets the device memory handle for this slice.
public DeviceMemory GetDeviceMemory()
Returns
- DeviceMemory
The device memory handle adjusted for the slice offset.
Map(MapMode)
Maps this buffer to host memory for direct access.
public MappedMemory<T> Map(MapMode mode = MapMode.ReadWrite)
Parameters
modeMapModeThe mapping mode.
Returns
- MappedMemory<T>
A mapped memory region.
MapAsync(MapMode, CancellationToken)
Asynchronously maps this buffer to host memory.
public ValueTask<MappedMemory<T>> MapAsync(MapMode mode = MapMode.ReadWrite, CancellationToken cancellationToken = default)
Parameters
modeMapModeThe mapping mode.
cancellationTokenCancellationTokenCancellation token.
Returns
- ValueTask<MappedMemory<T>>
A task that returns the mapped memory region.
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
offsetintThe offset to start mapping at.
lengthintThe number of elements to map.
modeMapModeThe mapping mode.
Returns
- MappedMemory<T>
A mapped memory region.
MarkDeviceDirty()
Marks the buffer as modified on the device.
public void MarkDeviceDirty()
MarkHostDirty()
Marks the buffer as modified on the host.
public void MarkHostDirty()
Slice(int, int)
Creates a slice of this buffer.
public IUnifiedMemoryBuffer<T> Slice(int offset, int length)
Parameters
Returns
- IUnifiedMemoryBuffer<T>
A slice of this buffer.
Synchronize()
Synchronizes the buffer state between host and device.
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