Table of Contents

Class CudaAsyncMemoryManagerAdapter

Namespace
DotCompute.Backends.CUDA.Memory
Assembly
DotCompute.Backends.CUDA.dll

Adapter that wraps CudaMemoryManager for async operations. Bridges the CUDA memory manager with the unified memory interface.

public sealed class CudaAsyncMemoryManagerAdapter : IUnifiedMemoryManager, IAsyncDisposable, IDisposable
Inheritance
CudaAsyncMemoryManagerAdapter
Implements
Inherited Members
Extension Methods

Remarks

Initializes a new instance of the CudaAsyncMemoryManagerAdapter class.

Constructors

CudaAsyncMemoryManagerAdapter(CudaMemoryManager)

Adapter that wraps CudaMemoryManager for async operations. Bridges the CUDA memory manager with the unified memory interface.

public CudaAsyncMemoryManagerAdapter(CudaMemoryManager memoryManager)

Parameters

memoryManager CudaMemoryManager

The underlying CUDA memory manager.

Remarks

Initializes a new instance of the CudaAsyncMemoryManagerAdapter class.

Properties

Accelerator

Gets the accelerator this memory manager is associated with.

public IAccelerator Accelerator { get; }

Property Value

IAccelerator

CurrentAllocatedMemory

Gets the current allocated memory in bytes.

public long CurrentAllocatedMemory { get; }

Property Value

long

MaxAllocationSize

Gets the maximum memory allocation size in bytes.

public long MaxAllocationSize { get; }

Property Value

long

Statistics

Gets memory usage statistics.

public MemoryStatistics Statistics { get; }

Property Value

MemoryStatistics

TotalAvailableMemory

Gets the total available memory in bytes.

public long TotalAvailableMemory { get; }

Property Value

long

Methods

AllocateAndCopyAsync<T>(ReadOnlyMemory<T>, MemoryOptions, CancellationToken)

Allocates memory and copies data from host.

public ValueTask<IUnifiedMemoryBuffer<T>> AllocateAndCopyAsync<T>(ReadOnlyMemory<T> source, MemoryOptions options = MemoryOptions.None, CancellationToken cancellationToken = default) where T : unmanaged

Parameters

source ReadOnlyMemory<T>

The source data to copy.

options MemoryOptions

Memory allocation options.

cancellationToken CancellationToken

Cancellation token.

Returns

ValueTask<IUnifiedMemoryBuffer<T>>

A newly allocated and initialized memory buffer.

Type Parameters

T

The element type.

AllocateAsync<T>(int, MemoryOptions, CancellationToken)

Allocates a memory buffer for a specific number of elements.

public ValueTask<IUnifiedMemoryBuffer<T>> AllocateAsync<T>(int count, MemoryOptions options = MemoryOptions.None, CancellationToken cancellationToken = default) where T : unmanaged

Parameters

count int

The number of elements to allocate.

options MemoryOptions

Memory allocation options.

cancellationToken CancellationToken

Cancellation token.

Returns

ValueTask<IUnifiedMemoryBuffer<T>>

A newly allocated memory buffer.

Type Parameters

T

The element type.

AllocateDevice(long)

Allocates device-specific memory.

public DeviceMemory AllocateDevice(long sizeInBytes)

Parameters

sizeInBytes long

Size in bytes to allocate.

Returns

DeviceMemory

Device memory handle.

AllocateRawAsync(long, MemoryOptions, CancellationToken)

Allocates memory by size in bytes (for advanced scenarios).

public ValueTask<IUnifiedMemoryBuffer> AllocateRawAsync(long sizeInBytes, MemoryOptions options = MemoryOptions.None, CancellationToken cancellationToken = default)

Parameters

sizeInBytes long

The size in bytes to allocate.

options MemoryOptions

Memory allocation options.

cancellationToken CancellationToken

Cancellation token.

Returns

ValueTask<IUnifiedMemoryBuffer>

A newly allocated memory buffer.

Clear()

Clears all allocated memory and resets the manager.

public void Clear()

CopyAsync<T>(IUnifiedMemoryBuffer<T>, IUnifiedMemoryBuffer<T>, CancellationToken)

Copies data between buffers.

public ValueTask CopyAsync<T>(IUnifiedMemoryBuffer<T> source, IUnifiedMemoryBuffer<T> destination, CancellationToken cancellationToken = default) where T : unmanaged

Parameters

source IUnifiedMemoryBuffer<T>

The source buffer.

destination IUnifiedMemoryBuffer<T>

The destination buffer.

cancellationToken CancellationToken

Cancellation token.

Returns

ValueTask

A task representing the copy operation.

Type Parameters

T

The element type.

CopyAsync<T>(IUnifiedMemoryBuffer<T>, int, IUnifiedMemoryBuffer<T>, int, int, CancellationToken)

Copies data between buffers with specified ranges.

public ValueTask CopyAsync<T>(IUnifiedMemoryBuffer<T> source, int sourceOffset, IUnifiedMemoryBuffer<T> destination, int destinationOffset, int count, CancellationToken cancellationToken = default) where T : unmanaged

Parameters

source IUnifiedMemoryBuffer<T>

The source buffer.

sourceOffset int

The offset in the source buffer.

destination IUnifiedMemoryBuffer<T>

The destination buffer.

destinationOffset int

The offset in the destination buffer.

count int

The number of elements to copy.

cancellationToken CancellationToken

Cancellation token.

Returns

ValueTask

A task representing the copy operation.

Type Parameters

T

The element type.

CopyDeviceToDevice(DeviceMemory, DeviceMemory, long)

Copies data between device memories.

public void CopyDeviceToDevice(DeviceMemory sourceDevice, DeviceMemory destinationDevice, long sizeInBytes)

Parameters

sourceDevice DeviceMemory

Source device memory.

destinationDevice DeviceMemory

Destination device memory.

sizeInBytes long

Size in bytes.

CopyDeviceToHost(DeviceMemory, nint, long)

Copies data from device to host memory.

public void CopyDeviceToHost(DeviceMemory deviceMemory, nint hostPointer, long sizeInBytes)

Parameters

deviceMemory DeviceMemory

Device memory.

hostPointer nint

Host memory pointer.

sizeInBytes long

Size in bytes.

CopyDeviceToHostAsync(DeviceMemory, nint, long, CancellationToken)

Asynchronously copies data from device to host memory.

public ValueTask CopyDeviceToHostAsync(DeviceMemory deviceMemory, nint hostPointer, long sizeInBytes, CancellationToken cancellationToken = default)

Parameters

deviceMemory DeviceMemory

Device memory.

hostPointer nint

Host memory pointer.

sizeInBytes long

Size in bytes.

cancellationToken CancellationToken

Cancellation token.

Returns

ValueTask

A task representing the operation.

CopyFromDeviceAsync<T>(IUnifiedMemoryBuffer<T>, Memory<T>, CancellationToken)

Copies data from a device buffer to host memory.

public ValueTask CopyFromDeviceAsync<T>(IUnifiedMemoryBuffer<T> source, Memory<T> destination, CancellationToken cancellationToken = default) where T : unmanaged

Parameters

source IUnifiedMemoryBuffer<T>

The source buffer.

destination Memory<T>

The destination memory.

cancellationToken CancellationToken

Cancellation token.

Returns

ValueTask

A task representing the copy operation.

Type Parameters

T

The element type.

CopyHostToDevice(nint, DeviceMemory, long)

Copies data from host to device memory.

public void CopyHostToDevice(nint hostPointer, DeviceMemory deviceMemory, long sizeInBytes)

Parameters

hostPointer nint

Host memory pointer.

deviceMemory DeviceMemory

Device memory.

sizeInBytes long

Size in bytes.

CopyHostToDeviceAsync(nint, DeviceMemory, long, CancellationToken)

Asynchronously copies data from host to device memory.

public ValueTask CopyHostToDeviceAsync(nint hostPointer, DeviceMemory deviceMemory, long sizeInBytes, CancellationToken cancellationToken = default)

Parameters

hostPointer nint

Host memory pointer.

deviceMemory DeviceMemory

Device memory.

sizeInBytes long

Size in bytes.

cancellationToken CancellationToken

Cancellation token.

Returns

ValueTask

A task representing the operation.

CopyToDeviceAsync<T>(ReadOnlyMemory<T>, IUnifiedMemoryBuffer<T>, CancellationToken)

Copies data from host memory to a device buffer.

public ValueTask CopyToDeviceAsync<T>(ReadOnlyMemory<T> source, IUnifiedMemoryBuffer<T> destination, CancellationToken cancellationToken = default) where T : unmanaged

Parameters

source ReadOnlyMemory<T>

The source data.

destination IUnifiedMemoryBuffer<T>

The destination buffer.

cancellationToken CancellationToken

Cancellation token.

Returns

ValueTask

A task representing the copy operation.

Type Parameters

T

The element type.

CreateView<T>(IUnifiedMemoryBuffer<T>, int, int)

Creates a view over existing memory.

public IUnifiedMemoryBuffer<T> CreateView<T>(IUnifiedMemoryBuffer<T> buffer, int offset, int length) where T : unmanaged

Parameters

buffer IUnifiedMemoryBuffer<T>

The source buffer.

offset int

The offset in elements.

length int

The length of the view in elements.

Returns

IUnifiedMemoryBuffer<T>

A view over the existing buffer.

Type Parameters

T

The element type.

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.

Free(IUnifiedMemoryBuffer)

Frees a memory buffer.

public void Free(IUnifiedMemoryBuffer buffer)

Parameters

buffer IUnifiedMemoryBuffer

The buffer to free.

FreeAsync(IUnifiedMemoryBuffer, CancellationToken)

Asynchronously frees a memory buffer.

public ValueTask FreeAsync(IUnifiedMemoryBuffer buffer, CancellationToken cancellationToken = default)

Parameters

buffer IUnifiedMemoryBuffer

The buffer to free.

cancellationToken CancellationToken

Cancellation token.

Returns

ValueTask

A task representing the free operation.

FreeDevice(DeviceMemory)

Frees device-specific memory.

public void FreeDevice(DeviceMemory deviceMemory)

Parameters

deviceMemory DeviceMemory

Device memory to free.

MemsetDevice(DeviceMemory, byte, long)

Sets device memory to a specific value.

public void MemsetDevice(DeviceMemory deviceMemory, byte value, long sizeInBytes)

Parameters

deviceMemory DeviceMemory

Device memory to set.

value byte

Value to set.

sizeInBytes long

Size in bytes.

MemsetDeviceAsync(DeviceMemory, byte, long, CancellationToken)

Asynchronously sets device memory to a specific value.

public ValueTask MemsetDeviceAsync(DeviceMemory deviceMemory, byte value, long sizeInBytes, CancellationToken cancellationToken = default)

Parameters

deviceMemory DeviceMemory

Device memory to set.

value byte

Value to set.

sizeInBytes long

Size in bytes.

cancellationToken CancellationToken

Cancellation token.

Returns

ValueTask

A task representing the operation.

OptimizeAsync(CancellationToken)

Optimizes memory by defragmenting and releasing unused memory.

public ValueTask OptimizeAsync(CancellationToken cancellationToken = default)

Parameters

cancellationToken CancellationToken

Cancellation token.

Returns

ValueTask

A task representing the optimization operation.