Table of Contents

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

underlyingBuffer IUnifiedMemoryBuffer

The underlying memory buffer that provides storage.

accelerator IAccelerator

The accelerator device that owns this buffer.

length int

The number of elements in the buffer.

supportsDirectP2P bool

Whether this buffer supports direct peer-to-peer transfers.

logger ILogger

The 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

IAccelerator

Exceptions

ArgumentNullException

Thrown when any required parameter is null.

IsDirty

Gets whether the buffer is dirty.

public bool IsDirty { get; }

Property Value

bool

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

bool

Exceptions

ArgumentNullException

Thrown when any required parameter is null.

IsOnDevice

Gets whether the buffer is currently on device.

public bool IsOnDevice { get; }

Property Value

bool

Exceptions

ArgumentNullException

Thrown when any required parameter is null.

IsOnHost

Gets whether the buffer is currently on host.

public bool IsOnHost { get; }

Property Value

bool

Exceptions

ArgumentNullException

Thrown when any required parameter is null.

Length

Gets the number of elements in the buffer.

public int Length { get; }

Property Value

int

Exceptions

ArgumentNullException

Thrown when any required parameter is null.

Options

Gets the memory options configured for this buffer.

public MemoryOptions Options { get; }

Property Value

MemoryOptions

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

long

Exceptions

ArgumentNullException

Thrown when any required parameter is null.

State

Gets or sets the buffer state.

public BufferState State { get; }

Property Value

BufferState

Exceptions

ArgumentNullException

Thrown when any required parameter is null.

SupportsDirectP2P

Indicates if this buffer supports direct P2P transfers.

public bool SupportsDirectP2P { get; }

Property Value

bool

Exceptions

ArgumentNullException

Thrown when any required parameter is null.

UnderlyingBuffer

Gets the underlying memory buffer for advanced operations.

public IUnifiedMemoryBuffer UnderlyingBuffer { get; }

Property Value

IUnifiedMemoryBuffer

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

cancellationToken CancellationToken

Returns

Task

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

source IUnifiedMemoryBuffer
cancellationToken CancellationToken

Returns

Task

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

source ReadOnlyMemory<T>

The source.

cancellationToken CancellationToken

The 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

source ReadOnlyMemory<TSource>

Source data to copy from.

offset long

Offset into this buffer in bytes.

cancellationToken CancellationToken

Cancellation token for the operation.

Returns

ValueTask

Completed task when copy finishes.

Type Parameters

TSource

Type 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

source ReadOnlyMemory<TData>
offset long
cancellationToken CancellationToken

Returns

ValueTask

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

source TData[]
offset int
cancellationToken CancellationToken

Returns

Task

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

destination IUnifiedMemoryBuffer
cancellationToken CancellationToken

Returns

Task

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

destination IUnifiedMemoryBuffer<T>
cancellationToken CancellationToken

Returns

ValueTask

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

sourceOffset int
destination IUnifiedMemoryBuffer<T>
destinationOffset int
count int
cancellationToken CancellationToken

Returns

ValueTask

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

destination Memory<T>

The destination.

cancellationToken CancellationToken

The 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

destination Memory<TDest>

Destination memory to copy to.

offset long

Offset into this buffer in bytes.

cancellationToken CancellationToken

Cancellation token for the operation.

Returns

ValueTask

Completed task when copy finishes.

Type Parameters

TDest

Type 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

destination Memory<TData>
offset long
cancellationToken CancellationToken

Returns

ValueTask

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

destination TData[]
offset int
cancellationToken CancellationToken

Returns

Task

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

context AcceleratorContext

The context.

cancellationToken CancellationToken

The 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

context AcceleratorContext

The context.

cancellationToken CancellationToken

The 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

value T
offset int
count int
cancellationToken CancellationToken

Returns

ValueTask

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

value T
cancellationToken CancellationToken

Returns

ValueTask

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

value TData
cancellationToken CancellationToken

Returns

Task

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

mode MapMode

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

mode MapMode
cancellationToken CancellationToken

Returns

ValueTask<MappedMemory<T>>

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

offset int
length int
mode MapMode

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

offset int
length int

Returns

IUnifiedMemoryBuffer<T>

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

context AcceleratorContext

The context.

cancellationToken CancellationToken

The cancellation token.

Returns

ValueTask

The result of the operation.

Exceptions

ArgumentNullException

Thrown when any required parameter is null.