6.33. C++ API 例程

本节介绍CUDA运行时应用程序编程接口的C++高级API函数。要使用这些函数,您的应用程序需要使用nvcc编译器进行编译。

class 

Functions

template < class T >__host__cudaChannelFormatDesc cudaCreateChannelDesc ( void )
[C++ API] Returns a channel descriptor using the specified format
__host__cudaError_t cudaEventCreate ( cudaEvent_t* event, unsigned int  flags )
[C++ API] Creates an event object with the specified flags
template < class T >__host__cudaError_t cudaFuncGetAttributes ( cudaFuncAttributes* attr, T* entry )
[C++ API] Find out attributes for a given function
template < class T >__host__cudaError_t cudaFuncGetName ( const char** name, T* func )
Returns the function name for a device entry function pointer.
template < class T >__host__cudaError_t cudaFuncSetAttribute ( T* func, cudaFuncAttribute attr, int  value )
[C++ API] Set attributes for a given function
template < class T >__host__cudaError_t cudaFuncSetCacheConfig ( T* func, cudaFuncCache cacheConfig )
[C++ API] Sets the preferred cache configuration for a device function
template < class T >__host__cudaError_t cudaGetKernel ( cudaKernel_t* kernelPtr, T* func )
Get pointer to device kernel that matches entry function entryFuncAddr.
template < class T >__host__cudaError_t cudaGetSymbolAddress ( void** devPtr, const T& symbol )
[C++ API] Finds the address associated with a CUDA symbol
template < class T >__host__cudaError_t cudaGetSymbolSize ( size_t* size, const T& symbol )
[C++ API] Finds the size of the object associated with a CUDA symbol
template < class T >__host__cudaError_t cudaGraphAddMemcpyNodeFromSymbol ( cudaGraphNode_t* pGraphNode, cudaGraph_t graph, const cudaGraphNode_t* pDependencies, size_t numDependencies, void* dst, const T& symbol, size_t count, size_t offset, cudaMemcpyKind kind )
Creates a memcpy node to copy from a symbol on the device and adds it to a graph.
template < class T >__host__cudaError_t cudaGraphAddMemcpyNodeToSymbol ( cudaGraphNode_t* pGraphNode, cudaGraph_t graph, const cudaGraphNode_t* pDependencies, size_t numDependencies, const T& symbol, const void* src, size_t count, size_t offset, cudaMemcpyKind kind )
Creates a memcpy node to copy to a symbol on the device and adds it to a graph.
template < class T >__host__cudaError_t cudaGraphExecMemcpyNodeSetParamsFromSymbol ( cudaGraphExec_t hGraphExec, cudaGraphNode_t node, void* dst, const T& symbol, size_t count, size_t offset, cudaMemcpyKind kind )
Sets the parameters for a memcpy node in the given graphExec to copy from a symbol on the device.
template < class T >__host__cudaError_t cudaGraphExecMemcpyNodeSetParamsToSymbol ( cudaGraphExec_t hGraphExec, cudaGraphNode_t node, const T& symbol, const void* src, size_t count, size_t offset, cudaMemcpyKind kind )
Sets the parameters for a memcpy node in the given graphExec to copy to a symbol on the device.
__host__cudaError_t cudaGraphInstantiate ( cudaGraphExec_t* pGraphExec, cudaGraph_t graph, cudaGraphNode_t* pErrorNode, char* pLogBuffer, size_t bufferSize )
Creates an executable graph from a graph.
template < class T >__host__cudaError_t cudaGraphMemcpyNodeSetParamsFromSymbol ( cudaGraphNode_t node, void* dst, const T& symbol, size_t count, size_t offset, cudaMemcpyKind kind )
Sets a memcpy node's parameters to copy from a symbol on the device.
template < class T >__host__cudaError_t cudaGraphMemcpyNodeSetParamsToSymbol ( cudaGraphNode_t node, const T& symbol, const void* src, size_t count, size_t offset, cudaMemcpyKind kind )
Sets a memcpy node's parameters to copy to a symbol on the device.
template < class T >__host__cudaError_t cudaLaunchCooperativeKernel ( T* func, dim3 gridDim, dim3 blockDim, void** args, size_t sharedMem = 0, cudaStream_t stream = 0 )
Launches a device function.
template < class T >__host__cudaError_t cudaLaunchKernel ( T* func, dim3 gridDim, dim3 blockDim, void** args, size_t sharedMem = 0, cudaStream_t stream = 0 )
Launches a device function.
template < typename... ActTypes >__host__cudaError_t cudaLaunchKernelEx ( const cudaLaunchConfig_t* config, const cudaKernel_t kernel, ActTypes &&... args )
Launches a CUDA function with launch-time configuration.
template < typename... ExpTypes, typename... ActTypes >__host__cudaError_t cudaLaunchKernelEx ( const cudaLaunchConfig_t* config, void(*)(ExpTypes...) kernel, ActTypes &&... args )
Launches a CUDA function with launch-time configuration.
template < class T >__host__cudaError_t cudaLibraryGetGlobal ( T** dptr, size_t* bytes, cudaLibrary_t library, const char* name )
Returns a global device pointer.
template < class T >__host__cudaError_t cudaLibraryGetManaged ( T** dptr, size_t* bytes, cudaLibrary_t library, const char* name )
Returns a pointer to managed memory.
template < class T >__host__cudaError_t cudaLibraryGetUnifiedFunction ( T** fptr, cudaLibrary_t library, const char* symbol )
Returns a pointer to a unified function.
__host__cudaError_t cudaMallocAsync ( void** ptr, size_t size, cudaMemPool_t memPool, cudaStream_t stream )
Allocate from a pool.
__host__cudaError_t cudaMallocHost ( void** ptr, size_t size, unsigned int  flags )
[C++ API] Allocates page-locked memory on the host
template < class T >__host__cudaError_t cudaMallocManaged ( T** devPtr, size_t size, unsigned int  flags = cudaMemAttachGlobal )
Allocates memory that will be automatically managed by the Unified Memory system.
template < class T >cudaError_t cudaMemAdvise ( T* devPtr, size_t count, cudaMemoryAdvise advice, cudaMemLocation location )
Advise about the usage of a given memory range.
template < typename T, typename U >__host__cudaError_t cudaMemcpyBatchAsync ( T** dsts, U** srcs, size_t* sizes, size_t count, cudaMemcpyAttributes attr, size_t* failIdx, cudaStream_t hStream )
Performs a batch of memory copies asynchronously.
template < typename T, typename U >__host__cudaError_t cudaMemcpyBatchAsync ( T** dsts, U** srcs, size_t* sizes, size_t count, cudaMemcpyAttributes* attrs, size_t* attrsIdxs, size_t numAttrs, size_t* failIdx, cudaStream_t hStream )
Performs a batch of memory copies asynchronously.
template < class T >__host__cudaError_t cudaMemcpyFromSymbol ( void* dst, const T& symbol, size_t count, size_t offset = 0, cudaMemcpyKind kind = cudaMemcpyDeviceToHost )
[C++ API] Copies data from the given symbol on the device
template < class T >__host__cudaError_t cudaMemcpyFromSymbolAsync ( void* dst, const T& symbol, size_t count, size_t offset = 0, cudaMemcpyKind kind = cudaMemcpyDeviceToHost, cudaStream_t stream = 0 )
[C++ API] Copies data from the given symbol on the device
template < class T >__host__cudaError_t cudaMemcpyToSymbol ( const T& symbol, const void* src, size_t count, size_t offset = 0, cudaMemcpyKind kind = cudaMemcpyHostToDevice )
[C++ API] Copies data to the given symbol on the device
template < class T >__host__cudaError_t cudaMemcpyToSymbolAsync ( const T& symbol, const void* src, size_t count, size_t offset = 0, cudaMemcpyKind kind = cudaMemcpyHostToDevice, cudaStream_t stream = 0 )
[C++ API] Copies data to the given symbol on the device
template < class T >__host__cudaError_t cudaOccupancyAvailableDynamicSMemPerBlock ( size_t* dynamicSmemSize, T* func, int  numBlocks, int  blockSize )
Returns dynamic shared memory available per block when launching numBlocks blocks on SM.
template < class T >__host__cudaError_t cudaOccupancyMaxActiveBlocksPerMultiprocessor ( int* numBlocks, T func, int  blockSize, size_t dynamicSMemSize )
Returns occupancy for a device function.
template < class T >__host__cudaError_t cudaOccupancyMaxActiveBlocksPerMultiprocessorWithFlags ( int* numBlocks, T func, int  blockSize, size_t dynamicSMemSize, unsigned int  flags )
Returns occupancy for a device function with the specified flags.
template < class T >__host__cudaError_t cudaOccupancyMaxActiveClusters ( int* numClusters, T* func, const cudaLaunchConfig_t* config )
Given the kernel function (func) and launch configuration (config), return the maximum number of clusters that could co-exist on the target device in *numClusters.
template < class T >__host__cudaError_t cudaOccupancyMaxPotentialBlockSize ( int* minGridSize, int* blockSize, T func, size_t dynamicSMemSize = 0, int  blockSizeLimit = 0 )
Returns grid and block size that achieves maximum potential occupancy for a device function.
template < typename UnaryFunction, class T >__host__cudaError_t cudaOccupancyMaxPotentialBlockSizeVariableSMem ( int* minGridSize, int* blockSize, T func, UnaryFunction blockSizeToDynamicSMemSize, int  blockSizeLimit = 0 )
Returns grid and block size that achieves maximum potential occupancy for a device function.
template < typename UnaryFunction, class T >__host__cudaError_t cudaOccupancyMaxPotentialBlockSizeVariableSMemWithFlags ( int* minGridSize, int* blockSize, T func, UnaryFunction blockSizeToDynamicSMemSize, int  blockSizeLimit = 0, unsigned int  flags = 0 )
Returns grid and block size that achieves maximum potential occupancy for a device function.
template < class T >__host__cudaError_t cudaOccupancyMaxPotentialBlockSizeWithFlags ( int* minGridSize, int* blockSize, T func, size_t dynamicSMemSize = 0, int  blockSizeLimit = 0, unsigned int  flags = 0 )
Returns grid and block size that achived maximum potential occupancy for a device function with the specified flags.
template < class T >__host__cudaError_t cudaOccupancyMaxPotentialClusterSize ( int* clusterSize, T* func, const cudaLaunchConfig_t* config )
Given the kernel function (func) and launch configuration (config), return the maximum cluster size in *clusterSize.
template < class T >__host__cudaError_t cudaStreamAttachMemAsync ( cudaStream_t stream, T* devPtr, size_t length = 0, unsigned int  flags = cudaMemAttachSingle )
Attach memory to a stream asynchronously.

Functions

模板 < 类 T >

__host__cudaChannelFormatDesc cudaCreateChannelDesc ( void ) [inline]
[C++ API] 返回使用指定格式的通道描述符
返回

具有格式f的通道描述符

描述

返回一个通道描述符,其格式为f,各分量xyzw的位数。该cudaChannelFormatDesc定义为:

‎  struct cudaChannelFormatDesc {
          int x, y, z, w;
          enum cudaChannelFormatKind 
                  f;
        };

其中cudaChannelFormatKind可以是以下之一:cudaChannelFormatKindSignedcudaChannelFormatKindUnsigned、cudaChannelFormatKindFloat、cudaChannelFormatKindSignedNormalized8X1cudaChannelFormatKindSignedNormalized8X2cudaChannelFormatKindSignedNormalized8X4cudaChannelFormatKindUnsignedNormalized8X1cudaChannelFormatKindUnsignedNormalized8X2cudaChannelFormatKindUnsignedNormalized8X4cudaChannelFormatKindSignedNormalized16X1cudaChannelFormatKindSignedNormalized16X2cudaChannelFormatKindSignedNormalized16X4cudaChannelFormatKindUnsignedNormalized16X1cudaChannelFormatKindUnsignedNormalized16X2cudaChannelFormatKindUnsignedNormalized16X4cudaChannelFormatKindUnsignedNormalized1010102cudaChannelFormatKindNV12

格式由模板特化指定。

该模板函数针对以下标量类型进行了特化:char、signed char、unsigned char、short、unsigned short、int、unsigned int、long、unsigned long和float。该模板函数针对以下向量类型进行了特化:char{1|2|4}、uchar{1|2|4}、short{1|2|4}、ushort{1|2|4}、int{1|2|4}、uint{1|2|4}、long{1|2|4}、ulong{1|2|4}、float{1|2|4}。该模板函数针对以下cudaChannelFormatKind枚举值进行了特化:cudaChannelFormatKind{Uns|S}ignedNormalized{8|16}X{1|2|4}、cudaChannelFormatKindUnsignedNormalized1010102cudaChannelFormatKindNV12

在没有特化的情况下调用该函数,默认会创建一个类型为cudaChannelFormatKindNone的通道格式

另请参阅:

cudaCreateChannelDesc (底层), cudaGetChannelDesc,

__host__cudaError_t cudaEventCreate ( cudaEvent_t* event, unsigned int  flags )
[C++ API] 使用指定标志创建事件对象
参数
event
- Newly created event
flags
- Flags for new event
描述

创建一个具有指定标志的事件对象。有效标志包括:

Note:

另请参阅:

cudaEventCreate ( C API), cudaEventCreateWithFlags, cudaEventRecord, cudaEventQuery, cudaEventSynchronize, cudaEventDestroy, cudaEventElapsedTime, cudaStreamWaitEvent

模板 < 类 T >

__host__cudaError_t cudaFuncGetAttributes ( cudaFuncAttributes* attr, T* entry ) [inline]
[C++ API] 查找给定函数的属性
参数
attr
- Return pointer to function's attributes
entry
- Function to get attributes of
描述

此函数获取通过entry指定的函数属性。参数entry必须是指向在设备上执行的函数的指针。由entry指定的参数必须声明为__global__函数。获取的属性将被放入attr中。如果指定的函数不存在,则返回cudaErrorInvalidDeviceFunction

请注意,某些函数属性(如maxThreadsPerBlock)可能会根据当前使用的设备而有所不同。

Note:

cudaLaunchKernel ( C++ API), cudaFuncSetCacheConfig ( C++ API), cudaFuncGetAttributes ( C API), cudaSetDoubleForDevice, cudaSetDoubleForHost

模板 < 类 T >

__host__cudaError_t cudaFuncGetName ( const char** name, T* func ) [inline]
返回设备入口函数指针对应的函数名称。
参数
name
- The returned name of the function
func
- The function pointer to retrieve name for
描述

返回与符号func关联的函数名称到**name中。函数名称以空终止字符串形式返回。如果函数未声明为具有C链接,此API可能返回一个混淆名称。如果**name为NULL,则返回cudaErrorInvalidValue。如果func不是设备入口函数,则返回cudaErrorInvalidDeviceFunction

Note:

cudaFuncGetName ( C API)

模板 < 类 T >

__host__cudaError_t cudaFuncSetAttribute ( T* func, cudaFuncAttribute attr, int  value ) [inline]
[C++ API] 为指定函数设置属性
参数
func
attr
- Attribute to set
value
- Value to set
描述

此函数用于设置通过entry指定的函数属性。参数entry必须是指向在设备上执行的函数的指针。由entry指定的参数必须声明为__global__函数。将attr定义的枚举设置为value定义的值。如果指定的函数不存在,则返回cudaErrorInvalidDeviceFunction。如果无法写入指定属性,或值不正确,则返回cudaErrorInvalidValue

attr的有效取值包括:

Note:

cudaLaunchKernel ( C++ API), cudaFuncSetCacheConfig ( C++ API), cudaFuncGetAttributes ( C API), cudaSetDoubleForDevice, cudaSetDoubleForHost

模板 < 类 T >

__host__cudaError_t cudaFuncSetCacheConfig ( T* func, cudaFuncCache cacheConfig ) [inline]
[C++ API] 为设备函数设置首选缓存配置
参数
func
- device function pointer
cacheConfig
- Requested cache configuration
描述

在L1缓存和共享内存使用相同硬件资源的设备上,通过cacheConfig设置由func指定的函数的首选缓存配置。这只是一个偏好设置。如果可能,运行时将使用请求的配置,但若需要执行func,运行时可以自由选择不同的配置。

func 必须是一个指向在设备上执行的函数的指针。由 func 指定的参数必须声明为 __global__ 函数。如果指定的函数不存在,则会返回 cudaErrorInvalidDeviceFunction

在L1缓存和共享内存大小固定的设备上,此设置不起作用。

使用与最近偏好设置不同的偏好启动内核可能会插入一个设备端同步点。

支持的缓存配置包括:

Note:

cudaLaunchKernel ( C++ API), cudaFuncSetCacheConfig ( C API), cudaFuncGetAttributes ( C++ API), cudaSetDoubleForDevice, cudaSetDoubleForHost, cudaThreadGetCacheConfig, cudaThreadSetCacheConfig

模板 < 类 T >

__host__cudaError_t cudaGetKernel ( cudaKernel_t* kernelPtr, T* func ) [inline]
Get pointer to device kernel that matches entry function entryFuncAddr.
参数
kernelPtr
- Returns the device kernel
func
返回

cudaSuccess

描述

kernelPtr中返回与入口函数entryFuncAddr对应的设备内核。

另请参阅:

cudaGetKernel ( C API)

模板 < 类 T >

__host__cudaError_t cudaGetSymbolAddress ( void** devPtr, const T& symbol ) [inline]
[C++ API] 查找与CUDA符号关联的地址
参数
devPtr
- Return device pointer associated with symbol
symbol
- Device symbol reference
描述

返回设备上符号symbol*devPtr中的地址。symbol可以是位于全局或常量内存空间中的变量。如果找不到symbol,或者symbol未在全局或常量内存空间中声明,则*devPtr保持不变,并返回错误cudaErrorInvalidSymbol

Note:

另请参阅:

cudaGetSymbolAddress ( C API), cudaGetSymbolSize ( C++ API)

模板 < 类 T >

__host__cudaError_t cudaGetSymbolSize ( size_t* size, const T& symbol ) [inline]
[C++ API] 查找与CUDA符号关联的对象大小
参数
size
- Size of object associated with symbol
symbol
- Device symbol reference
描述

返回符号symbol的大小到*size中。symbol必须是位于全局或常量内存空间中的变量。如果找不到symbol,或者symbol未在全局或常量内存空间中声明,则*size保持不变并返回错误cudaErrorInvalidSymbol

Note:

另请参阅:

cudaGetSymbolAddress ( C++ API), cudaGetSymbolSize ( C API)

模板 < 类 T >

__host__cudaError_t cudaGraphAddMemcpyNodeFromSymbol ( cudaGraphNode_t* pGraphNode, cudaGraph_t graph, const cudaGraphNode_t* pDependencies, size_t numDependencies, void* dst, const T& symbol, size_t count, size_t offset, cudaMemcpyKind kind ) [inline]
创建一个memcpy节点以从设备上的符号复制数据,并将其添加到图中。
参数
pGraphNode
- Returns newly created node
graph
- Graph to which to add the node
pDependencies
- Dependencies of the node
numDependencies
- Number of dependencies
dst
- Destination memory address
symbol
- Device symbol address
count
- Size in bytes to copy
offset
- Offset from start of symbol in bytes
kind
- Type of transfer
描述

创建一个新的memcpy节点,用于从symbol复制数据,并通过pDependencies指定的numDependencies个依赖项将其添加到graph中。numDependencies可以为0,此时该节点将被放置在图的根位置。pDependencies中不能有任何重复条目。新节点的句柄将在pGraphNode中返回。

当图启动时,该节点将从符号symbol起始位置偏移offset字节所指向的内存区域复制count字节数据到dst指向的内存区域。内存区域之间不得重叠。symbol是驻留在全局或常量内存空间中的变量。kind可以是cudaMemcpyDeviceToHostcudaMemcpyDeviceToDevicecudaMemcpyDefault。建议传递cudaMemcpyDefault,此时传输类型会根据指针值自动推断。但cudaMemcpyDefault仅在支持统一虚拟寻址的系统上可用。

如果系统中至少有一个设备的属性cudaDevAttrConcurrentManagedAccess值为零,则内存拷贝节点对托管内存有一些额外限制。

Note:

另请参阅:

cudaMemcpyFromSymbol, cudaGraphAddMemcpyNode, cudaGraphAddMemcpyNodeToSymbol, cudaGraphMemcpyNodeGetParams, cudaGraphMemcpyNodeSetParams, cudaGraphMemcpyNodeSetParamsFromSymbol, cudaGraphMemcpyNodeSetParamsToSymbol, cudaGraphCreate, cudaGraphDestroyNode, cudaGraphAddChildGraphNode, cudaGraphAddEmptyNode, cudaGraphAddKernelNode, cudaGraphAddHostNode, cudaGraphAddMemsetNode

模板 < 类 T >

__host__cudaError_t cudaGraphAddMemcpyNodeToSymbol ( cudaGraphNode_t* pGraphNode, cudaGraph_t graph, const cudaGraphNode_t* pDependencies, size_t numDependencies, const T& symbol, const void* src, size_t count, size_t offset, cudaMemcpyKind kind ) [inline]
创建一个memcpy节点以复制到设备上的符号,并将其添加到图中。
参数
pGraphNode
- Returns newly created node
graph
- Graph to which to add the node
pDependencies
- Dependencies of the node
numDependencies
- Number of dependencies
symbol
- Device symbol address
src
- Source memory address
count
- Size in bytes to copy
offset
- Offset from start of symbol in bytes
kind
- Type of transfer
描述

创建一个新的memcpy节点以复制到symbol,并通过pDependencies指定的numDependencies个依赖项将其添加到graph中。numDependencies可以为0,此时该节点将被放置在图的根位置。pDependencies不能包含任何重复条目。新节点的句柄将在pGraphNode中返回。

当图形启动时,该节点将从src指向的内存区域复制count字节到符号symbol起始位置偏移offset字节指向的内存区域。内存区域不得重叠。symbol是驻留在全局或常量内存空间中的变量。kind可以是cudaMemcpyHostToDevicecudaMemcpyDeviceToDevicecudaMemcpyDefault。建议传递cudaMemcpyDefault,此时传输类型将从指针值推断得出。但cudaMemcpyDefault仅在支持统一虚拟寻址的系统上允许使用。

如果系统中至少有一个设备的属性cudaDevAttrConcurrentManagedAccess值为零,则内存拷贝节点对托管内存有一些额外限制。

Note:

另请参阅:

cudaMemcpyToSymbol, cudaGraphAddMemcpyNode, cudaGraphAddMemcpyNodeFromSymbol, cudaGraphMemcpyNodeGetParams, cudaGraphMemcpyNodeSetParams, cudaGraphMemcpyNodeSetParamsToSymbol, cudaGraphMemcpyNodeSetParamsFromSymbol, cudaGraphCreate, cudaGraphDestroyNode, cudaGraphAddChildGraphNode, cudaGraphAddEmptyNode, cudaGraphAddKernelNode, cudaGraphAddHostNode, cudaGraphAddMemsetNode

模板 < 类 T >

__host__cudaError_t cudaGraphExecMemcpyNodeSetParamsFromSymbol ( cudaGraphExec_t hGraphExec, cudaGraphNode_t node, void* dst, const T& symbol, size_t count, size_t offset, cudaMemcpyKind kind ) [inline]
设置给定graphExec中memcpy节点的参数,以便从设备上的符号进行复制。
参数
hGraphExec
- The executable graph in which to set the specified node
node
- Memcpy node from the graph which was used to instantiate graphExec
dst
- Destination memory address
symbol
- Device symbol address
count
- Size in bytes to copy
offset
- Offset from start of symbol in bytes
kind
- Type of transfer
描述

更新hGraphExec中由node表示的工作,就像node在实例化时包含给定参数一样。node必须保留在用于实例化hGraphExec的图中。忽略对node的进出边所做的更改。

symboldst 必须从与原始源内存和目标内存相同的上下文中分配。实例化时的内存操作数必须是一维的。不支持零长度操作。

这些修改仅影响未来启动的hGraphExec。已排队或正在运行的hGraphExec启动不受此调用影响。node也不会被此调用修改。

如果内存操作数的映射发生更改或原始内存操作数为多维,则返回cudaErrorInvalidValue

Note:

另请参阅:

cudaGraphAddMemcpyNode, cudaGraphAddMemcpyNodeFromSymbol, cudaGraphMemcpyNodeSetParams, cudaGraphMemcpyNodeSetParamsFromSymbol, cudaGraphInstantiate, cudaGraphExecMemcpyNodeSetParams, cudaGraphExecMemcpyNodeSetParamsToSymbol, cudaGraphExecKernelNodeSetParams, cudaGraphExecMemsetNodeSetParams, cudaGraphExecHostNodeSetParams

模板 < 类 T >

__host__cudaError_t cudaGraphExecMemcpyNodeSetParamsToSymbol ( cudaGraphExec_t hGraphExec, cudaGraphNode_t node, const T& symbol, const void* src, size_t count, size_t offset, cudaMemcpyKind kind ) [inline]
设置给定graphExec中memcpy节点的参数,以复制到设备上的符号。
参数
hGraphExec
- The executable graph in which to set the specified node
node
- Memcpy node from the graph which was used to instantiate graphExec
symbol
- Device symbol address
src
- Source memory address
count
- Size in bytes to copy
offset
- Offset from start of symbol in bytes
kind
- Type of transfer
描述

更新hGraphExec中由node表示的工作,就像node在实例化时包含给定参数一样。node必须保留在用于实例化hGraphExec的图中。忽略对node的进出边所做的更改。

srcsymbol 必须从与原始源内存和目标内存相同的上下文中分配。实例化时的内存操作数必须是一维的。不支持零长度操作。

这些修改仅影响未来启动的hGraphExec。已排队或正在运行的hGraphExec启动不受此调用影响。node也不会被此调用修改。

如果内存操作数的映射发生更改或原始内存操作数为多维,则返回cudaErrorInvalidValue

Note:

另请参阅:

cudaGraphAddMemcpyNode, cudaGraphAddMemcpyNodeToSymbol, cudaGraphMemcpyNodeSetParams, cudaGraphMemcpyNodeSetParamsToSymbol, cudaGraphInstantiate, cudaGraphExecMemcpyNodeSetParams, cudaGraphExecMemcpyNodeSetParamsFromSymbol, cudaGraphExecKernelNodeSetParams, cudaGraphExecMemsetNodeSetParams, cudaGraphExecHostNodeSetParams

__host__cudaError_t cudaGraphInstantiate ( cudaGraphExec_t* pGraphExec, cudaGraph_t graph, cudaGraphNode_t* pErrorNode, char* pLogBuffer, size_t bufferSize )
从图形创建可执行图形。
参数
pGraphExec
- Returns instantiated graph
graph
- Graph to instantiate
pErrorNode
- In case of an instantiation error, this may be modified to indicate a node contributing to the error
pLogBuffer
- A character buffer to store diagnostic messages
bufferSize
- Size of the log buffer in bytes
描述

graph实例化为可执行图。该图会针对之前未验证的任何结构约束或节点内约束进行验证。如果实例化成功,实例化图的句柄将在pGraphExec中返回。

如果出现任何错误,诊断信息可能会返回在pErrorNodepLogBuffer中。这是检查实例化错误的主要方式。除非诊断信息超出缓冲区容量,否则输出将以空字符终止。如果发生溢出,信息将被截断,此时可通过检查最后一个字节来判断是否发生了截断。

Note:

另请参阅:

cudaGraphInstantiateWithFlags, cudaGraphCreate, cudaGraphUpload, cudaGraphLaunch, cudaGraphExecDestroy

模板 < 类 T >

__host__cudaError_t cudaGraphMemcpyNodeSetParamsFromSymbol ( cudaGraphNode_t node, void* dst, const T& symbol, size_t count, size_t offset, cudaMemcpyKind kind ) [inline]
设置一个memcpy节点的参数,以从设备上的符号进行复制。
参数
node
- Node to set the parameters for
dst
- Destination memory address
symbol
- Device symbol address
count
- Size in bytes to copy
offset
- Offset from start of symbol in bytes
kind
- Type of transfer
描述

将memcpy节点node的参数设置为由提供的参数描述的复制操作。

当图启动时,该节点将从符号symbol起始位置偏移offset字节所指向的内存区域复制count字节数据到dst指向的内存区域。内存区域之间不得重叠。symbol是驻留在全局或常量内存空间中的变量。kind可以是cudaMemcpyDeviceToHostcudaMemcpyDeviceToDevicecudaMemcpyDefault。建议传递cudaMemcpyDefault,此时传输类型会根据指针值自动推断。但cudaMemcpyDefault仅在支持统一虚拟寻址的系统上可用。

Note:

另请参阅:

cudaMemcpyFromSymbol, cudaGraphMemcpyNodeSetParams, cudaGraphMemcpyNodeSetParamsToSymbol, cudaGraphAddMemcpyNode, cudaGraphMemcpyNodeGetParams

模板 < 类 T >

__host__cudaError_t cudaGraphMemcpyNodeSetParamsToSymbol ( cudaGraphNode_t node, const T& symbol, const void* src, size_t count, size_t offset, cudaMemcpyKind kind ) [inline]
设置内存复制节点的参数,以复制到设备上的符号。
参数
node
- Node to set the parameters for
symbol
- Device symbol address
src
- Source memory address
count
- Size in bytes to copy
offset
- Offset from start of symbol in bytes
kind
- Type of transfer
描述

将memcpy节点node的参数设置为由提供的参数描述的复制操作。

当图形启动时,该节点将从src指向的内存区域复制count字节到符号symbol起始位置偏移offset字节指向的内存区域。内存区域不得重叠。symbol是驻留在全局或常量内存空间中的变量。kind可以是cudaMemcpyHostToDevicecudaMemcpyDeviceToDevicecudaMemcpyDefault。建议传递cudaMemcpyDefault,此时传输类型将从指针值推断得出。但cudaMemcpyDefault仅在支持统一虚拟寻址的系统上允许使用。

Note:

另请参阅:

cudaMemcpyToSymbol, cudaGraphMemcpyNodeSetParams, cudaGraphMemcpyNodeSetParamsFromSymbol, cudaGraphAddMemcpyNode, cudaGraphMemcpyNodeGetParams

模板 < 类 T >

__host__cudaError_t cudaLaunchCooperativeKernel ( T* func, dim3 gridDim, dim3 blockDim, void** args, size_t sharedMem = 0, cudaStream_t stream = 0 ) [inline]
启动一个设备函数。
参数
func
- Device function symbol
gridDim
- Grid dimentions
blockDim
- Block dimentions
args
- Arguments
sharedMem
- Shared memory (defaults to 0)
stream
- Stream identifier (defaults to NULL)
描述

该函数在gridDimgridDim.xgridDim.ygridDim.z)网格块上调用内核func。每个块包含blockDimblockDim.xblockDim.yblockDim.z)个线程。

调用此内核的设备必须具有非零值的设备属性cudaDevAttrCooperativeLaunch

启动的块总数不能超过每个多处理器最大块数(由cudaOccupancyMaxActiveBlocksPerMultiprocessorcudaOccupancyMaxActiveBlocksPerMultiprocessorWithFlags返回)乘以设备属性cudaDevAttrMultiProcessorCount指定的多处理器数量。

该内核无法利用CUDA动态并行功能。

如果内核有N个参数,args应指向包含N个指针的数组。每个指针从args[0]args[N - 1],都指向内存中实际参数将被复制的区域。

sharedMem 设置每个线程块可用的动态共享内存量。

stream 指定调用关联到的流。

Note:

cudaLaunchCooperativeKernel ( C API)

模板 < 类 T >

__host__cudaError_t cudaLaunchKernel ( T* func, dim3 gridDim, dim3 blockDim, void** args, size_t sharedMem = 0, cudaStream_t stream = 0 ) [inline]
启动一个设备函数。
参数
func
- Device function symbol
gridDim
- Grid dimentions
blockDim
- Block dimentions
args
- Arguments
sharedMem
- Shared memory (defaults to 0)
stream
- Stream identifier (defaults to NULL)
描述

该函数在gridDimgridDim.xgridDim.ygridDim.z)网格块上调用内核func。每个块包含blockDimblockDim.xblockDim.yblockDim.z)个线程。

如果内核有N个参数,args应指向包含N个指针的数组。每个指针从args[0]args[N - 1],都指向内存中实际参数将被复制的区域。

sharedMem 设置每个线程块可用的动态共享内存量。

stream 指定调用关联到的流。

Note:

cudaLaunchKernel ( C API)

模板 < 类型名... 动作类型 >

__host__cudaError_t cudaLaunchKernelEx ( const cudaLaunchConfig_t* config, const cudaKernel_t kernel, ActTypes &&... args ) [inline]
使用启动时配置启动一个CUDA函数。
参数
config
- Launch configuration
kernel
args
- Parameter pack of kernel parameters
描述

在由config->gridDim (config->gridDim.xconfig->gridDim.yconfig->gridDim.z)定义的网格块上调用内核kernel。每个块包含config->blockDim (config->blockDim.xconfig->blockDim.yconfig->blockDim.z)个线程。

config->dynamicSmemBytes 设置每个线程块可用的动态共享内存量。

config->stream 指定了调用关联的流。

除了网格和块维度、动态共享内存大小以及流之外,还可以通过config:的以下两个字段提供更多配置:

config->attrs 是一个包含 config->numAttrs 个连续 cudaLaunchAttribute 元素的数组。如果 config->numAttrs 为零,则不考虑该指针的值。但在这种情况下,建议将指针设置为 NULL。config->numAttrs 表示填充 config->attrs 数组前 config->numAttrs 个位置的属性数量。

内核参数应通过args参数包作为参数传递给此函数。

该函数的C API版本cudaLaunchKernelExC也可用于C++11之前的编译器,以及需要通过void*数组传递内核参数的使用场景。

Note:

另请参阅:

cudaLaunchKernelEx ( C API), cuLaunchKernelEx

模板 < 类型名... ExpTypes, 类型名... ActTypes >

__host__cudaError_t cudaLaunchKernelEx ( const cudaLaunchConfig_t* config, void(*)(ExpTypes...) kernel, ActTypes &&... args ) [inline]
使用启动时配置启动一个CUDA函数。
参数
config
- Launch configuration
kernel
- Kernel to launch
args
- Parameter pack of kernel parameters
描述

在由config->gridDim (config->gridDim.xconfig->gridDim.yconfig->gridDim.z)定义的网格块上调用内核kernel。每个块包含config->blockDim (config->blockDim.xconfig->blockDim.yconfig->blockDim.z)个线程。

config->dynamicSmemBytes 设置每个线程块可用的动态共享内存量。

config->stream 指定了调用关联的流。

除了网格和块维度、动态共享内存大小以及流之外,还可以通过config:的以下两个字段提供更多配置:

config->attrs 是一个包含 config->numAttrs 个连续 cudaLaunchAttribute 元素的数组。如果 config->numAttrs 为零,则不考虑该指针的值。但在这种情况下,建议将指针设置为 NULL。config->numAttrs 表示填充 config->attrs 数组前 config->numAttrs 个位置的属性数量。

内核参数应通过args参数包作为参数传递给此函数。

该函数的C API版本cudaLaunchKernelExC也可用于C++11之前的编译器,以及需要通过void*数组传递内核参数的使用场景。

Note:

另请参阅:

cudaLaunchKernelEx ( C API), cuLaunchKernelEx

模板 < 类 T >

__host__cudaError_t cudaLibraryGetGlobal ( T** dptr, size_t* bytes, cudaLibrary_t library, const char* name ) [inline]
返回一个全局设备指针。
参数
dptr
- Returned global device pointer for the requested library
bytes
- Returned global size in bytes
library
- Library to retrieve global from
name
- Name of global to retrieve
描述

*dptr*bytes中返回指定库library中名为name的全局变量的基指针和大小(针对当前设备)。如果请求的名称name不存在对应的全局变量,该调用将返回cudaErrorSymbolNotFound。参数dptrbytes(不能同时为NULL)可以设为NULL,此时该参数将被忽略。

另请参阅:

cudaLibraryLoadData, cudaLibraryLoadFromFile, cudaLibraryUnload, cudaLibraryGetManaged

模板 < 类 T >

__host__cudaError_t cudaLibraryGetManaged ( T** dptr, size_t* bytes, cudaLibrary_t library, const char* name ) [inline]
返回指向托管内存的指针。
参数
dptr
- Returned pointer to the managed memory
bytes
- Returned memory size in bytes
library
- Library to retrieve managed memory from
name
- Name of managed memory to retrieve
描述

*dptr*bytes中返回请求库library中名为name的托管内存的基指针和大小。如果不存在具有请求名称name的托管内存,则调用返回cudaErrorSymbolNotFound。参数dptrbytes(不能同时为NULL)可以为NULL,此时该参数将被忽略。请注意,库library的托管内存跨设备共享,并在加载库时注册。

另请参阅:

cudaLibraryLoadData, cudaLibraryLoadFromFile, cudaLibraryUnload, cudaLibraryGetGlobal

模板 < 类 T >

__host__cudaError_t cudaLibraryGetUnifiedFunction ( T** fptr, cudaLibrary_t library, const char* symbol ) [inline]
返回指向统一函数的指针。
参数
fptr
- Returned pointer to a unified function
library
- Library to retrieve function pointer memory from
symbol
- Name of function pointer to retrieve
描述

*fptr中返回由symbol表示的统一函数的函数指针。如果不存在名为symbol的统一函数,该调用将返回cudaErrorSymbolNotFound。如果系统中不存在具有cudaDeviceProp::unifiedFunctionPointers属性的设备,该调用可能会返回cudaErrorSymbolNotFound

另请参阅:

cudaLibraryLoadData, cudaLibraryLoadFromFile, cudaLibraryUnload

__host__cudaError_t cudaMallocAsync ( void** ptr, size_t size, cudaMemPool_t memPool, cudaStream_t stream )
从池中分配。
描述

这是通过函数重载提供的cudaMallocFromPoolAsync的另一种拼写形式。

另请参阅:

cudaMallocFromPoolAsync, cudaMallocAsync ( C API)

__host__cudaError_t cudaMallocHost ( void** ptr, size_t size, unsigned int  flags )
[C++ API] 在主机上分配页锁定内存
参数
ptr
- Device pointer to allocated memory
size
- Requested allocation size in bytes
flags
- Requested properties of allocated memory
描述

分配size字节的页锁定主机内存,该内存可被设备直接访问。驱动程序会跟踪通过此函数分配的虚拟内存范围,并自动加速对cudaMemcpy()等函数的调用。由于设备可以直接访问该内存,其读写带宽远高于通过malloc()等函数分配的可分页内存。但分配过多的固定内存可能会降低系统性能,因为这会减少系统可用于分页的内存量。因此,建议谨慎使用此函数,主要用于分配主机与设备间数据交换的暂存区域。

flags 参数用于指定影响分配的不同选项,如下所示。

  • cudaHostAllocDefault: 该标志位的值被定义为0。

  • cudaHostAllocPortable: 此调用返回的内存将被所有CUDA上下文视为固定内存,而不仅仅是执行分配的那个上下文。

  • cudaHostAllocMapped: 将分配的内存映射到CUDA地址空间。可以通过调用cudaHostGetDevicePointer()获取该内存的设备指针。

  • cudaHostAllocWriteCombined: 将内存分配为写合并(WC)模式。在某些系统配置下,WC内存能更快地通过PCI Express总线传输,但大多数CPU无法高效读取。对于CPU写入并通过映射固定内存或主机到设备传输由设备读取的缓冲区,WC内存是一个很好的选择。

所有这些标志彼此之间是正交的:开发者可以不受限制地分配具有可移植性、映射和/或写合并特性的内存。

cudaSetDeviceFlags() 必须已使用 cudaDeviceMapHost 标志调用,才能使 cudaHostAllocMapped 标志生效。

对于不支持映射固定内存的设备,可以在CUDA上下文中指定cudaHostAllocMapped标志。由于内存可能通过cudaHostAllocPortable标志映射到其他CUDA上下文中,因此失败会延迟到cudaHostGetDevicePointer()

通过此函数分配的内存必须使用cudaFreeHost()释放。

Note:

另请参阅:

cudaSetDeviceFlags, cudaMallocHost ( C API), cudaFreeHost, cudaHostAlloc

模板 < 类 T >

__host__cudaError_t cudaMallocManaged ( T** devPtr, size_t size, unsigned int  flags = cudaMemAttachGlobal ) [inline]
分配将由统一内存系统自动管理的内存。
参数
devPtr
- Pointer to allocated device memory
size
- Requested allocation size in bytes
flags
- Must be either cudaMemAttachGlobal or cudaMemAttachHost (defaults to cudaMemAttachGlobal)
描述

在设备上分配size字节的托管内存,并通过*devPtr返回指向已分配内存的指针。如果设备不支持分配托管内存,则返回cudaErrorNotSupported。可以使用设备属性cudaDevAttrManagedMemory查询对托管内存的支持情况。分配的内存会进行适当对齐,适合任何类型的变量。内存不会被清零。如果size为0,cudaMallocManaged将返回cudaErrorInvalidValue。该指针在CPU和系统中所有支持托管内存的GPU上均有效。对此指针的所有访问都必须遵循统一内存编程模型。

flags 指定此内存分配的默认流关联。flags 必须是 cudaMemAttachGlobalcudaMemAttachHost 之一。flags 的默认值为 cudaMemAttachGlobal。如果指定了 cudaMemAttachGlobal,则此内存可从任何设备上的任何流访问。如果指定了 cudaMemAttachHost,则不应从设备属性 cudaDevAttrConcurrentManagedAccess 值为零的设备访问该分配;需要显式调用 cudaStreamAttachMemAsync 才能在此类设备上启用访问。

如果之后通过cudaStreamAttachMemAsync将关联更改为单个流,则在销毁该流时,会恢复cudaMallocManaged期间指定的默认关联。对于__managed__变量,默认关联始终是cudaMemAttachGlobal。请注意,销毁流是一个异步操作,因此,在流中的所有工作完成之前,不会发生默认关联的更改。

使用cudaMallocManaged分配的内存应当通过cudaFree释放。

对于设备属性cudaDevAttrConcurrentManagedAccess值为非零的GPU,可以实现设备内存超额订阅。在这类GPU上,统一内存驱动程序可能随时将托管内存从设备内存驱逐到主机内存,以便为其他分配腾出空间。

在多GPU系统中,当所有GPU的设备属性cudaDevAttrConcurrentManagedAccess都具有非零值时,托管内存在此API返回时可能尚未填充,而是在访问时才会填充。在此类系统中,托管内存可以随时迁移到任何处理器的内存中。统一内存驱动程序将采用启发式方法来尽可能保持数据局部性并防止过多的页面错误。应用程序还可以通过cudaMemAdvise指导驱动程序关于内存使用模式。应用程序也可以通过cudaMemPrefetchAsync显式地将内存迁移到目标处理器的内存中。

在一个多GPU系统中,如果所有GPU的设备属性cudaDevAttrConcurrentManagedAccess值均为零,且所有GPU之间都支持点对点访问,那么托管内存的物理存储将在调用cudaMallocManaged时所在的活跃GPU上创建。所有其他GPU将通过PCIe总线上的点对点映射以较低带宽访问该数据。统一内存驱动程序不会在这些GPU之间迁移内存。

在多GPU系统中,如果并非所有GPU都支持彼此之间的点对点通信,并且其中至少一个GPU的设备属性cudaDevAttrConcurrentManagedAccess值为零时,托管内存物理存储位置的选择将取决于系统。

  • On Linux, the location chosen will be device memory as long as the current set of active contexts are on devices that either have peer-to-peer support with each other or have a non-zero value for the device attribute cudaDevAttrConcurrentManagedAccess. If there is an active context on a GPU that does not have a non-zero value for that device attribute and it does not have peer-to-peer support with the other devices that have active contexts on them, then the location for physical storage will be 'zero-copy' or host memory. Note that this means that managed memory that is located in device memory is migrated to host memory if a new context is created on a GPU that doesn't have a non-zero value for the device attribute and does not support peer-to-peer with at least one of the other devices that has an active context. This in turn implies that context creation may fail if there is insufficient host memory to migrate all managed allocations.

  • 在Windows系统上,物理存储始终创建在'零拷贝'或主机内存中。所有GPU将通过PCIe总线以降低的带宽引用数据。在这种情况下,建议使用环境变量CUDA_VISIBLE_DEVICES来限制CUDA仅使用那些支持点对点传输的GPU。或者,用户也可以将CUDA_MANAGED_FORCE_DEVICE_ALLOC设置为非零值,强制驱动程序始终使用设备内存作为物理存储。当此环境变量设置为非零值时,该进程中使用且支持托管内存的所有设备必须彼此具备点对点兼容性。如果使用了支持托管内存的设备,但该设备与该进程先前使用的其他支持托管内存的设备不具备点对点兼容性,即使已对这些设备调用cudaDeviceReset,也将返回错误cudaErrorInvalidDevice。这些环境变量在CUDA编程指南的"CUDA环境变量"章节中有详细说明。

  • 在ARM架构上,Drive PX-2的独立GPU不支持托管内存。

Note:

另请参阅:

cudaMallocPitch, cudaFree, cudaMallocArray, cudaFreeArray, cudaMalloc3D, cudaMalloc3DArray, cudaMallocHost ( C API), cudaFreeHost, cudaHostAlloc, cudaDeviceGetAttribute, cudaStreamAttachMemAsync

模板 < 类 T >

cudaError_t cudaMemAdvise ( T* devPtr, size_t count, cudaMemoryAdvise advice, cudaMemLocation location ) [inline]
提供关于给定内存范围使用情况的建议。
描述

这是通过函数重载提供的cudaMemAdvise的另一种拼写形式。

另请参阅:

cudaMemAdvise, cudaMemAdvise (C API)

模板 < 类型 T, 类型 U >

__host__cudaError_t cudaMemcpyBatchAsync ( T** dsts, U** srcs, size_t* sizes, size_t count, cudaMemcpyAttributes attr, size_t* failIdx, cudaStream_t hStream ) [inline]
异步执行一批内存拷贝操作。
描述

这是通过函数重载提供的cudaMemcpyBatchAsync的另一种拼写形式。

attr指定的cudaMemcpyAttributes适用于批处理中指定的所有复制操作。

另请参阅:

cudaMemcpyBatchAsync

模板 < 类型 T, 类型 U >

__host__cudaError_t cudaMemcpyBatchAsync ( T** dsts, U** srcs, size_t* sizes, size_t count, cudaMemcpyAttributes* attrs, size_t* attrsIdxs, size_t numAttrs, size_t* failIdx, cudaStream_t hStream ) [inline]
异步执行一批内存拷贝操作。
描述

这是通过函数重载提供的cudaMemcpyBatchAsync的另一种拼写形式。

另请参阅:

cudaMemcpyBatchAsync

模板 < 类 T >

__host__cudaError_t cudaMemcpyFromSymbol ( void* dst, const T& symbol, size_t count, size_t offset = 0, cudaMemcpyKind kind = cudaMemcpyDeviceToHost ) [inline]
[C++ API] 从设备上的给定符号复制数据
参数
dst
- Destination memory address
symbol
- Device symbol reference
count
- Size in bytes to copy
offset
- Offset from start of symbol in bytes
kind
- Type of transfer
描述

从符号symbol起始位置偏移offset字节的内存区域复制count个字节到dst指向的内存区域。内存区域不得重叠。symbol是位于全局或常量内存空间的变量。kind可以是cudaMemcpyDeviceToHostcudaMemcpyDeviceToDevice

Note:
  • 请注意,此函数也可能返回之前异步启动的错误代码。

  • 该函数在大多数使用场景下表现出同步行为。

  • 在CUDA 4.1中已弃用将变量名称字符串作为symbol参数的做法,并在CUDA 5.0中移除了该功能。

  • 请注意,如果此调用尝试初始化CUDA RT内部状态,该函数也可能返回cudaErrorInitializationErrorcudaErrorInsufficientDrivercudaErrorNoDevice

  • 请注意,根据cudaStreamAddCallback的规定,回调函数中不得调用任何CUDA函数。在这种情况下,可能会(但不保证)返回cudaErrorNotPermitted作为诊断信息。

另请参阅:

cudaMemcpy, cudaMemcpy2D, cudaMemcpy2DToArray, cudaMemcpy2DFromArray, cudaMemcpy2DArrayToArray, cudaMemcpyToSymbol, cudaMemcpyAsync, cudaMemcpy2DAsync, cudaMemcpy2DToArrayAsync, cudaMemcpy2DFromArrayAsync, cudaMemcpyToSymbolAsync, cudaMemcpyFromSymbolAsync

模板 < 类 T >

__host__cudaError_t cudaMemcpyFromSymbolAsync ( void* dst, const T& symbol, size_t count, size_t offset = 0, cudaMemcpyKind kind = cudaMemcpyDeviceToHost, cudaStream_t stream = 0 ) [inline]
[C++ API] 从设备上的给定符号复制数据
参数
dst
- Destination memory address
symbol
- Device symbol reference
count
- Size in bytes to copy
offset
- Offset from start of symbol in bytes
kind
- Type of transfer
stream
- Stream identifier
描述

从符号symbol起始位置偏移offset字节的内存区域复制count个字节到dst指向的内存区域。内存区域不得重叠。symbol是位于全局或常量内存空间的变量。kind可以是cudaMemcpyDeviceToHostcudaMemcpyDeviceToDevice

cudaMemcpyFromSymbolAsync() 相对于主机是异步的,因此调用可能在复制完成前就返回。可以通过传递非零的stream参数选择性地将复制操作关联到某个流。如果kindcudaMemcpyDeviceToHoststream非零,则复制操作可能与其他流中的操作重叠。

Note:
  • 请注意,此函数也可能返回之前异步启动的错误代码。

  • 该函数在大多数使用场景下表现出异步行为。

  • 在CUDA 4.1中已弃用将变量名称字符串作为symbol参数的做法,并在CUDA 5.0中移除了该功能。

  • 请注意,如果此调用尝试初始化CUDA RT内部状态,该函数也可能返回cudaErrorInitializationErrorcudaErrorInsufficientDrivercudaErrorNoDevice

  • 请注意,根据cudaStreamAddCallback的规定,回调函数中不得调用任何CUDA函数。在这种情况下,可能会(但不保证)返回cudaErrorNotPermitted作为诊断信息。

另请参阅:

cudaMemcpy, cudaMemcpy2D, cudaMemcpy2DToArray, cudaMemcpy2DFromArray, cudaMemcpy2DArrayToArray, cudaMemcpyToSymbol, cudaMemcpyFromSymbol, cudaMemcpyAsync, cudaMemcpy2DAsync, cudaMemcpy2DToArrayAsync, cudaMemcpy2DFromArrayAsync, cudaMemcpyToSymbolAsync

模板 < 类 T >

__host__cudaError_t cudaMemcpyToSymbol ( const T& symbol, const void* src, size_t count, size_t offset = 0, cudaMemcpyKind kind = cudaMemcpyHostToDevice ) [inline]
[C++ API] 将数据复制到设备上的指定符号
参数
symbol
- Device symbol reference
src
- Source memory address
count
- Size in bytes to copy
offset
- Offset from start of symbol in bytes
kind
- Type of transfer
描述

count字节从src指向的内存区域复制到符号symbol起始位置偏移offset字节的内存区域。内存区域不能重叠。symbol是位于全局或常量内存空间中的变量。kind可以是cudaMemcpyHostToDevicecudaMemcpyDeviceToDevice

Note:
  • 请注意,此函数也可能返回之前异步启动的错误代码。

  • 该函数在大多数使用场景下表现出同步行为。

  • 在CUDA 4.1中已弃用将变量名称字符串作为symbol参数的做法,并在CUDA 5.0中移除了该功能。

  • 请注意,如果此调用尝试初始化CUDA RT内部状态,该函数也可能返回cudaErrorInitializationErrorcudaErrorInsufficientDrivercudaErrorNoDevice

  • 请注意,根据cudaStreamAddCallback的规定,回调函数中不得调用任何CUDA函数。在这种情况下,可能会(但不保证)返回cudaErrorNotPermitted作为诊断信息。

另请参阅:

cudaMemcpy, cudaMemcpy2D, cudaMemcpy2DToArray, cudaMemcpy2DFromArray, cudaMemcpy2DArrayToArray, cudaMemcpyFromSymbol, cudaMemcpyAsync, cudaMemcpy2DAsync, cudaMemcpy2DToArrayAsync, cudaMemcpy2DFromArrayAsync, cudaMemcpyToSymbolAsync, cudaMemcpyFromSymbolAsync

模板 < 类 T >

__host__cudaError_t cudaMemcpyToSymbolAsync ( const T& symbol, const void* src, size_t count, size_t offset = 0, cudaMemcpyKind kind = cudaMemcpyHostToDevice, cudaStream_t stream = 0 ) [inline]
[C++ API] 将数据复制到设备上的指定符号
参数
symbol
- Device symbol reference
src
- Source memory address
count
- Size in bytes to copy
offset
- Offset from start of symbol in bytes
kind
- Type of transfer
stream
- Stream identifier
描述

src指向的内存区域复制count字节到符号symbol起始位置偏移offset字节的内存区域。内存区域不能重叠。symbol是位于全局或常量内存空间的变量。kind可以是cudaMemcpyHostToDevicecudaMemcpyDeviceToDevice

cudaMemcpyToSymbolAsync() 相对于主机是异步的,因此调用可能在复制完成前就返回。可以通过传递非零的stream参数选择性地将复制操作关联到某个流。如果kindcudaMemcpyHostToDevicestream为非零,该复制操作可能与其他流中的操作重叠。

Note:
  • 请注意,此函数也可能返回之前异步启动的错误代码。

  • 该函数在大多数使用场景下表现出异步行为。

  • 在CUDA 4.1中已弃用将变量名称字符串作为symbol参数的做法,并在CUDA 5.0中移除了该功能。

  • 请注意,如果此调用尝试初始化CUDA RT内部状态,该函数也可能返回cudaErrorInitializationErrorcudaErrorInsufficientDrivercudaErrorNoDevice

  • 请注意,根据cudaStreamAddCallback的规定,回调函数中不得调用任何CUDA函数。在这种情况下,可能会(但不保证)返回cudaErrorNotPermitted作为诊断信息。

另请参阅:

cudaMemcpy, cudaMemcpy2D, cudaMemcpy2DToArray, cudaMemcpy2DFromArray, cudaMemcpy2DArrayToArray, cudaMemcpyToSymbol, cudaMemcpyFromSymbol, cudaMemcpyAsync, cudaMemcpy2DAsync, cudaMemcpy2DToArrayAsync, cudaMemcpy2DFromArrayAsync, cudaMemcpyFromSymbolAsync

模板 < 类 T >

__host__cudaError_t cudaOccupancyAvailableDynamicSMemPerBlock ( size_t* dynamicSmemSize, T* func, int  numBlocks, int  blockSize ) [inline]
Returns dynamic shared memory available per block when launching numBlocks blocks on SM.
参数
dynamicSmemSize
- Returned maximum dynamic shared memory
func
- Kernel function for which occupancy is calculated
numBlocks
- Number of blocks to fit on SM
blockSize
- Size of the block
描述

*dynamicSmemSize中返回允许每个SM运行numBlocks个块所需的动态共享内存最大大小。

Note:

另请参阅:

cudaOccupancyMaxPotentialBlockSize

cudaOccupancyMaxPotentialBlockSizeWithFlags

cudaOccupancyMaxActiveBlocksPerMultiprocessor

cudaOccupancyMaxActiveBlocksPerMultiprocessorWithFlags

cudaOccupancyMaxPotentialBlockSizeVariableSMem

cudaOccupancyMaxPotentialBlockSizeVariableSMemWithFlags

模板 < 类 T >

__host__cudaError_t cudaOccupancyMaxActiveBlocksPerMultiprocessor ( int* numBlocks, T func, int  blockSize, size_t dynamicSMemSize ) [inline]
返回设备函数的占用情况。
参数
numBlocks
- Returned occupancy
func
- Kernel function for which occupancy is calulated
blockSize
- Block size the kernel is intended to be launched with
dynamicSMemSize
- Per-block dynamic shared memory usage intended, in bytes
描述

返回设备函数每个流式多处理器在*numBlocks中的最大活动块数。

Note:

另请参阅:

cudaOccupancyMaxActiveBlocksPerMultiprocessorWithFlags

cudaOccupancyMaxPotentialBlockSize

cudaOccupancyMaxPotentialBlockSizeWithFlags

cudaOccupancyMaxPotentialBlockSizeVariableSMem

cudaOccupancyMaxPotentialBlockSizeVariableSMemWithFlags

cudaOccupancyAvailableDynamicSMemPerBlock

模板 < 类 T >

__host__cudaError_t cudaOccupancyMaxActiveBlocksPerMultiprocessorWithFlags ( int* numBlocks, T func, int  blockSize, size_t dynamicSMemSize, unsigned int  flags ) [inline]
返回具有指定标志的设备函数的占用情况。
参数
numBlocks
- Returned occupancy
func
- Kernel function for which occupancy is calulated
blockSize
- Block size the kernel is intended to be launched with
dynamicSMemSize
- Per-block dynamic shared memory usage intended, in bytes
flags
- Requested behavior for the occupancy calculator
描述

返回设备函数每个流式多处理器在*numBlocks中的最大活动块数。

flags 参数用于控制特殊情况的处理方式。有效标志包括:

  • cudaOccupancyDisableCachingOverride: 在全局缓存影响占用率的平台上抑制默认行为。在此类平台上,如果启用了缓存,但每个块的SM资源使用会导致零占用率,占用率计算器将按照缓存被禁用的情况计算占用率。设置此标志将使占用率计算器在此类情况下返回0。有关此功能的更多信息,请参阅Maxwell调优指南中的"统一L1/纹理缓存"部分。

Note:

另请参阅:

cudaOccupancyMaxActiveBlocksPerMultiprocessor

cudaOccupancyMaxPotentialBlockSize

cudaOccupancyMaxPotentialBlockSizeWithFlags

cudaOccupancyMaxPotentialBlockSizeVariableSMem

cudaOccupancyMaxPotentialBlockSizeVariableSMemWithFlags

cudaOccupancyAvailableDynamicSMemPerBlock

模板 < 类 T >

__host__cudaError_t cudaOccupancyMaxActiveClusters ( int* numClusters, T* func, const cudaLaunchConfig_t* config ) [inline]
Given the kernel function (func) and launch configuration (config), return the maximum number of clusters that could co-exist on the target device in *numClusters.
参数
numClusters
- Returned maximum number of clusters that could co-exist on the target device
func
- Kernel function for which maximum number of clusters are calculated
config
- Launch configuration for the given kernel function
描述

如果函数已设置所需的集群大小(参见cudaFuncGetAttributes),则配置中的集群大小必须未指定或与所需大小匹配。若未设置所需大小,则必须在配置中指定集群大小,否则函数将返回错误。

请注意,内核函数的各种属性可能会影响占用率计算。运行时环境可能会影响硬件如何调度集群,因此计算出的占用率并不保证能够实现。

Note:

另请参阅:

cudaFuncGetAttributes

模板 < 类 T >

__host__cudaError_t cudaOccupancyMaxPotentialBlockSize ( int* minGridSize, int* blockSize, T func, size_t dynamicSMemSize = 0, int  blockSizeLimit = 0 ) [inline]
返回能实现设备函数最大潜在占用率的网格和块大小。
参数
minGridSize
- Returned minimum grid size needed to achieve the best potential occupancy
blockSize
- Returned block size
func
- Device function symbol
dynamicSMemSize
- Per-block dynamic shared memory usage intended, in bytes
blockSizeLimit
- The maximum block size func is designed to work with. 0 means no limit.
描述

*minGridSize*blocksize中返回一个推荐的网格/块大小组合,该组合能够实现最佳潜在占用率(即用最少的块数达到最大活跃warp数量)。

使用

另请参阅:

cudaOccupancyMaxPotentialBlockSizeVariableSMem 如果每个块的动态共享内存量随不同块大小而变化。

Note:

另请参阅:

cudaOccupancyMaxPotentialBlockSizeWithFlags

cudaOccupancyMaxActiveBlocksPerMultiprocessor

cudaOccupancyMaxActiveBlocksPerMultiprocessorWithFlags

cudaOccupancyMaxPotentialBlockSizeVariableSMem

cudaOccupancyMaxPotentialBlockSizeVariableSMemWithFlags

cudaOccupancyAvailableDynamicSMemPerBlock

模板 < 类型名 UnaryFunction, 类 T >

__host__cudaError_t cudaOccupancyMaxPotentialBlockSizeVariableSMem ( int* minGridSize, int* blockSize, T func, UnaryFunction blockSizeToDynamicSMemSize, int  blockSizeLimit = 0 ) [inline]
返回能实现设备函数最大潜在占用率的网格(grid)和块(block)大小。
参数
minGridSize
- Returned minimum grid size needed to achieve the best potential occupancy
blockSize
- Returned block size
func
- Device function symbol
blockSizeToDynamicSMemSize
- A unary function / functor that takes block size, and returns the size, in bytes, of dynamic shared memory needed for a block
blockSizeLimit
- The maximum block size func is designed to work with. 0 means no limit.
描述

*minGridSize*blocksize中返回一个推荐的网格/块大小组合,该组合能够实现最佳潜在占用率(即用最少的块数达到最大数量的活动warp)。

Note:

另请参阅:

cudaOccupancyMaxPotentialBlockSizeVariableSMemWithFlags

cudaOccupancyMaxActiveBlocksPerMultiprocessor

cudaOccupancyMaxActiveBlocksPerMultiprocessorWithFlags

cudaOccupancyMaxPotentialBlockSize

cudaOccupancyMaxPotentialBlockSizeWithFlags

cudaOccupancyAvailableDynamicSMemPerBlock

模板 < 类型名 UnaryFunction, 类 T >

__host__cudaError_t cudaOccupancyMaxPotentialBlockSizeVariableSMemWithFlags ( int* minGridSize, int* blockSize, T func, UnaryFunction blockSizeToDynamicSMemSize, int  blockSizeLimit = 0, unsigned int  flags = 0 ) [inline]
返回能实现设备函数最大潜在占用率的网格(grid)和块(block)大小。
参数
minGridSize
- Returned minimum grid size needed to achieve the best potential occupancy
blockSize
- Returned block size
func
- Device function symbol
blockSizeToDynamicSMemSize
- A unary function / functor that takes block size, and returns the size, in bytes, of dynamic shared memory needed for a block
blockSizeLimit
- The maximum block size func is designed to work with. 0 means no limit.
flags
- Requested behavior for the occupancy calculator
描述

*minGridSize*blocksize中返回一个推荐的网格/块大小组合,该组合能够实现最佳潜在占用率(即用最少的块数获得最大数量的活动warp)。

flags 参数用于控制特殊情况的处理方式。有效标志包括:

  • cudaOccupancyDisableCachingOverride: 该标志用于在全局缓存影响占用率的平台上抑制默认行为。在此类平台上,如果启用了缓存,但每个块的SM资源使用会导致零占用率,占用率计算器将按照禁用缓存的情况计算占用率。设置此标志将使占用率计算器在此类情况下返回0。有关此功能的更多信息,请参阅Maxwell调优指南中的"统一L1/纹理缓存"部分。

Note:

另请参阅:

cudaOccupancyMaxPotentialBlockSizeVariableSMem

cudaOccupancyMaxActiveBlocksPerMultiprocessor

cudaOccupancyMaxActiveBlocksPerMultiprocessorWithFlags

cudaOccupancyMaxPotentialBlockSize

cudaOccupancyMaxPotentialBlockSizeWithFlags

cudaOccupancyAvailableDynamicSMemPerBlock

模板 < 类 T >

__host__cudaError_t cudaOccupancyMaxPotentialBlockSizeWithFlags ( int* minGridSize, int* blockSize, T func, size_t dynamicSMemSize = 0, int  blockSizeLimit = 0, unsigned int  flags = 0 ) [inline]
返回针对具有指定标志的设备函数实现最大潜在占用率的网格和块大小。
参数
minGridSize
- Returned minimum grid size needed to achieve the best potential occupancy
blockSize
- Returned block size
func
- Device function symbol
dynamicSMemSize
- Per-block dynamic shared memory usage intended, in bytes
blockSizeLimit
- The maximum block size func is designed to work with. 0 means no limit.
flags
- Requested behavior for the occupancy calculator
描述

*minGridSize*blocksize中返回一个推荐的网格/块大小组合,该组合能够实现最佳潜在占用率(即用最少的块数获得最大数量的活动warp)。

flags 参数用于控制特殊情况的处理方式。有效标志包括:

  • cudaOccupancyDisableCachingOverride: 该标志用于在全局缓存影响占用率的平台上抑制默认行为。在此类平台上,如果启用了缓存,但每个块的SM资源使用会导致零占用率时,占用率计算器将按照禁用缓存的情况计算占用率。设置此标志将使占用率计算器在此类情况下返回0。有关此功能的更多信息,请参阅Maxwell调优指南中的"统一L1/纹理缓存"部分。

使用

另请参阅:

cudaOccupancyMaxPotentialBlockSizeVariableSMem 如果每个块的动态共享内存量随不同块大小而变化。

Note:

另请参阅:

cudaOccupancyMaxPotentialBlockSize

cudaOccupancyMaxActiveBlocksPerMultiprocessor

cudaOccupancyMaxActiveBlocksPerMultiprocessorWithFlags

cudaOccupancyMaxPotentialBlockSizeVariableSMem

cudaOccupancyMaxPotentialBlockSizeVariableSMemWithFlags

cudaOccupancyAvailableDynamicSMemPerBlock

模板 < 类 T >

__host__cudaError_t cudaOccupancyMaxPotentialClusterSize ( int* clusterSize, T* func, const cudaLaunchConfig_t* config ) [inline]
Given the kernel function (func) and launch configuration (config), return the maximum cluster size in *clusterSize.
参数
clusterSize
- Returned maximum cluster size that can be launched for the given kernel function and launch configuration
func
- Kernel function for which maximum cluster size is calculated
config
- Launch configuration for the given kernel function
描述

config中的集群维度参数将被忽略。如果func设置了必需的集群大小(参见cudaFuncGetAttributes),*clusterSize将反映所需的集群大小。

默认情况下,此函数始终会返回一个可在未来硬件上移植的值。如果内核函数允许非可移植的集群大小,则可能返回更高的值。

该函数将遵循编译时的启动边界限制。

Note:

另请参阅:

cudaFuncGetAttributes

模板 < 类 T >

__host__cudaError_t cudaStreamAttachMemAsync ( cudaStream_t stream, T* devPtr, size_t length = 0, unsigned int  flags = cudaMemAttachSingle ) [inline]
异步地将内存附加到流。
参数
stream
- Stream in which to enqueue the attach operation
devPtr
- Pointer to memory (must be a pointer to managed memory or to a valid host-accessible region of system-allocated memory)
length
- Length of memory (defaults to zero)
flags
- Must be one of cudaMemAttachGlobal, cudaMemAttachHost or cudaMemAttachSingle (defaults to cudaMemAttachSingle)
描述

stream中排队一个操作,用于指定从devPtr开始、长度为length字节的内存与流的关联。此函数是一个流顺序操作,意味着它依赖于流中先前的工作,并且只有在流中先前的工作完成后才会生效。任何先前的关联都会被自动替换。

devPtr 必须指向以下类型的内存之一:

  • 使用__managed__关键字声明的托管内存或通过cudaMallocManaged分配的托管内存。

  • 一个有效的、主机可访问的系统分配可分页内存区域。只有当与流关联的设备报告设备属性cudaDevAttrPageableMemoryAccess为非零值时,才能指定此类内存。

对于托管分配,length必须为零或整个分配的大小。两者都表示正在更改整个分配的流关联。目前,无法更改托管分配部分区域的流关联。

对于可分页的内存分配,length必须是非零值。

流关联通过flags参数指定,该参数必须是cudaMemAttachGlobalcudaMemAttachHostcudaMemAttachSingle之一。flags的默认值为cudaMemAttachSingle。如果指定了cudaMemAttachGlobal标志,则任何设备上的任何流都可以访问该内存。如果指定了cudaMemAttachHost标志,程序保证不会从设备属性cudaDevAttrConcurrentManagedAccess值为零的设备上的任何流访问该设备上的内存。如果指定了cudaMemAttachSingle标志且stream关联的设备其设备属性cudaDevAttrConcurrentManagedAccess值为零,程序保证仅会通过stream访问该设备上的内存。将内存单独附加到NULL流是非法的,因为NULL流是虚拟全局流而非特定流。这种情况下将返回错误。

当内存与单个流关联时,只要stream中的所有操作已完成,无论其他流是否处于活动状态,统一内存系统都将允许CPU访问此内存区域。实际上,这将活动GPU对托管内存区域的独占所有权限制为按流活动,而非整个GPU活动。

从未关联的流访问设备内存将产生未定义的结果。统一内存系统不会执行错误检查,以确保在其他流中启动的内核不会访问此区域。

程序有责任通过事件、同步或其他方式对cudaStreamAttachMemAsync的调用进行排序,以确保始终合法访问内存。对于所有跟随流关联更改的内核,数据可见性和一致性将相应更改。

如果在数据与stream关联期间该流被销毁,关联关系将被解除,分配将恢复为cudaMallocManaged指定的默认可见性。对于__managed__变量,默认关联始终是cudaMemAttachGlobal。请注意,销毁流是一个异步操作,因此,在流中的所有工作完成之前,不会发生默认关联的更改。

Note:

另请参阅:

cudaStreamCreate, cudaStreamCreateWithFlags, cudaStreamWaitEvent, cudaStreamSynchronize, cudaStreamAddCallback, cudaStreamDestroy, cudaMallocManaged