Class CudaMemory
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
sizeInBytesintThe 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
SizeInBytes
Gets the allocation size in bytes. 获取分配大小,单位为字节。
public int SizeInBytes { get; }
Property Value
Methods
Advise(CudaMemoryAdvice, int)
Applies a CUDA memory advice to the entire allocation. 对整个分配应用 CUDA memory advice。
public void Advise(CudaMemoryAdvice advice, int device)
Parameters
adviceCudaMemoryAdviceThe CUDA memory advice. CUDA 内存建议。
deviceintThe 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
countintThe number of bytes covered by the advice. advice 覆盖的字节数。
adviceCudaMemoryAdviceThe CUDA memory advice. CUDA 内存建议。
deviceintThe 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
offsetintThe byte offset within this allocation. 当前分配内的字节偏移。
countintThe number of bytes covered by the advice. advice 覆盖的字节数。
adviceCudaMemoryAdviceThe CUDA memory advice. CUDA 内存建议。
deviceintThe 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
sizeInBytesintThe allocation size in bytes. 分配大小,单位为字节。
streamCudaStreamThe 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
sizeInBytesintThe allocation size in bytes. 分配大小,单位为字节。
memoryPoolCudaMemoryPoolThe CUDA memory pool used for allocation. 用于分配的 CUDA memory pool。
streamCudaStreamThe 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
sourcebyte[]The source byte array. 源字节数组。
CopyFrom(float[])
Copies a float array into device memory. 将浮点数组复制到设备内存中。
public void CopyFrom(float[] source)
Parameters
sourcefloat[]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
sourceCudaPinnedMemoryThe source pinned host buffer. 源 pinned host 缓冲区。
streamCudaStreamThe 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
sourceCudaPinnedMemoryThe source pinned host buffer. 源 pinned host 缓冲区。
countintThe number of bytes to copy. 要复制的字节数。
streamCudaStreamThe 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
sourceCudaRegisteredHostMemoryThe source registered host buffer. 源 registered host 缓冲区。
countintThe number of bytes to copy. 要复制的字节数。
streamCudaStreamThe 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
destinationCudaMemoryThe destination device allocation. 目标设备内存分配。
CopyTo(CudaMemory, int)
Copies bytes from this allocation to another device allocation. 将当前分配中的字节复制到另一个设备分配中。
public void CopyTo(CudaMemory destination, int count)
Parameters
destinationCudaMemoryThe destination device allocation. 目标设备内存分配。
countintThe number of bytes to copy. 要复制的字节数。
CopyTo(byte[])
Copies device memory into a byte array. 将设备内存复制到字节数组中。
public void CopyTo(byte[] destination)
Parameters
destinationbyte[]The destination byte array. 目标字节数组。
CopyTo(float[])
Copies device memory into a float array. 将设备内存复制到浮点数组中。
public void CopyTo(float[] destination)
Parameters
destinationfloat[]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
destinationCudaMemoryThe destination device allocation. 目标设备内存分配。
streamCudaStreamThe 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
destinationCudaMemoryThe destination device allocation. 目标设备内存分配。
countintThe number of bytes to copy. 要复制的字节数。
streamCudaStreamThe 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
destinationCudaPinnedMemoryThe destination pinned host buffer. 目标 pinned host 缓冲区。
streamCudaStreamThe 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
destinationCudaPinnedMemoryThe destination pinned host buffer. 目标 pinned host 缓冲区。
countintThe number of bytes to copy. 要复制的字节数。
streamCudaStreamThe 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
destinationCudaRegisteredHostMemoryThe destination registered host buffer. 目标 registered host 缓冲区。
countintThe number of bytes to copy. 要复制的字节数。
streamCudaStreamThe 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
destinationCudaMemoryThe destination allocation. 目标设备内存分配。
countintThe 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
destinationCudaMemoryThe destination allocation. 目标设备内存分配。
countintThe number of bytes to copy. 要复制的字节数。
streamCudaStreamThe 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
destinationCudaMemoryThe destination allocation. 目标设备内存分配。
sourceDeviceintThe CUDA device ordinal that owns this source allocation. 拥有当前源分配的 CUDA 设备序号。
destinationDeviceintThe CUDA device ordinal that owns the destination allocation. 拥有目标分配的 CUDA 设备序号。
countintThe 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
destinationCudaMemoryThe destination allocation. 目标设备内存分配。
sourceDeviceintThe CUDA device ordinal that owns this source allocation. 拥有当前源分配的 CUDA 设备序号。
destinationDeviceintThe CUDA device ordinal that owns the destination allocation. 拥有目标分配的 CUDA 设备序号。
countintThe number of bytes to copy. 要复制的字节数。
streamCudaStreamThe 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
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
valuebyteThe fill byte value. 填充值。
Fill(byte, int)
Fills part of the allocation with a byte value. 使用一个字节值填充部分分配区域。
public void Fill(byte value, int count)
Parameters
FillAsync(byte, CudaStream)
Asynchronously fills the entire allocation with a byte value. 异步使用一个字节值填充整个设备内存分配。
public void FillAsync(byte value, CudaStream stream)
Parameters
valuebyteThe fill byte value. 填充值。
streamCudaStreamThe 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
valuebyteThe fill byte value. 填充值。
countintThe number of bytes to fill. 要填充的字节数。
streamCudaStreamThe 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
streamCudaStreamThe 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
offsetintThe byte offset within this allocation. 当前分配内的字节偏移。
countintThe 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
attributeCudaMemoryRangeAttributeThe 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
attributeCudaMemoryRangeAttributeThe scalar memory range attribute. 标量 memory range 属性。
offsetintThe byte offset within this allocation. 当前分配内的字节偏移。
countintThe 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
attributesCudaMemoryRangeAttribute[]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
offsetintThe byte offset within this allocation. 当前分配内的字节偏移。
countintThe number of bytes to query. 要查询的字节数。
attributesCudaMemoryRangeAttribute[]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
attributesCudaMemoryRangeAttribute[]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
offsetintThe byte offset within this allocation. 当前分配内的字节偏移。
countintThe number of bytes to query. 要查询的字节数。
adviceControlAttemptedboolWhether memory advice control was attempted before this summary. 快照前是否尝试过 memory advice 控制。
prefetchControlAttemptedboolWhether memory prefetch control was attempted before this summary. 快照前是否尝试过 memory prefetch 控制。
attributesCudaMemoryRangeAttribute[]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
descriptorCudaIpcMemoryExportDescriptorThe 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
destinationDeviceintThe target CUDA device ordinal. 目标 CUDA 设备序号。
streamCudaStreamThe 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
countintThe number of bytes to prefetch. 要预取的字节数。
destinationDeviceintThe target CUDA device ordinal. 目标 CUDA 设备序号。
streamCudaStreamThe 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
offsetintThe byte offset within this allocation. 当前分配内的字节偏移。
countintThe number of bytes to prefetch. 要预取的字节数。
destinationDeviceintThe target CUDA device ordinal. 目标 CUDA 设备序号。
streamCudaStreamThe CUDA stream that orders the prefetch. 用于排序预取操作的 CUDA stream。
ToArray(int)
Materializes a byte array from device memory. 从设备内存中生成字节数组。
public byte[] ToArray(int count)
Parameters
countintThe 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
elementCountintThe 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
tokenCudaIpcExportTokendiagnosticstring
Returns
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
descriptorCudaIpcMemoryExportDescriptormemoryCudaMemorydiagnosticstring
Returns
ValidateMemoryAdvice(CudaMemoryAdvice, string)
Validates a CUDA memory-advice value. 验证 CUDA 内存建议值。
protected static void ValidateMemoryAdvice(CudaMemoryAdvice advice, string parameterName)
Parameters
adviceCudaMemoryAdviceThe memory advice to validate. 要验证的内存建议。
parameterNamestringThe 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)