Table of Contents

Class CudaMemory

Namespace
JYPPX.CudaSharp
Assembly
JYPPX.CudaSharp.dll

Managed wrapper around a device memory allocation. CUDA 设备内存分配的托管封装。

public class CudaMemory : IDisposable
Inheritance
CudaMemory
Implements
Derived
Inherited Members

Constructors

CudaMemory(int)

Allocates CUDA device memory. 分配 CUDA 设备内存。

public CudaMemory(int sizeInBytes)

Parameters

sizeInBytes int

The allocation size in bytes. 分配大小,单位为字节。

Properties

IsIpcImported

Gets whether this wrapper owns a process-local CUDA IPC mapping. 获取此 wrapper 是否拥有进程内 CUDA IPC mapping。

public bool IsIpcImported { get; }

Property Value

bool

SizeInBytes

Gets the allocation size in bytes. 获取分配大小,单位为字节。

public int SizeInBytes { get; }

Property Value

int

Methods

Advise(CudaMemoryAdvice, int)

Applies a CUDA memory advice to the entire allocation. 对整个分配应用 CUDA memory advice。

public void Advise(CudaMemoryAdvice advice, int device)

Parameters

advice CudaMemoryAdvice

The CUDA memory advice. CUDA 内存建议。

device int

The device ordinal associated with the advice. 与 advice 关联的设备序号。

Advise(int, CudaMemoryAdvice, int)

Applies CUDA memory advice to the first bytes of the allocation. 对分配起始处的一部分应用 CUDA memory advice。

public void Advise(int count, CudaMemoryAdvice advice, int device)

Parameters

count int

The number of bytes covered by the advice. advice 覆盖的字节数。

advice CudaMemoryAdvice

The CUDA memory advice. CUDA 内存建议。

device int

The device ordinal associated with the advice. 与 advice 关联的设备序号。

Advise(int, int, CudaMemoryAdvice, int)

Applies CUDA memory advice to part of the allocation. 对分配的一部分应用 CUDA memory advice。

public void Advise(int offset, int count, CudaMemoryAdvice advice, int device)

Parameters

offset int

The byte offset within this allocation. 当前分配内的字节偏移。

count int

The number of bytes covered by the advice. advice 覆盖的字节数。

advice CudaMemoryAdvice

The CUDA memory advice. CUDA 内存建议。

device int

The device ordinal associated with the advice. 与 advice 关联的设备序号。

AllocateAsync(int, CudaStream)

Asynchronously allocates device memory on a CUDA stream. 在 CUDA stream 上异步分配设备内存。

public static CudaMemory AllocateAsync(int sizeInBytes, CudaStream stream)

Parameters

sizeInBytes int

The allocation size in bytes. 分配大小,单位为字节。

stream CudaStream

The CUDA stream that orders the allocation. 用于排序分配操作的 CUDA stream。

Returns

CudaMemory

A managed device-memory wrapper. 设备内存的托管封装。

AllocateFromPoolAsync(int, CudaMemoryPool, CudaStream)

Asynchronously allocates device memory from a specific CUDA memory pool. 从指定 CUDA memory pool 中异步分配设备内存。

public static CudaMemory AllocateFromPoolAsync(int sizeInBytes, CudaMemoryPool memoryPool, CudaStream stream)

Parameters

sizeInBytes int

The allocation size in bytes. 分配大小,单位为字节。

memoryPool CudaMemoryPool

The CUDA memory pool used for allocation. 用于分配的 CUDA memory pool。

stream CudaStream

The CUDA stream that orders the allocation. 用于排序分配操作的 CUDA stream。

Returns

CudaMemory

A managed CUDA memory wrapper. CUDA 设备内存托管封装。

CopyFrom(byte[])

Copies a byte array into device memory. 将字节数组复制到设备内存中。

public void CopyFrom(byte[] source)

Parameters

source byte[]

The source byte array. 源字节数组。

CopyFrom(float[])

Copies a float array into device memory. 将浮点数组复制到设备内存中。

public void CopyFrom(float[] source)

Parameters

source float[]

The source float array. 源浮点数组。

CopyFromAsync(CudaPinnedMemory, CudaStream)

Asynchronously copies the full pinned-host buffer into this device allocation. 异步将整个 pinned host 缓冲区复制到当前设备内存。

public void CopyFromAsync(CudaPinnedMemory source, CudaStream stream)

Parameters

source CudaPinnedMemory

The source pinned host buffer. 源 pinned host 缓冲区。

stream CudaStream

The CUDA stream that orders the copy. 用于排序复制操作的 CUDA stream。

CopyFromAsync(CudaPinnedMemory, int, CudaStream)

Asynchronously copies a pinned host buffer into device memory. 将 pinned host 缓冲区异步复制到设备内存中。

public void CopyFromAsync(CudaPinnedMemory source, int count, CudaStream stream)

Parameters

source CudaPinnedMemory

The source pinned host buffer. 源 pinned host 缓冲区。

count int

The number of bytes to copy. 要复制的字节数。

stream CudaStream

The CUDA stream that orders the copy. 用于排序复制操作的 CUDA stream。

CopyFromAsync(CudaRegisteredHostMemory, int, CudaStream)

Asynchronously copies registered host memory into device memory. 将 registered host memory 异步复制到设备内存中。

public void CopyFromAsync(CudaRegisteredHostMemory source, int count, CudaStream stream)

Parameters

source CudaRegisteredHostMemory

The source registered host buffer. 源 registered host 缓冲区。

count int

The number of bytes to copy. 要复制的字节数。

stream CudaStream

The CUDA stream that orders the copy. 用于排序复制操作的 CUDA stream。

CopyTo(CudaMemory)

Copies bytes from this allocation to another allocation using the smaller allocation size. 按两个分配中较小的大小,将当前分配复制到另一个分配。

public void CopyTo(CudaMemory destination)

Parameters

destination CudaMemory

The destination device allocation. 目标设备内存分配。

CopyTo(CudaMemory, int)

Copies bytes from this allocation to another device allocation. 将当前分配中的字节复制到另一个设备分配中。

public void CopyTo(CudaMemory destination, int count)

Parameters

destination CudaMemory

The destination device allocation. 目标设备内存分配。

count int

The number of bytes to copy. 要复制的字节数。

CopyTo(byte[])

Copies device memory into a byte array. 将设备内存复制到字节数组中。

public void CopyTo(byte[] destination)

Parameters

destination byte[]

The destination byte array. 目标字节数组。

CopyTo(float[])

Copies device memory into a float array. 将设备内存复制到浮点数组中。

public void CopyTo(float[] destination)

Parameters

destination float[]

The destination float array. 目标浮点数组。

CopyToAsync(CudaMemory, CudaStream)

Asynchronously copies bytes to another allocation using the smaller allocation size. 按两个分配中较小的大小,异步将当前分配复制到另一个分配。

public void CopyToAsync(CudaMemory destination, CudaStream stream)

Parameters

destination CudaMemory

The destination device allocation. 目标设备内存分配。

stream CudaStream

The CUDA stream that orders the copy. 用于排序复制操作的 CUDA stream。

CopyToAsync(CudaMemory, int, CudaStream)

Asynchronously copies bytes to another device allocation. 异步将字节复制到另一个设备分配中。

public void CopyToAsync(CudaMemory destination, int count, CudaStream stream)

Parameters

destination CudaMemory

The destination device allocation. 目标设备内存分配。

count int

The number of bytes to copy. 要复制的字节数。

stream CudaStream

The CUDA stream that orders the copy. 用于排序复制操作的 CUDA stream。

CopyToAsync(CudaPinnedMemory, CudaStream)

Asynchronously copies this allocation into a full pinned-host buffer. 异步将当前设备内存复制到整个 pinned host 缓冲区。

public void CopyToAsync(CudaPinnedMemory destination, CudaStream stream)

Parameters

destination CudaPinnedMemory

The destination pinned host buffer. 目标 pinned host 缓冲区。

stream CudaStream

The CUDA stream that orders the copy. 用于排序复制操作的 CUDA stream。

CopyToAsync(CudaPinnedMemory, int, CudaStream)

Asynchronously copies device memory into a pinned host buffer. 将设备内存异步复制到 pinned host 缓冲区中。

public void CopyToAsync(CudaPinnedMemory destination, int count, CudaStream stream)

Parameters

destination CudaPinnedMemory

The destination pinned host buffer. 目标 pinned host 缓冲区。

count int

The number of bytes to copy. 要复制的字节数。

stream CudaStream

The CUDA stream that orders the copy. 用于排序复制操作的 CUDA stream。

CopyToAsync(CudaRegisteredHostMemory, int, CudaStream)

Asynchronously copies device memory into registered host memory. 将设备内存异步复制到 registered host memory 中。

public void CopyToAsync(CudaRegisteredHostMemory destination, int count, CudaStream stream)

Parameters

destination CudaRegisteredHostMemory

The destination registered host buffer. 目标 registered host 缓冲区。

count int

The number of bytes to copy. 要复制的字节数。

stream CudaStream

The CUDA stream that orders the copy. 用于排序复制操作的 CUDA stream。

CopyToAuto(CudaMemory, int)

Copies bytes to another CUDA allocation and lets CUDA infer the copy direction. 将字节复制到另一个 CUDA 分配,并让 CUDA 自动推断复制方向。

public void CopyToAuto(CudaMemory destination, int count)

Parameters

destination CudaMemory

The destination allocation. 目标设备内存分配。

count int

The number of bytes to copy. 要复制的字节数。

CopyToAutoAsync(CudaMemory, int, CudaStream)

Asynchronously copies bytes to another CUDA allocation and lets CUDA infer the copy direction. 异步将字节复制到另一个 CUDA 分配,并让 CUDA 自动推断复制方向。

public void CopyToAutoAsync(CudaMemory destination, int count, CudaStream stream)

Parameters

destination CudaMemory

The destination allocation. 目标设备内存分配。

count int

The number of bytes to copy. 要复制的字节数。

stream CudaStream

The CUDA stream that orders the copy. 用于排序复制操作的 CUDA stream。

CopyToPeer(CudaMemory, int, int, int)

Copies bytes from this allocation to a destination allocation on another CUDA device. 将当前分配中的字节复制到另一个 CUDA 设备上的目标分配。

public void CopyToPeer(CudaMemory destination, int sourceDevice, int destinationDevice, int count)

Parameters

destination CudaMemory

The destination allocation. 目标设备内存分配。

sourceDevice int

The CUDA device ordinal that owns this source allocation. 拥有当前源分配的 CUDA 设备序号。

destinationDevice int

The CUDA device ordinal that owns the destination allocation. 拥有目标分配的 CUDA 设备序号。

count int

The number of bytes to copy. 要复制的字节数。

CopyToPeerAsync(CudaMemory, int, int, int, CudaStream)

Asynchronously copies bytes from this allocation to another CUDA device using a stream. 使用 stream 将当前分配中的字节异步复制到另一个 CUDA 设备。

public void CopyToPeerAsync(CudaMemory destination, int sourceDevice, int destinationDevice, int count, CudaStream stream)

Parameters

destination CudaMemory

The destination allocation. 目标设备内存分配。

sourceDevice int

The CUDA device ordinal that owns this source allocation. 拥有当前源分配的 CUDA 设备序号。

destinationDevice int

The CUDA device ordinal that owns the destination allocation. 拥有目标分配的 CUDA 设备序号。

count int

The number of bytes to copy. 要复制的字节数。

stream CudaStream

The CUDA stream that orders the copy. 用于排序该复制操作的 CUDA stream。

Dispose()

Releases the device allocation. 释放设备内存分配。

public void Dispose()

ExportIpcDescriptor()

Creates a transport descriptor for this synchronous allocation. 为当前同步 allocation 创建传输 descriptor。

public CudaIpcMemoryExportDescriptor ExportIpcDescriptor()

Returns

CudaIpcMemoryExportDescriptor

The copied token and exact allocation size. 复制型 token 与精确 allocation 大小。

ExportIpcToken()

Copies an opaque CUDA IPC export token for this synchronous device allocation. 复制当前同步 device allocation 的 opaque CUDA IPC 导出 token。

public CudaIpcExportToken ExportIpcToken()

Returns

CudaIpcExportToken

Remarks

Only allocations created by the public CudaMemory(int) constructor are exportable. Managed, asynchronous, and pool allocations are rejected. Keep this allocation alive while another process uses the token. This method never exposes the device pointer. 只有通过公开构造函数创建的同步分配可导出;managed、async 与 pool allocation 会被拒绝。 其他进程使用 token 期间必须保持当前分配存活;此方法绝不公开 device pointer。

Fill(byte)

Fills the entire allocation with a byte value. 使用一个字节值填充整个设备内存分配。

public void Fill(byte value)

Parameters

value byte

The fill byte value. 填充值。

Fill(byte, int)

Fills part of the allocation with a byte value. 使用一个字节值填充部分分配区域。

public void Fill(byte value, int count)

Parameters

value byte

The fill byte value. 填充值。

count int

The number of bytes to fill. 要填充的字节数。

FillAsync(byte, CudaStream)

Asynchronously fills the entire allocation with a byte value. 异步使用一个字节值填充整个设备内存分配。

public void FillAsync(byte value, CudaStream stream)

Parameters

value byte

The fill byte value. 填充值。

stream CudaStream

The CUDA stream that orders the fill. 用于排序填充操作的 CUDA stream。

FillAsync(byte, int, CudaStream)

Asynchronously fills part of the allocation with a byte value. 使用一个字节值异步填充分配的一部分区域。

public void FillAsync(byte value, int count, CudaStream stream)

Parameters

value byte

The fill byte value. 填充值。

count int

The number of bytes to fill. 要填充的字节数。

stream CudaStream

The CUDA stream that orders the fill. 用于排序填充操作的 CUDA stream。

FreeAsync(CudaStream)

Asynchronously frees this allocation on the supplied CUDA stream and invalidates the managed handle. 在指定 CUDA stream 上异步释放当前分配,并使托管句柄失效。

public void FreeAsync(CudaStream stream)

Parameters

stream CudaStream

The CUDA stream that orders the free operation. 用于排序释放操作的 CUDA stream。

GetPointerAttributes()

Queries CUDA pointer attributes for this device allocation. 查询当前设备内存分配的 CUDA 指针属性。

public CudaPointerAttributes GetPointerAttributes()

Returns

CudaPointerAttributes

Pointer metadata useful for diagnostics and deployment validation. 用于诊断和部署验证的指针元数据。

GetRangeAccessedByDevices()

Queries CUDA device ordinals that have accessed-by advice for this allocation. 查询对当前分配设置了 accessed-by 建议的 CUDA 设备序号。

public int[] GetRangeAccessedByDevices()

Returns

int[]

A managed copy of device ordinals reported by CUDA. CUDA 返回的设备序号托管副本。

GetRangeAccessedByDevices(int, int)

Queries CUDA device ordinals that have accessed-by advice for a subrange of this allocation. 查询当前分配子范围内设置了 accessed-by 建议的 CUDA 设备序号。

public int[] GetRangeAccessedByDevices(int offset, int count)

Parameters

offset int

The byte offset within this allocation. 当前分配内的字节偏移。

count int

The number of bytes to query. 要查询的字节数。

Returns

int[]

A managed copy of device ordinals reported by CUDA. CUDA 返回的设备序号托管副本。

GetRangeAttribute(CudaMemoryRangeAttribute)

Queries one scalar CUDA managed-memory range attribute for this allocation. 查询当前分配的一个 CUDA managed memory range 标量属性。

public CudaMemoryRangeAttributeValue GetRangeAttribute(CudaMemoryRangeAttribute attribute)

Parameters

attribute CudaMemoryRangeAttribute

The scalar memory range attribute. 标量 memory range 属性。

Returns

CudaMemoryRangeAttributeValue

A managed attribute snapshot. 托管属性快照。

GetRangeAttribute(CudaMemoryRangeAttribute, int, int)

Queries one scalar CUDA managed-memory range attribute for a subrange of this allocation. 查询当前分配子范围的一个 CUDA managed memory range 标量属性。

public CudaMemoryRangeAttributeValue GetRangeAttribute(CudaMemoryRangeAttribute attribute, int offset, int count)

Parameters

attribute CudaMemoryRangeAttribute

The scalar memory range attribute. 标量 memory range 属性。

offset int

The byte offset within this allocation. 当前分配内的字节偏移。

count int

The number of bytes to query. 要查询的字节数。

Returns

CudaMemoryRangeAttributeValue

A managed attribute snapshot. 托管属性快照。

GetRangeAttributes(params CudaMemoryRangeAttribute[])

Queries multiple scalar CUDA managed-memory range attributes for this allocation. 查询当前分配的多个 CUDA managed memory range 标量属性。

public CudaMemoryRangeAttributeValue[] GetRangeAttributes(params CudaMemoryRangeAttribute[] attributes)

Parameters

attributes CudaMemoryRangeAttribute[]

The scalar memory range attributes. 标量 memory range 属性列表。

Returns

CudaMemoryRangeAttributeValue[]

Managed attribute snapshots in request order. 按请求顺序返回的托管属性快照。

GetRangeAttributes(int, int, params CudaMemoryRangeAttribute[])

Queries multiple scalar CUDA managed-memory range attributes for a subrange of this allocation. 查询当前分配子范围的多个 CUDA managed memory range 标量属性。

public CudaMemoryRangeAttributeValue[] GetRangeAttributes(int offset, int count, params CudaMemoryRangeAttribute[] attributes)

Parameters

offset int

The byte offset within this allocation. 当前分配内的字节偏移。

count int

The number of bytes to query. 要查询的字节数。

attributes CudaMemoryRangeAttribute[]

The scalar memory range attributes. 标量 memory range 属性列表。

Returns

CudaMemoryRangeAttributeValue[]

Managed attribute snapshots in request order. 按请求顺序返回的托管属性快照。

GetRangeDiagnosticSummary(params CudaMemoryRangeAttribute[])

Gets a compact copied diagnostic summary for CUDA managed-memory range queries. 获取 CUDA managed-memory range 查询的紧凑复制型诊断摘要。

public CudaMemoryRangeDiagnosticSummary GetRangeDiagnosticSummary(params CudaMemoryRangeAttribute[] attributes)

Parameters

attributes CudaMemoryRangeAttribute[]

The scalar attributes to query. 要查询的标量属性。

Returns

CudaMemoryRangeDiagnosticSummary

A pointer-free copied memory range diagnostic summary. 无指针逃逸的复制型 memory range 诊断摘要。

Remarks

This method copies scalar attributes and accessed-by device ids into managed values. It does not expose native memory pointers and does not promote local diagnostics to runtime or package-consumer proof. 该方法将标量属性和 accessed-by 设备序号复制到托管值;不暴露原生内存指针,也不会将本地诊断 晋级为 runtime 或 package-consumer proof。

GetRangeDiagnosticSummary(int, int, bool, bool, params CudaMemoryRangeAttribute[])

Gets a compact copied diagnostic summary for a CUDA managed-memory subrange. 获取 CUDA managed-memory 子范围的紧凑复制型诊断摘要。

public CudaMemoryRangeDiagnosticSummary GetRangeDiagnosticSummary(int offset, int count, bool adviceControlAttempted, bool prefetchControlAttempted, params CudaMemoryRangeAttribute[] attributes)

Parameters

offset int

The byte offset within this allocation. 当前分配内的字节偏移。

count int

The number of bytes to query. 要查询的字节数。

adviceControlAttempted bool

Whether memory advice control was attempted before this summary. 快照前是否尝试过 memory advice 控制。

prefetchControlAttempted bool

Whether memory prefetch control was attempted before this summary. 快照前是否尝试过 memory prefetch 控制。

attributes CudaMemoryRangeAttribute[]

The scalar attributes to query. 要查询的标量属性。

Returns

CudaMemoryRangeDiagnosticSummary

A pointer-free copied memory range diagnostic summary. 无指针逃逸的复制型 memory range 诊断摘要。

ImportIpcDescriptor(CudaIpcMemoryExportDescriptor)

Opens a process-local mapping from an exported CUDA IPC memory descriptor. 从导出的 CUDA IPC memory descriptor 打开进程内 mapping。

public static CudaMemory ImportIpcDescriptor(CudaIpcMemoryExportDescriptor descriptor)

Parameters

descriptor CudaIpcMemoryExportDescriptor

The token and exact allocation size transported from another process. 从其他进程传输的 token 与精确 allocation 大小。

Returns

CudaMemory

An owner wrapper that closes the mapping with cudaIpcCloseMemHandle. 使用 cudaIpcCloseMemHandle 关闭 mapping 的 owner wrapper。

PrefetchAsync(int, CudaStream)

Asynchronously prefetches the entire allocation to a target CUDA device. 异步将整个分配预取到目标 CUDA 设备。

public void PrefetchAsync(int destinationDevice, CudaStream stream)

Parameters

destinationDevice int

The target CUDA device ordinal. 目标 CUDA 设备序号。

stream CudaStream

The CUDA stream that orders the prefetch. 用于排序预取操作的 CUDA stream。

PrefetchAsync(int, int, CudaStream)

Asynchronously prefetches the first bytes of the allocation to a target device. 异步将分配起始处的一部分预取到目标设备。

public void PrefetchAsync(int count, int destinationDevice, CudaStream stream)

Parameters

count int

The number of bytes to prefetch. 要预取的字节数。

destinationDevice int

The target CUDA device ordinal. 目标 CUDA 设备序号。

stream CudaStream

The CUDA stream that orders the prefetch. 用于排序预取操作的 CUDA stream。

PrefetchAsync(int, int, int, CudaStream)

Asynchronously prefetches part of the allocation to a target device. 异步将分配的一部分预取到目标设备。

public void PrefetchAsync(int offset, int count, int destinationDevice, CudaStream stream)

Parameters

offset int

The byte offset within this allocation. 当前分配内的字节偏移。

count int

The number of bytes to prefetch. 要预取的字节数。

destinationDevice int

The target CUDA device ordinal. 目标 CUDA 设备序号。

stream CudaStream

The CUDA stream that orders the prefetch. 用于排序预取操作的 CUDA stream。

ToArray(int)

Materializes a byte array from device memory. 从设备内存中生成字节数组。

public byte[] ToArray(int count)

Parameters

count int

The number of bytes to copy. 要复制的字节数。

Returns

byte[]

A managed byte array copy. 托管字节数组副本。

ToSingleArray(int)

Materializes a float array from device memory. 从设备内存中生成浮点数组。

public float[] ToSingleArray(int elementCount)

Parameters

elementCount int

The number of single-precision elements to copy. 要复制的单精度元素数量。

Returns

float[]

A managed float array copy. 托管浮点数组副本。

TryExportIpcToken(out CudaIpcExportToken?, out string)

Tries to copy an IPC export token and returns a diagnostic on failure. 尝试复制 IPC 导出 token,失败时返回诊断。

public bool TryExportIpcToken(out CudaIpcExportToken? token, out string diagnostic)

Parameters

token CudaIpcExportToken
diagnostic string

Returns

bool

TryImportIpcDescriptor(CudaIpcMemoryExportDescriptor, out CudaMemory?, out string)

Tries to import a CUDA IPC memory descriptor and returns a diagnostic on CUDA failure. 尝试导入 CUDA IPC memory descriptor,并在 CUDA 失败时返回诊断。

public static bool TryImportIpcDescriptor(CudaIpcMemoryExportDescriptor descriptor, out CudaMemory? memory, out string diagnostic)

Parameters

descriptor CudaIpcMemoryExportDescriptor
memory CudaMemory
diagnostic string

Returns

bool

ValidateMemoryAdvice(CudaMemoryAdvice, string)

Validates a CUDA memory-advice value. 验证 CUDA 内存建议值。

protected static void ValidateMemoryAdvice(CudaMemoryAdvice advice, string parameterName)

Parameters

advice CudaMemoryAdvice

The memory advice to validate. 要验证的内存建议。

parameterName string

The parameter name used by exceptions. 异常使用的参数名。

ValidateRange(int, int, string, string)

Validates a non-empty byte range within this allocation. 验证当前分配内的非空字节范围。

protected void ValidateRange(int offset, int count, string offsetParameterName, string countParameterName)

Parameters

offset int

The byte offset to validate. 要验证的字节偏移。

count int

The byte count to validate. 要验证的字节数。

offsetParameterName string

The offset parameter name used by exceptions. 异常使用的 offset 参数名。

countParameterName string

The count parameter name used by exceptions. 异常使用的 count 参数名。