Class P2PBuffer<T>
- Namespace
- DotCompute.Core.Memory
- Assembly
- DotCompute.Core.dll
P2P-optimized buffer that supports direct GPU-to-GPU transfers and host-mediated fallbacks. Implements type-aware transfer pipelines with proper error handling and synchronization.
public sealed class P2PBuffer<T> : IUnifiedMemoryBuffer<T>, IUnifiedMemoryBuffer, IDisposable, IAsyncDisposable where T : unmanaged
Type Parameters
T
- Inheritance
-
P2PBuffer<T>
- Implements
- Inherited Members
- Extension Methods
Remarks
Initializes a new instance of the P2PBuffer class with the specified configuration.
Constructors
P2PBuffer(IUnifiedMemoryBuffer, IAccelerator, int, bool, ILogger)
P2P-optimized buffer that supports direct GPU-to-GPU transfers and host-mediated fallbacks. Implements type-aware transfer pipelines with proper error handling and synchronization.
public P2PBuffer(IUnifiedMemoryBuffer underlyingBuffer, IAccelerator accelerator, int length, bool supportsDirectP2P, ILogger logger)
Parameters
underlyingBufferIUnifiedMemoryBufferThe underlying memory buffer that provides storage.
acceleratorIAcceleratorThe accelerator device that owns this buffer.
lengthintThe number of elements in the buffer.
supportsDirectP2PboolWhether this buffer supports direct peer-to-peer transfers.
loggerILoggerThe logger for monitoring transfer operations.
Remarks
Initializes a new instance of the P2PBuffer class with the specified configuration.
Exceptions
- ArgumentNullException
Thrown when any required parameter is null.
Properties
Accelerator
Gets the accelerator device that owns this buffer.
public IAccelerator Accelerator { get; }
Property Value
Exceptions
- ArgumentNullException
Thrown when any required parameter is null.
IsDirty
Gets whether the buffer is dirty.
public bool IsDirty { get; }
Property Value
Exceptions
- ArgumentNullException
Thrown when any required parameter is null.
IsDisposed
Gets a value indicating whether this buffer has been disposed.
public bool IsDisposed { get; }
Property Value
Exceptions
- ArgumentNullException
Thrown when any required parameter is null.
IsOnDevice
Gets whether the buffer is currently on device.
public bool IsOnDevice { get; }
Property Value
Exceptions
- ArgumentNullException
Thrown when any required parameter is null.
IsOnHost
Gets whether the buffer is currently on host.
public bool IsOnHost { get; }
Property Value
Exceptions
- ArgumentNullException
Thrown when any required parameter is null.
Length
Gets the number of elements in the buffer.
public int Length { get; }
Property Value
Exceptions
- ArgumentNullException
Thrown when any required parameter is null.
Options
Gets the memory options configured for this buffer.
public MemoryOptions Options { get; }
Property Value
Exceptions
- ArgumentNullException
Thrown when any required parameter is null.
SizeInBytes
Gets the total size of the buffer in bytes.
public long SizeInBytes { get; }
Property Value
Exceptions
- ArgumentNullException
Thrown when any required parameter is null.
State
Gets or sets the buffer state.
public BufferState State { get; }
Property Value
Exceptions
- ArgumentNullException
Thrown when any required parameter is null.
SupportsDirectP2P
Indicates if this buffer supports direct P2P transfers.
public bool SupportsDirectP2P { get; }
Property Value
Exceptions
- ArgumentNullException
Thrown when any required parameter is null.
UnderlyingBuffer
Gets the underlying memory buffer for advanced operations.
public IUnifiedMemoryBuffer UnderlyingBuffer { get; }
Property Value
Exceptions
- ArgumentNullException
Thrown when any required parameter is null.
Methods
AsMemory()
Gets as memory.
public Memory<T> AsMemory()
Returns
- Memory<T>
The result of the operation.
Exceptions
- ArgumentNullException
Thrown when any required parameter is null.
AsReadOnlyMemory()
Gets as read only memory.
public ReadOnlyMemory<T> AsReadOnlyMemory()
Returns
- ReadOnlyMemory<T>
The result of the operation.
Exceptions
- ArgumentNullException
Thrown when any required parameter is null.
AsReadOnlySpan()
Gets as read only span.
public ReadOnlySpan<T> AsReadOnlySpan()
Returns
- ReadOnlySpan<T>
The result of the operation.
Exceptions
- ArgumentNullException
Thrown when any required parameter is null.
AsSpan()
Gets as span.
public Span<T> AsSpan()
Returns
- Span<T>
The result of the operation.
Exceptions
- ArgumentNullException
Thrown when any required parameter is null.
AsType<TNew>()
Converts this buffer to a different type.
public IUnifiedMemoryBuffer<TNew> AsType<TNew>() where TNew : unmanaged
Returns
- IUnifiedMemoryBuffer<TNew>
Type Parameters
TNew
Exceptions
- ArgumentNullException
Thrown when any required parameter is null.
ClearAsync(CancellationToken)
Clears the buffer (fills with zero).
public Task ClearAsync(CancellationToken cancellationToken = default)
Parameters
cancellationTokenCancellationToken
Returns
Exceptions
- ArgumentNullException
Thrown when any required parameter is null.
CopyFromAsync(IUnifiedMemoryBuffer, CancellationToken)
Copies from another memory buffer to this P2P buffer.
public Task CopyFromAsync(IUnifiedMemoryBuffer source, CancellationToken cancellationToken = default)
Parameters
sourceIUnifiedMemoryBuffercancellationTokenCancellationToken
Returns
Exceptions
- ArgumentNullException
Thrown when any required parameter is null.
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.
Exceptions
- ArgumentNullException
Thrown when any required parameter is null.
CopyFromAsync<TSource>(ReadOnlyMemory<TSource>, long, CancellationToken)
Copies data from source memory to this P2P buffer with offset support.
public ValueTask CopyFromAsync<TSource>(ReadOnlyMemory<TSource> source, long offset, CancellationToken cancellationToken = default) where TSource : unmanaged
Parameters
sourceReadOnlyMemory<TSource>Source data to copy from.
offsetlongOffset into this buffer in bytes.
cancellationTokenCancellationTokenCancellation token for the operation.
Returns
- ValueTask
Completed task when copy finishes.
Type Parameters
TSourceType of elements to copy.
Exceptions
- ArgumentNullException
Thrown when any required parameter is null.
CopyFromHostAsync<TData>(ReadOnlyMemory<TData>, long, CancellationToken)
Copies data from host memory to this P2P buffer.
public ValueTask CopyFromHostAsync<TData>(ReadOnlyMemory<TData> source, long offset, CancellationToken cancellationToken = default) where TData : unmanaged
Parameters
sourceReadOnlyMemory<TData>offsetlongcancellationTokenCancellationToken
Returns
Type Parameters
TData
Exceptions
- ArgumentNullException
Thrown when any required parameter is null.
CopyFromHostAsync<TData>(TData[], int, CancellationToken)
Copies data from host array to this P2P buffer with optimizations.
public Task CopyFromHostAsync<TData>(TData[] source, int offset, CancellationToken cancellationToken = default) where TData : unmanaged
Parameters
sourceTData[]offsetintcancellationTokenCancellationToken
Returns
Type Parameters
TData
Exceptions
- ArgumentNullException
Thrown when any required parameter is null.
CopyToAsync(IUnifiedMemoryBuffer, CancellationToken)
Copies to another memory buffer from this P2P buffer.
public Task CopyToAsync(IUnifiedMemoryBuffer destination, CancellationToken cancellationToken = default)
Parameters
destinationIUnifiedMemoryBuffercancellationTokenCancellationToken
Returns
Exceptions
- ArgumentNullException
Thrown when any required parameter is null.
CopyToAsync(IUnifiedMemoryBuffer<T>, CancellationToken)
Copies to another P2P buffer with optimizations.
public ValueTask CopyToAsync(IUnifiedMemoryBuffer<T> destination, CancellationToken cancellationToken = default)
Parameters
destinationIUnifiedMemoryBuffer<T>cancellationTokenCancellationToken
Returns
Exceptions
- ArgumentNullException
Thrown when any required parameter is null.
CopyToAsync(int, IUnifiedMemoryBuffer<T>, int, int, CancellationToken)
Copies a range to another buffer with P2P optimizations.
public ValueTask CopyToAsync(int sourceOffset, IUnifiedMemoryBuffer<T> destination, int destinationOffset, int count, CancellationToken cancellationToken = default)
Parameters
sourceOffsetintdestinationIUnifiedMemoryBuffer<T>destinationOffsetintcountintcancellationTokenCancellationToken
Returns
Exceptions
- ArgumentNullException
Thrown when any required parameter is null.
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.
Exceptions
- ArgumentNullException
Thrown when any required parameter is null.
CopyToAsync<TDest>(Memory<TDest>, long, CancellationToken)
Copies data from this P2P buffer to destination memory with offset support.
public ValueTask CopyToAsync<TDest>(Memory<TDest> destination, long offset, CancellationToken cancellationToken = default) where TDest : unmanaged
Parameters
destinationMemory<TDest>Destination memory to copy to.
offsetlongOffset into this buffer in bytes.
cancellationTokenCancellationTokenCancellation token for the operation.
Returns
- ValueTask
Completed task when copy finishes.
Type Parameters
TDestType of elements to copy.
Exceptions
- ArgumentNullException
Thrown when any required parameter is null.
CopyToHostAsync<TData>(Memory<TData>, long, CancellationToken)
Copies data to host memory from this P2P buffer.
public ValueTask CopyToHostAsync<TData>(Memory<TData> destination, long offset, CancellationToken cancellationToken = default) where TData : unmanaged
Parameters
destinationMemory<TData>offsetlongcancellationTokenCancellationToken
Returns
Type Parameters
TData
Exceptions
- ArgumentNullException
Thrown when any required parameter is null.
CopyToHostAsync<TData>(TData[], int, CancellationToken)
Copies data to host array from this P2P buffer.
public Task CopyToHostAsync<TData>(TData[] destination, int offset, CancellationToken cancellationToken = default) where TData : unmanaged
Parameters
destinationTData[]offsetintcancellationTokenCancellationToken
Returns
Type Parameters
TData
Exceptions
- ArgumentNullException
Thrown when any required parameter is null.
Dispose()
Performs dispose.
public void Dispose()
Exceptions
- ArgumentNullException
Thrown when any required parameter is null.
DisposeAsync()
Gets dispose asynchronously.
public ValueTask DisposeAsync()
Returns
- ValueTask
The result of the operation.
Exceptions
- ArgumentNullException
Thrown when any required parameter is null.
EnsureOnDevice()
Performs ensure on device.
public void EnsureOnDevice()
Exceptions
- ArgumentNullException
Thrown when any required parameter is null.
EnsureOnDeviceAsync(AcceleratorContext, CancellationToken)
Gets ensure on device asynchronously.
public ValueTask EnsureOnDeviceAsync(AcceleratorContext context, CancellationToken cancellationToken = default)
Parameters
contextAcceleratorContextThe context.
cancellationTokenCancellationTokenThe cancellation token.
Returns
- ValueTask
The result of the operation.
Exceptions
- ArgumentNullException
Thrown when any required parameter is null.
EnsureOnHost()
Performs ensure on host.
public void EnsureOnHost()
Exceptions
- ArgumentNullException
Thrown when any required parameter is null.
EnsureOnHostAsync(AcceleratorContext, CancellationToken)
Gets ensure on host asynchronously.
public ValueTask EnsureOnHostAsync(AcceleratorContext context, CancellationToken cancellationToken = default)
Parameters
contextAcceleratorContextThe context.
cancellationTokenCancellationTokenThe cancellation token.
Returns
- ValueTask
The result of the operation.
Exceptions
- ArgumentNullException
Thrown when any required parameter is null.
FillAsync(T, int, int, CancellationToken)
Fills a range of the buffer with a specific value.
public ValueTask FillAsync(T value, int offset, int count, CancellationToken cancellationToken = default)
Parameters
valueToffsetintcountintcancellationTokenCancellationToken
Returns
Exceptions
- ArgumentNullException
Thrown when any required parameter is null.
FillAsync(T, CancellationToken)
Fills the buffer with a specific value.
public ValueTask FillAsync(T value, CancellationToken cancellationToken = default)
Parameters
valueTcancellationTokenCancellationToken
Returns
Exceptions
- ArgumentNullException
Thrown when any required parameter is null.
FillAsync<TData>(TData, CancellationToken)
Fills the buffer with a specific value using P2P optimizations.
public Task FillAsync<TData>(TData value, CancellationToken cancellationToken = default) where TData : unmanaged
Parameters
valueTDatacancellationTokenCancellationToken
Returns
Type Parameters
TData
Exceptions
- ArgumentNullException
Thrown when any required parameter is null.
GetDeviceMemory()
Gets the device memory.
public DeviceMemory GetDeviceMemory()
Returns
- DeviceMemory
The device memory.
Exceptions
- ArgumentNullException
Thrown when any required parameter is null.
Map(MapMode)
Maps the buffer for direct CPU access.
public MappedMemory<T> Map(MapMode mode)
Parameters
modeMapMode
Returns
- MappedMemory<T>
Exceptions
- ArgumentNullException
Thrown when any required parameter is null.
MapAsync(MapMode, CancellationToken)
Asynchronously maps the buffer for direct CPU access.
public ValueTask<MappedMemory<T>> MapAsync(MapMode mode, CancellationToken cancellationToken = default)
Parameters
modeMapModecancellationTokenCancellationToken
Returns
Exceptions
- ArgumentNullException
Thrown when any required parameter is null.
MapRange(int, int, MapMode)
Maps a range of the buffer for direct CPU access.
public MappedMemory<T> MapRange(int offset, int length, MapMode mode)
Parameters
Returns
- MappedMemory<T>
Exceptions
- ArgumentNullException
Thrown when any required parameter is null.
MarkDeviceDirty()
Performs mark device dirty.
public void MarkDeviceDirty()
Exceptions
- ArgumentNullException
Thrown when any required parameter is null.
MarkHostDirty()
Performs mark host dirty.
public void MarkHostDirty()
Exceptions
- ArgumentNullException
Thrown when any required parameter is null.
Slice(int, int)
Creates a slice of this buffer.
public IUnifiedMemoryBuffer<T> Slice(int offset, int length)
Parameters
Returns
Exceptions
- ArgumentNullException
Thrown when any required parameter is null.
Synchronize()
Performs synchronize.
public void Synchronize()
Exceptions
- ArgumentNullException
Thrown when any required parameter is null.
SynchronizeAsync(AcceleratorContext, CancellationToken)
Gets synchronize asynchronously.
public ValueTask SynchronizeAsync(AcceleratorContext context, CancellationToken cancellationToken = default)
Parameters
contextAcceleratorContextThe context.
cancellationTokenCancellationTokenThe cancellation token.
Returns
- ValueTask
The result of the operation.
Exceptions
- ArgumentNullException
Thrown when any required parameter is null.