Table of Contents

Class DeviceBuffer<T>

Namespace
NVMathNet
Assembly
NVMath.Net.dll

A strongly-typed buffer of T residing in GPU device memory. Supports zero-copy async transfers with host-pinned memory. Implements System.IDisposable - dispose to free the device allocation.

public sealed class DeviceBuffer<T> : IDisposable, IAsyncDisposable where T : unmanaged, INumberBase<T>

Type Parameters

T

Unmanaged numeric element type (float, double, System.Numerics.Complex, etc.).

Inheritance

Implements

Inherited Members

Constructors

DeviceBuffer(long, bool)

Allocates a device buffer of length elements using the process-wide NVMathNet.Memory.DeviceMemory.Default manager (cudaMalloc unless replaced).

public DeviceBuffer(long length, bool allocate = true)

Parameters

length long
allocate bool

DeviceBuffer(long, IDeviceMemoryManager)

Allocates a device buffer of length elements from manager. The memory is returned to the same manager on dispose.

public DeviceBuffer(long length, IDeviceMemoryManager manager)

Parameters

length long
manager IDeviceMemoryManager

Properties

Length

Number of elements.

public long Length { get; }

Property Value

long

Pointer

Raw device pointer as void*.

public void* Pointer { get; }

Property Value

void*

PointerAsInt

Raw device pointer as System.IntPtr.

public nint PointerAsInt { get; }

Property Value

nint

SizeInBytes

Total size in bytes.

public nuint SizeInBytes { get; }

Property Value

nuint

Methods

AllocAsync(long, CudaStream)

Allocates a device buffer asynchronously on stream using cudaMallocAsync.

public static DeviceBuffer<T> AllocAsync(long length, CudaStream stream)

Parameters

length long
stream CudaStream

Returns

DeviceBuffer<T>

Clear(CudaStream?)

Fills the buffer with zero bytes.

public void Clear(CudaStream? stream = null)

Parameters

stream CudaStream?

CopyFrom(ReadOnlySpan<T>, CudaStream?)

Copies data from a managed System.ReadOnlySpan`1 to this device buffer. Uses pinned memory for a zero-copy host->device transfer.

public void CopyFrom(ReadOnlySpan<T> source, CudaStream? stream = null)

Parameters

source ReadOnlySpan<T>
stream CudaStream?

CopyFrom(DeviceBuffer<T>, CudaStream)

Copies from another device buffer using cudaMemcpyAsync.

public void CopyFrom(DeviceBuffer<T> other, CudaStream stream)

Parameters

other DeviceBuffer<T>
stream CudaStream

CopyFromAsync(T[], CudaStream, CancellationToken)

Asynchronously copies from a managed array to device, returning a System.Threading.Tasks.Task that completes when the GPU has consumed the data.

public Task CopyFromAsync(T[] source, CudaStream stream, CancellationToken ct = default)

Parameters

source T[]
stream CudaStream
ct CancellationToken

Returns

Task

CopyTo(Span<T>, CudaStream?)

Copies data from this device buffer into a managed System.Span`1. Uses pinned memory for a zero-copy device->host transfer.

public void CopyTo(Span<T> destination, CudaStream? stream = null)

Parameters

destination Span<T>
stream CudaStream?

CopyToAsync(T[], CudaStream, CancellationToken)

Asynchronously copies from device to a managed array, returning a System.Threading.Tasks.Task that completes when the data is available on the host.

public Task CopyToAsync(T[] destination, CudaStream stream, CancellationToken ct = default)

Parameters

destination T[]
stream CudaStream
ct CancellationToken

Returns

Task

Dispose()

Releases the unmanaged CUDA memory allocated by this instance.

public void Dispose()

DisposeAsync()

Asynchronously disposes the device buffer, freeing the underlying device memory.

public ValueTask DisposeAsync()

Returns

ValueTask

A System.Threading.Tasks.ValueTask that represents the asynchronous dispose operation.

ToArray()

Returns a new T[] containing a snapshot of the device data. Blocks until transfer completes.

public T[] ToArray()

Returns

T[]