Class CudaMemoryBuffer<T>
- Namespace
- DotCompute.Backends.CUDA.Memory
- Assembly
- DotCompute.Backends.CUDA.dll
Represents a generic CUDA memory buffer allocated on the GPU device.
public sealed class CudaMemoryBuffer<T> : IUnifiedMemoryBuffer<T>, IUnifiedMemoryBuffer, IAsyncDisposable, IDisposable where T : unmanaged
Type Parameters
TThe element type stored in the buffer.
- Inheritance
-
CudaMemoryBuffer<T>
- Implements
- Inherited Members
- Extension Methods
Remarks
Initializes a new instance of the CudaMemoryBuffer<T> class.
Constructors
CudaMemoryBuffer(nint, long, CudaContext, MemoryOptions, IAccelerator?)
Represents a generic CUDA memory buffer allocated on the GPU device.
public CudaMemoryBuffer(nint devicePointer, long count, CudaContext context, MemoryOptions options = MemoryOptions.None, IAccelerator? accelerator = null)
Parameters
devicePointernintThe device memory pointer.
countlongThe number of elements.
contextCudaContextThe CUDA context.
optionsMemoryOptionsMemory allocation options.
acceleratorIAcceleratorOptional accelerator reference for advanced features.
Remarks
Initializes a new instance of the CudaMemoryBuffer<T> class.
Properties
Accelerator
Gets the accelerator this buffer is associated with.
public IAccelerator Accelerator { get; }
Property Value
Count
public long Count { get; }
Property Value
DevicePointer
Gets the device memory pointer.
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 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 host memory. Will trigger transfer if needed.
public Memory<T> AsMemory()
Returns
- Memory<T>
AsReadOnlyMemory()
Gets a read-only memory handle to the host memory. Will trigger transfer if needed.
public ReadOnlyMemory<T> AsReadOnlyMemory()
Returns
AsReadOnlySpan()
Gets a read-only span to the host memory. Will trigger transfer if needed.
public ReadOnlySpan<T> AsReadOnlySpan()
Returns
- ReadOnlySpan<T>
AsSpan()
Gets a span to the host memory. Will trigger transfer if needed.
public Span<T> AsSpan()
Returns
- Span<T>
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.
ClearDirty()
Clears the dirty flag and all dirty ranges.
public void ClearDirty()
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<TSource>(ReadOnlyMemory<TSource>, long, CancellationToken)
Asynchronously copies data from host memory to this buffer.
public ValueTask CopyFromAsync<TSource>(ReadOnlyMemory<TSource> source, long offset = 0, CancellationToken cancellationToken = default) where TSource : unmanaged
Parameters
sourceReadOnlyMemory<TSource>offsetlongcancellationTokenCancellationToken
Returns
Type Parameters
TSource
CopyFromHost(ReadOnlySpan<T>)
Copies data from host memory to this device buffer.
public void CopyFromHost(ReadOnlySpan<T> source)
Parameters
sourceReadOnlySpan<T>
CopyRangeTo(CudaMemoryBuffer<T>, long, long, long)
Copies a range of data from this buffer to another device buffer.
public void CopyRangeTo(CudaMemoryBuffer<T> destination, long sourceOffset, long destinationOffset, long count)
Parameters
destinationCudaMemoryBuffer<T>The destination buffer.
sourceOffsetlongThe offset in the source buffer (in elements).
destinationOffsetlongThe offset in the destination buffer (in elements).
countlongThe number of elements to copy.
CopyRangeToAsync(CudaMemoryBuffer<T>, long, long, long, nint, CancellationToken)
Asynchronously copies a range of data from this buffer to another device buffer.
public ValueTask CopyRangeToAsync(CudaMemoryBuffer<T> destination, long sourceOffset, long destinationOffset, long count, nint stream = 0, CancellationToken cancellationToken = default)
Parameters
destinationCudaMemoryBuffer<T>sourceOffsetlongdestinationOffsetlongcountlongstreamnintcancellationTokenCancellationToken
Returns
CopyTo(CudaMemoryBuffer<T>)
Copies data from this buffer to another buffer.
public void CopyTo(CudaMemoryBuffer<T> destination)
Parameters
destinationCudaMemoryBuffer<T>
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<TDest>(Memory<TDest>, long, CancellationToken)
Asynchronously copies data from this buffer to host memory.
public ValueTask CopyToAsync<TDest>(Memory<TDest> destination, long offset = 0, CancellationToken cancellationToken = default) where TDest : unmanaged
Parameters
destinationMemory<TDest>offsetlongcancellationTokenCancellationToken
Returns
Type Parameters
TDest
CopyToHost(Span<T>)
Copies data from this device buffer to host memory.
public void CopyToHost(Span<T> destination)
Parameters
destinationSpan<T>
Dispose()
Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
public void Dispose()
DisposeAsync()
Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources asynchronously.
public ValueTask DisposeAsync()
Returns
- ValueTask
A task that represents the asynchronous dispose operation.
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. Will trigger transfer if needed.
public DeviceMemory GetDeviceMemory()
Returns
GetDirtyRanges()
Gets the dirty ranges in the buffer.
public (long start, long end)[] GetDirtyRanges()
Returns
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()
MarkDirty()
Marks the entire buffer as dirty.
public void MarkDirty()
MarkHostDirty()
Marks the buffer as modified on the host.
public void MarkHostDirty()
MarkRangeDirty(long, long)
Marks a specific range of the buffer as dirty.
public void MarkRangeDirty(long start, long end)
Parameters
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.
SyncDirtyToHostAsync<TDest>(Memory<TDest>, CancellationToken)
Synchronizes only the dirty ranges from device to host.
public ValueTask SyncDirtyToHostAsync<TDest>(Memory<TDest> destination, CancellationToken cancellationToken = default) where TDest : unmanaged
Parameters
destinationMemory<TDest>cancellationTokenCancellationToken
Returns
Type Parameters
TDest
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