6.16. Execution Control
This section describes the execution control functions of the low-level CUDA driver application programming interface.
Enumerations
Functions
- CUresult cuFuncGetAttribute(int *pi, CUfunction_attribute attrib, CUfunction hfunc)
Returns information about a function.
- CUresult cuFuncGetDeviceCodeInfo(CUfunction func, CUdeviceptr *address, size_t *bytesize)
- CUresult cuFuncGetModule(CUmodule *hmod, CUfunction hfunc)
Returns a module handle.
- CUresult cuFuncGetName(const char **name, CUfunction hfunc)
Returns the function name for a CUfunction handle.
- CUresult cuFuncGetParamCount(CUfunction func, size_t *paramCount)
Returns the number of parameters used by the function.
- CUresult cuFuncGetParamInfo(CUfunction func, size_t paramIndex, size_t *paramOffset, size_t *paramSize)
Returns the offset and size of a kernel parameter in the device-side parameter layout.
- CUresult cuFuncIsLoaded(CUfunctionLoadingState *state, CUfunction function)
Returns if the function is loaded.
- CUresult cuFuncLoad(CUfunction function)
Loads a function.
- CUresult cuFuncSetAttribute(CUfunction hfunc, CUfunction_attribute attrib, int value)
Sets information about a function.
- CUresult cuFuncSetCacheConfig(CUfunction hfunc, CUfunc_cache config)
Sets the preferred cache configuration for a device function.
- CUresult cuLaunchCooperativeKernel(CUfunction f, unsigned int gridDimX, unsigned int gridDimY, unsigned int gridDimZ, unsigned int blockDimX, unsigned int blockDimY, unsigned int blockDimZ, unsigned int sharedMemBytes, CUstream hStream, void **kernelParams)
Launches a CUDA function CUfunction or a CUDA kernel CUkernel where thread blocks can cooperate and synchronize as they execute.
- CUresult cuLaunchHostFunc(CUstream hStream, CUhostFn fn, void *userData)
Enqueues a host function call in a stream.
- CUresult cuLaunchHostFunc_v2(CUstream hStream, CUhostFn fn, void *userData, unsigned int syncMode)
Enqueues a host function call in a stream.
- CUresult cuLaunchKernel(CUfunction f, unsigned int gridDimX, unsigned int gridDimY, unsigned int gridDimZ, unsigned int blockDimX, unsigned int blockDimY, unsigned int blockDimZ, unsigned int sharedMemBytes, CUstream hStream, void **kernelParams, void **extra)
Launches a CUDA function CUfunction or a CUDA kernel CUkernel .
- CUresult cuLaunchKernelEx(const CUlaunchConfig *config, CUfunction f, void **kernelParams, void **extra)
Launches a CUDA function CUfunction or a CUDA kernel CUkernel with launch-time configuration.
- CUresult cuMultiKernelCooperativeDomainCreate(CUcontext ctx, CUmultiKernelCooperativeDomain *domain_out, CUmultiKernelCooperativeDomain domain, cuuint64_t tag, void *commObj, CUhostCommunicationsVtable *commVtable, unsigned int localCount, void *localId)
Creates a new multi-kernel cooperative domain.
- CUresult cuMultiKernelCooperativeDomainDestroy(CUcontext ctx, CUmultiKernelCooperativeDomain domain, void *commObj, CUhostCommunicationsVtable *commVtable, unsigned int localCount, void *localId)
Destroys a multi-kernel cooperative domain.
6.16.1. Enumerations
6.16.2. Functions
-
CUresult cuFuncGetAttribute(int *pi, CUfunction_attribute attrib, CUfunction hfunc)
Returns information about a function.
Returns in
*pithe integer value of the attributeattribon the kernel given byhfunc. The supported attributes are:CU_FUNC_ATTRIBUTE_MAX_THREADS_PER_BLOCK: The maximum number of threads per block, beyond which a launch of the function would fail. This number depends on both the function and the device on which the function is currently loaded.
CU_FUNC_ATTRIBUTE_SHARED_SIZE_BYTES: The size in bytes of statically-allocated shared memory per block required by this function. This does not include dynamically-allocated shared memory requested by the user at runtime.
CU_FUNC_ATTRIBUTE_CONST_SIZE_BYTES: The size in bytes of user-allocated constant memory required by this function.
CU_FUNC_ATTRIBUTE_LOCAL_SIZE_BYTES: The size in bytes of local memory used by each thread of this function.
CU_FUNC_ATTRIBUTE_NUM_REGS: The number of registers used by each thread of this function.
CU_FUNC_ATTRIBUTE_PTX_VERSION: The PTX virtual architecture version for which the function was compiled. This value is the major PTX version * 10
the minor PTX version, so a PTX version 1.3 function would return the value 13. Note that this may return the undefined value of 0 for cubins compiled prior to CUDA 3.0.
CU_FUNC_ATTRIBUTE_BINARY_VERSION: The binary architecture version for which the function was compiled. This value is the major binary version * 10 + the minor binary version, so a binary version 1.3 function would return the value 13. Note that this will return a value of 10 for legacy cubins that do not have a properly-encoded binary architecture version.
::CU_FUNC_CACHE_MODE_CA: The attribute to indicate whether the function has been compiled with user specified option “-Xptxas –dlcm=ca” set .
CU_FUNC_ATTRIBUTE_MAX_DYNAMIC_SHARED_SIZE_BYTES: The maximum size in bytes of dynamically-allocated shared memory.
CU_FUNC_ATTRIBUTE_PREFERRED_SHARED_MEMORY_CARVEOUT: Preferred shared memory-L1 cache split ratio in percent of total shared memory.
CU_FUNC_ATTRIBUTE_CLUSTER_SIZE_MUST_BE_SET: If this attribute is set, the kernel must launch with a valid cluster size specified.
CU_FUNC_ATTRIBUTE_REQUIRED_CLUSTER_WIDTH: The required cluster width in blocks.
CU_FUNC_ATTRIBUTE_REQUIRED_CLUSTER_HEIGHT: The required cluster height in blocks.
CU_FUNC_ATTRIBUTE_REQUIRED_CLUSTER_DEPTH: The required cluster depth in blocks.
CU_FUNC_ATTRIBUTE_NON_PORTABLE_CLUSTER_SIZE_ALLOWED: Indicates whether the function can be launched with non-portable cluster size. 1 is allowed, 0 is disallowed. A non-portable cluster size may only function on the specific SKUs the program is tested on. The launch might fail if the program is run on a different hardware platform. CUDA API provides cudaOccupancyMaxActiveClusters to assist with checking whether the desired size can be launched on the current device. A portable cluster size is guaranteed to be functional on all compute capabilities higher than the target compute capability. The portable cluster size for sm_90 is 8 blocks per cluster. This value may increase for future compute capabilities. The specific hardware unit may support higher cluster sizes that’s not guaranteed to be portable.
CU_FUNC_ATTRIBUTE_CLUSTER_SCHEDULING_POLICY_PREFERENCE: The block scheduling policy of a function. The value type is CUclusterSchedulingPolicy.
CU_FUNC_ATTRIBUTE_SHARED_MEMORY_MODE: The shared memory mode of a function. The value type is CUsharedMemoryMode / cudaSharedMemoryMode.
With a few execeptions, function attributes may also be queried on unloaded function handles returned from cuModuleEnumerateFunctions. CUDA_ERROR_FUNCTION_NOT_LOADED is returned if the attribute requires a fully loaded function but the function is not loaded. The loading state of a function may be queried using ::cuFuncIsloaded. cuFuncLoad may be called to explicitly load a function before querying the following attributes that require the function to be loaded:
See also
cuCtxGetCacheConfig, cuCtxSetCacheConfig, cuFuncSetCacheConfig, cuLaunchKernel, ::cudaFuncGetAttributes, ::cudaFuncSetAttribute, cuFuncIsLoaded, cuFuncLoad, cuKernelGetAttribute
Note
Note that this function may also return error codes from previous, asynchronous launches.
- Parameters
pi – - Returned attribute value
attrib – - Attribute requested
hfunc – - Function to query attribute of
- Returns
CUDA_SUCCESS, CUDA_ERROR_DEINITIALIZED, CUDA_ERROR_NOT_INITIALIZED, CUDA_ERROR_INVALID_CONTEXT, CUDA_ERROR_INVALID_HANDLE, CUDA_ERROR_INVALID_VALUE, CUDA_ERROR_FUNCTION_NOT_LOADED
-
CUresult cuFuncGetDeviceCodeInfo(CUfunction func, CUdeviceptr *address, size_t *bytesize)
-
CUresult cuFuncGetModule(CUmodule *hmod, CUfunction hfunc)
Returns a module handle.
Returns in
*hmodthe handle of the module that functionhfuncis located in. The lifetime of the module corresponds to the lifetime of the context it was loaded in or until the module is explicitly unloaded.The CUDA runtime manages its own modules loaded into the primary context. If the handle returned by this API refers to a module loaded by the CUDA runtime, calling cuModuleUnload() on that module will result in undefined behavior.
Note
Note that this function may also return error codes from previous, asynchronous launches.
- Parameters
hmod – - Returned module handle
hfunc – - Function to retrieve module for
- Returns
CUDA_SUCCESS, CUDA_ERROR_DEINITIALIZED, CUDA_ERROR_NOT_INITIALIZED, CUDA_ERROR_INVALID_CONTEXT, CUDA_ERROR_INVALID_VALUE, CUDA_ERROR_NOT_FOUND
-
CUresult cuFuncGetName(const char **name, CUfunction hfunc)
Returns the function name for a CUfunction handle.
Returns in
**namethe function name associated with the function handlehfunc. The function name is returned as a null-terminated string. The returned name is only valid when the function handle is valid. If the module is unloaded or reloaded, one must call the API again to get the updated name. This API may return a mangled name if the function is not declared as having C linkage. If either**nameorhfuncis NULL, CUDA_ERROR_INVALID_VALUE is returned.Note
Note that this function may also return error codes from previous, asynchronous launches.
- Parameters
name – - The returned name of the function
hfunc – - The function handle to retrieve the name for
- Returns
-
CUresult cuFuncGetParamCount(CUfunction func, size_t *paramCount)
Returns the number of parameters used by the function.
Queries the number of kernel parameters used by
funcand returns it inparamCount.See also
Note
Note that this function may also return error codes from previous, asynchronous launches.
- Parameters
func – - The function to query
paramCount – - Returns the number of parameters used by the function
- Returns
-
CUresult cuFuncGetParamInfo(CUfunction func, size_t paramIndex, size_t *paramOffset, size_t *paramSize)
Returns the offset and size of a kernel parameter in the device-side parameter layout.
Queries the kernel parameter at
paramIndexintofunc'slist of parameters, and returns inparamOffsetandparamSizethe offset and size, respectively, where the parameter will reside in the device-side parameter layout. This information can be used to update kernel node parameters from the device via ::cudaGraphKernelNodeSetParam() and ::cudaGraphKernelNodeUpdatesApply().paramIndexmust be less than the number of parameters thatfunctakes.paramSizecan be set to NULL if only the parameter offset is desired.See also
Note
Note that this function may also return error codes from previous, asynchronous launches.
- Parameters
func – - The function to query
paramIndex – - The parameter index to query
paramOffset – - Returns the offset into the device-side parameter layout at which the parameter resides
paramSize – - Optionally returns the size of the parameter in the device-side parameter layout
- Returns
-
CUresult cuFuncIsLoaded(CUfunctionLoadingState *state, CUfunction function)
Returns if the function is loaded.
Returns in
statethe loading state offunction.See also
- Parameters
state – - returned loading state
function – - the function to check
- Returns
CUDA_SUCCESS, CUDA_ERROR_INVALID_HANDLE, CUDA_ERROR_INVALID_VALUE
-
CUresult cuFuncLoad(CUfunction function)
Loads a function.
Finalizes function loading for
function. Calling this API with a fully loaded function has no effect.See also
- Parameters
function – - the function to load
- Returns
CUDA_SUCCESS, CUDA_ERROR_INVALID_HANDLE, CUDA_ERROR_INVALID_VALUE
-
CUresult cuFuncSetAttribute(CUfunction hfunc, CUfunction_attribute attrib, int value)
Sets information about a function.
This call sets the value of a specified attribute
attribon the kernel given byhfuncto an integer value specified byvalThis function returns CUDA_SUCCESS if the new value of the attribute could be successfully set. If the set fails, this call will return an error. Not all attributes can have values set. Attempting to set a value on a read-only attribute will result in an error (CUDA_ERROR_INVALID_VALUE)Supported attributes for the cuFuncSetAttribute call are:
CU_FUNC_ATTRIBUTE_MAX_DYNAMIC_SHARED_SIZE_BYTES: This maximum size in bytes of dynamically-allocated shared memory. The value should contain the requested maximum size of dynamically-allocated shared memory. The sum of this value and the function attribute CU_FUNC_ATTRIBUTE_SHARED_SIZE_BYTES cannot exceed the device attribute CU_DEVICE_ATTRIBUTE_MAX_SHARED_MEMORY_PER_BLOCK_OPTIN. The maximal size of requestable dynamic shared memory may differ by GPU architecture.
CU_FUNC_ATTRIBUTE_PREFERRED_SHARED_MEMORY_CARVEOUT: On devices where the L1 cache and shared memory use the same hardware resources, this sets the shared memory carveout preference, in percent of the total shared memory. See CU_DEVICE_ATTRIBUTE_MAX_SHARED_MEMORY_PER_MULTIPROCESSOR This is only a hint, and the driver can choose a different ratio if required to execute the function.
CU_FUNC_ATTRIBUTE_REQUIRED_CLUSTER_WIDTH: The required cluster width in blocks. The width, height, and depth values must either all be 0 or all be positive. The validity of the cluster dimensions is checked at launch time. If the value is set during compile time, it cannot be set at runtime. Setting it at runtime will return CUDA_ERROR_NOT_PERMITTED.
CU_FUNC_ATTRIBUTE_REQUIRED_CLUSTER_HEIGHT: The required cluster height in blocks. The width, height, and depth values must either all be 0 or all be positive. The validity of the cluster dimensions is checked at launch time. If the value is set during compile time, it cannot be set at runtime. Setting it at runtime will return CUDA_ERROR_NOT_PERMITTED.
CU_FUNC_ATTRIBUTE_REQUIRED_CLUSTER_DEPTH: The required cluster depth in blocks. The width, height, and depth values must either all be 0 or all be positive. The validity of the cluster dimensions is checked at launch time. If the value is set during compile time, it cannot be set at runtime. Setting it at runtime will return CUDA_ERROR_NOT_PERMITTED.
CU_FUNC_ATTRIBUTE_NON_PORTABLE_CLUSTER_SIZE_ALLOWED: Indicates whether the function can be launched with non-portable cluster size. 1 is allowed, 0 is disallowed.
CU_FUNC_ATTRIBUTE_CLUSTER_SCHEDULING_POLICY_PREFERENCE: The block scheduling policy of a function. The value type is CUclusterSchedulingPolicy.
CU_FUNC_ATTRIBUTE_SHARED_MEMORY_MODE: The shared memory mode of a function. The value type is CUsharedMemoryMode / cudaSharedMemoryMode.
See also
cuCtxGetCacheConfig, cuCtxSetCacheConfig, cuFuncSetCacheConfig, cuLaunchKernel, ::cudaFuncGetAttributes, ::cudaFuncSetAttribute, cuKernelSetAttribute
Note
Note that this function may also return error codes from previous, asynchronous launches.
- Parameters
hfunc – - Function to query attribute of
attrib – - Attribute requested
value – - The value to set
- Returns
CUDA_SUCCESS, CUDA_ERROR_DEINITIALIZED, CUDA_ERROR_NOT_INITIALIZED, CUDA_ERROR_INVALID_CONTEXT, CUDA_ERROR_INVALID_HANDLE, CUDA_ERROR_INVALID_VALUE
-
CUresult cuFuncSetCacheConfig(CUfunction hfunc, CUfunc_cache config)
Sets the preferred cache configuration for a device function.
On devices where the L1 cache and shared memory use the same hardware resources, this sets through
configthe preferred cache configuration for the device functionhfunc. This is only a preference. The driver will use the requested configuration if possible, but it is free to choose a different configuration if required to executehfunc. Any context-wide preference set via cuCtxSetCacheConfig() will be overridden by this per-function setting unless the per-function setting is CU_FUNC_CACHE_PREFER_NONE. In that case, the current context-wide setting will be used.This setting does nothing on devices where the size of the L1 cache and shared memory are fixed.
Launching a kernel with a different preference than the most recent preference setting may insert a device-side synchronization point.
The supported cache configurations are:
CU_FUNC_CACHE_PREFER_NONE: no preference for shared memory or L1 (default)
CU_FUNC_CACHE_PREFER_SHARED: prefer larger shared memory and smaller L1 cache
CU_FUNC_CACHE_PREFER_L1: prefer larger L1 cache and smaller shared memory
CU_FUNC_CACHE_PREFER_EQUAL: prefer equal sized L1 cache and shared memory
See also
cuCtxGetCacheConfig, cuCtxSetCacheConfig, cuFuncGetAttribute, cuLaunchKernel, ::cudaFuncSetCacheConfig, cuKernelSetCacheConfig
Note
Note that this function may also return error codes from previous, asynchronous launches.
- Parameters
hfunc – - Kernel to configure cache for
config – - Requested cache configuration
- Returns
CUDA_SUCCESS, CUDA_ERROR_INVALID_VALUE, CUDA_ERROR_DEINITIALIZED, CUDA_ERROR_NOT_INITIALIZED, CUDA_ERROR_INVALID_CONTEXT
-
CUresult cuLaunchCooperativeKernel(CUfunction f, unsigned int gridDimX, unsigned int gridDimY, unsigned int gridDimZ, unsigned int blockDimX, unsigned int blockDimY, unsigned int blockDimZ, unsigned int sharedMemBytes, CUstream hStream, void **kernelParams)
Launches a CUDA function CUfunction or a CUDA kernel CUkernel where thread blocks can cooperate and synchronize as they execute.
Invokes the function CUfunction or the kernel CUkernel
fon agridDimXxgridDimYxgridDimZgrid of blocks. Each block containsblockDimXxblockDimYxblockDimZthreads.sharedMemBytessets the amount of dynamic shared memory that will be available to each thread block.The device on which this kernel is invoked must have a non-zero value for the device attribute CU_DEVICE_ATTRIBUTE_COOPERATIVE_LAUNCH.
The total number of blocks launched cannot exceed the maximum number of blocks per multiprocessor as returned by cuOccupancyMaxActiveBlocksPerMultiprocessor (or cuOccupancyMaxActiveBlocksPerMultiprocessorWithFlags) times the number of multiprocessors as specified by the device attribute CU_DEVICE_ATTRIBUTE_MULTIPROCESSOR_COUNT.
The kernel cannot make use of CUDA dynamic parallelism.
Kernel parameters must be specified via
kernelParams. Iffhas N parameters, thenkernelParamsneeds to be an array of N pointers. Each ofkernelParams[0] throughkernelParams[N-1] must point to a region of memory from which the actual kernel parameter will be copied. The number of kernel parameters and their offsets and sizes do not need to be specified as that information is retrieved directly from the kernel’s image.Calling cuLaunchCooperativeKernel() sets persistent function state that is the same as function state set through cuLaunchKernel API
When the kernel
fis launched via cuLaunchCooperativeKernel(), the previous block shape, shared size and parameter info associated withfis overwritten.Note that to use cuLaunchCooperativeKernel(), the kernel
fmust either have been compiled with toolchain version 3.2 or later so that it will contain kernel parameter information, or have no kernel parameters. If either of these conditions is not met, then cuLaunchCooperativeKernel() will return CUDA_ERROR_INVALID_IMAGE.Note that the API can also be used to launch context-less kernel CUkernel by querying the handle using cuLibraryGetKernel() and then passing it to the API by casting to CUfunction. Here, the context to launch the kernel on will either be taken from the specified stream
hStreamor the current context in case of NULL stream.See also
cuCtxGetCacheConfig, cuCtxSetCacheConfig, cuFuncSetCacheConfig, cuFuncGetAttribute, cuLaunchCooperativeKernelMultiDevice, ::cudaLaunchCooperativeKernel, cuLibraryGetKernel, cuKernelSetCacheConfig, cuKernelGetAttribute, cuKernelSetAttribute
Note
This function uses standard default stream semantics.
Note
Note that this function may also return error codes from previous, asynchronous launches.
- Parameters
f – - Function CUfunction or Kernel CUkernel to launch
gridDimX – - Width of grid in blocks
gridDimY – - Height of grid in blocks
gridDimZ – - Depth of grid in blocks
blockDimX – - X dimension of each thread block
blockDimY – - Y dimension of each thread block
blockDimZ – - Z dimension of each thread block
sharedMemBytes – - Dynamic shared-memory size per thread block in bytes
hStream – - Stream identifier
kernelParams – - Array of pointers to kernel parameters
- Returns
CUDA_SUCCESS, CUDA_ERROR_DEINITIALIZED, CUDA_ERROR_NOT_INITIALIZED, CUDA_ERROR_INVALID_CONTEXT, CUDA_ERROR_INVALID_HANDLE, CUDA_ERROR_INVALID_IMAGE, CUDA_ERROR_INVALID_VALUE, CUDA_ERROR_LAUNCH_FAILED, CUDA_ERROR_LAUNCH_OUT_OF_RESOURCES, CUDA_ERROR_LAUNCH_TIMEOUT, CUDA_ERROR_LAUNCH_INCOMPATIBLE_TEXTURING, CUDA_ERROR_COOPERATIVE_LAUNCH_TOO_LARGE, CUDA_ERROR_SHARED_OBJECT_INIT_FAILED, CUDA_ERROR_NOT_FOUND
-
CUresult cuLaunchHostFunc(CUstream hStream, CUhostFn fn, void *userData)
Enqueues a host function call in a stream.
Enqueues a host function to run in a stream. The function will be called after currently enqueued work and will block work added after it.
The host function must not make any CUDA API calls. Attempting to use a CUDA API may result in CUDA_ERROR_NOT_PERMITTED, but this is not required. The host function must not perform any synchronization that may depend on outstanding CUDA work not mandated to run earlier. Host functions without a mandated order (such as in independent streams) execute in undefined order and may be serialized.
For the purposes of Unified Memory, execution makes a number of guarantees:
The stream is considered idle for the duration of the function’s execution. Thus, for example, the function may always use memory attached to the stream it was enqueued in.
The start of execution of the function has the same effect as synchronizing an event recorded in the same stream immediately prior to the function. It thus synchronizes streams which have been “joined” prior to the function.
Adding device work to any stream does not have the effect of making the stream active until all preceding host functions and stream callbacks have executed. Thus, for example, a function might use global attached memory even if work has been added to another stream, if the work has been ordered behind the function call with an event.
Completion of the function does not cause a stream to become active except as described above. The stream will remain idle if no device work follows the function, and will remain idle across consecutive host functions or stream callbacks without device work in between. Thus, for example, stream synchronization can be done by signaling from a host function at the end of the stream.
Note that, in contrast to cuStreamAddCallback, the function will not be called in the event of an error in the CUDA context.
See also
cuStreamCreate, cuStreamQuery, cuStreamSynchronize, cuStreamWaitEvent, cuStreamDestroy, cuMemAllocManaged, cuStreamAttachMemAsync, cuStreamAddCallback
Note
This function uses standard default stream semantics.
Note
Note that this function may also return error codes from previous, asynchronous launches.
- Parameters
hStream – - Stream to enqueue function call in
fn – - The function to call once preceding stream operations are complete
userData – - User-specified data to be passed to the function
- Returns
CUDA_SUCCESS, CUDA_ERROR_DEINITIALIZED, CUDA_ERROR_NOT_INITIALIZED, CUDA_ERROR_INVALID_CONTEXT, CUDA_ERROR_INVALID_HANDLE, CUDA_ERROR_NOT_SUPPORTED
-
CUresult cuLaunchHostFunc_v2(CUstream hStream, CUhostFn fn, void *userData, unsigned int syncMode)
Enqueues a host function call in a stream.
Enqueues a host function to run in a stream. The function will be called after currently enqueued work and will block work added after it.
The host function must not make any CUDA API calls. Attempting to use a CUDA API may result in CUDA_ERROR_NOT_PERMITTED, but this is not required. The host function must not perform any synchronization that may depend on outstanding CUDA work not mandated to run earlier. Host functions without a mandated order (such as in independent streams) execute in undefined order and may be serialized.
For the purposes of Unified Memory, execution makes a number of guarantees:
The stream is considered idle for the duration of the function’s execution. Thus, for example, the function may always use memory attached to the stream it was enqueued in.
The start of execution of the function has the same effect as synchronizing an event recorded in the same stream immediately prior to the function. It thus synchronizes streams which have been “joined” prior to the function.
Adding device work to any stream does not have the effect of making the stream active until all preceding host functions and stream callbacks have executed. Thus, for example, a function might use global attached memory even if work has been added to another stream, if the work has been ordered behind the function call with an event.
Completion of the function does not cause a stream to become active except as described above. The stream will remain idle if no device work follows the function, and will remain idle across consecutive host functions or stream callbacks without device work in between. Thus, for example, stream synchronization can be done by signaling from a host function at the end of the stream.
Note that, in contrast to cuStreamAddCallback, the function will not be called in the event of an error in the CUDA context.
See also
cuStreamCreate, cuStreamQuery, cuStreamSynchronize, cuStreamWaitEvent, cuStreamDestroy, cuMemAllocManaged, cuStreamAttachMemAsync, cuStreamAddCallback
Note
This function uses standard default stream semantics.
Note
Note that this function may also return error codes from previous, asynchronous launches.
- Parameters
hStream – - Stream to enqueue function call in
fn – - The function to call once preceding stream operations are complete
userData – - User-specified data to be passed to the function
syncMode – - Synchronization mode for the host function
- Returns
CUDA_SUCCESS, CUDA_ERROR_DEINITIALIZED, CUDA_ERROR_NOT_INITIALIZED, CUDA_ERROR_INVALID_CONTEXT, CUDA_ERROR_INVALID_HANDLE, CUDA_ERROR_NOT_SUPPORTED
-
CUresult cuLaunchKernel(CUfunction f, unsigned int gridDimX, unsigned int gridDimY, unsigned int gridDimZ, unsigned int blockDimX, unsigned int blockDimY, unsigned int blockDimZ, unsigned int sharedMemBytes, CUstream hStream, void **kernelParams, void **extra)
Launches a CUDA function CUfunction or a CUDA kernel CUkernel.
Invokes the function CUfunction or the kernel CUkernel
fon agridDimXxgridDimYxgridDimZgrid of blocks. Each block containsblockDimXxblockDimYxblockDimZthreads.sharedMemBytessets the amount of dynamic shared memory that will be available to each thread block.Kernel parameters to
fcan be specified in one of two ways:1) Kernel parameters can be specified via
kernelParams. Iffhas N parameters, thenkernelParamsneeds to be an array of N pointers. Each ofkernelParams[0] throughkernelParams[N-1] must point to a region of memory from which the actual kernel parameter will be copied. The number of kernel parameters and their offsets and sizes do not need to be specified as that information is retrieved directly from the kernel’s image.2) Kernel parameters can also be packaged by the application into a single buffer that is passed in via the
extraparameter. This places the burden on the application of knowing each kernel parameter’s size and alignment/padding within the buffer. Here is an example of using theextraparameter in this manner:size_t argBufferSize; char argBuffer[256]; // populate argBuffer and argBufferSize void *config[] = { CU_LAUNCH_PARAM_BUFFER_POINTER, argBuffer, CU_LAUNCH_PARAM_BUFFER_SIZE, &argBufferSize, CU_LAUNCH_PARAM_END }; status = cuLaunchKernel(f, gx, gy, gz, bx, by, bz, sh, s, NULL, config);
The
extraparameter exists to allow cuLaunchKernel to take additional less commonly used arguments.extraspecifies a list of names of extra settings and their corresponding values. Each extra setting name is immediately followed by the corresponding value. The list must be terminated with either NULL or CU_LAUNCH_PARAM_END.CU_LAUNCH_PARAM_END, which indicates the end of the
extraarray;CU_LAUNCH_PARAM_BUFFER_POINTER, which specifies that the next value in
extrawill be a pointer to a buffer containing all the kernel parameters for launching kernelf;CU_LAUNCH_PARAM_BUFFER_SIZE, which specifies that the next value in
extrawill be a pointer to a size_t containing the size of the buffer specified with CU_LAUNCH_PARAM_BUFFER_POINTER;
The error CUDA_ERROR_INVALID_VALUE will be returned if kernel parameters are specified with both
kernelParamsandextra(i.e. bothkernelParamsandextraare non-NULL).Calling cuLaunchKernel() invalidates the persistent function state set through the following deprecated APIs: cuFuncSetBlockShape(), cuFuncSetSharedSize(), cuParamSetSize(), cuParamSeti(), cuParamSetf(), cuParamSetv().
Note that to use cuLaunchKernel(), the kernel
fmust either have been compiled with toolchain version 3.2 or later so that it will contain kernel parameter information, or have no kernel parameters. If either of these conditions is not met, then cuLaunchKernel() will return CUDA_ERROR_INVALID_IMAGE.Note that the API can also be used to launch context-less kernel CUkernel by querying the handle using cuLibraryGetKernel() and then passing it to the API by casting to CUfunction. Here, the context to launch the kernel on will either be taken from the specified stream
hStreamor the current context in case of NULL stream.See also
cuCtxGetCacheConfig, cuCtxSetCacheConfig, cuFuncSetCacheConfig, cuFuncGetAttribute, ::cudaLaunchKernel, cuLibraryGetKernel, cuKernelSetCacheConfig, cuKernelGetAttribute, cuKernelSetAttribute
Note
This function uses standard default stream semantics.
Note
Note that this function may also return error codes from previous, asynchronous launches.
- Parameters
f – - Function CUfunction or Kernel CUkernel to launch
gridDimX – - Width of grid in blocks
gridDimY – - Height of grid in blocks
gridDimZ – - Depth of grid in blocks
blockDimX – - X dimension of each thread block
blockDimY – - Y dimension of each thread block
blockDimZ – - Z dimension of each thread block
sharedMemBytes – - Dynamic shared-memory size per thread block in bytes
hStream – - Stream identifier
kernelParams – - Array of pointers to kernel parameters
extra – - Extra options
- Returns
CUDA_SUCCESS, CUDA_ERROR_DEINITIALIZED, CUDA_ERROR_NOT_INITIALIZED, CUDA_ERROR_INVALID_CONTEXT, CUDA_ERROR_INVALID_HANDLE, CUDA_ERROR_INVALID_IMAGE, CUDA_ERROR_INVALID_VALUE, CUDA_ERROR_LAUNCH_FAILED, CUDA_ERROR_LAUNCH_OUT_OF_RESOURCES, CUDA_ERROR_LAUNCH_TIMEOUT, CUDA_ERROR_LAUNCH_INCOMPATIBLE_TEXTURING, CUDA_ERROR_SHARED_OBJECT_INIT_FAILED, CUDA_ERROR_NOT_FOUND
-
CUresult cuLaunchKernelEx(const CUlaunchConfig *config, CUfunction f, void **kernelParams, void **extra)
Launches a CUDA function CUfunction or a CUDA kernel CUkernel with launch-time configuration.
Invokes the function CUfunction or the kernel CUkernel
fwith the specified launch-time configurationconfig.The CUlaunchConfig structure is defined as:
typedef struct CUlaunchConfig_st { unsigned int gridDimX; unsigned int gridDimY; unsigned int gridDimZ; unsigned int blockDimX; unsigned int blockDimY; unsigned int blockDimZ; unsigned int sharedMemBytes; CUstream hStream; CUlaunchAttribute *attrs; unsigned int numAttrs; } CUlaunchConfig;
where:
CUlaunchConfig::gridDimX is the width of the grid in blocks.
CUlaunchConfig::gridDimY is the height of the grid in blocks.
CUlaunchConfig::gridDimZ is the depth of the grid in blocks.
CUlaunchConfig::blockDimX is the X dimension of each thread block.
CUlaunchConfig::blockDimX is the Y dimension of each thread block.
CUlaunchConfig::blockDimZ is the Z dimension of each thread block.
CUlaunchConfig::sharedMemBytes is the dynamic shared-memory size per thread block in bytes.
CUlaunchConfig::hStream is the handle to the stream to perform the launch in. The CUDA context associated with this stream must match that associated with function f.
CUlaunchConfig::attrs is an array of CUlaunchConfig::numAttrs continguous CUlaunchAttribute elements. The value of this pointer is not considered if CUlaunchConfig::numAttrs is zero. However, in that case, it is recommended to set the pointer to NULL.
CUlaunchConfig::numAttrs is the number of attributes populating the first CUlaunchConfig::numAttrs positions of the CUlaunchConfig::attrs array.
Launch-time configuration is specified by adding entries to CUlaunchConfig::attrs. Each entry is an attribute ID and a corresponding attribute value.
The CUlaunchAttribute structure is defined as:
where:typedef struct CUlaunchAttribute_st { CUlaunchAttributeID id; CUlaunchAttributeValue value; } CUlaunchAttribute;
::CUlaunchAttribute::id is a unique enum identifying the attribute.
::CUlaunchAttribute::value is a union that hold the attribute value.
An example of using the
configparameter:CUlaunchAttribute coopAttr = {.id = CU_LAUNCH_ATTRIBUTE_COOPERATIVE, .value = 1}; CUlaunchConfig config = {... // set block and grid dimensions .attrs = &coopAttr, .numAttrs = 1}; cuLaunchKernelEx(&config, kernel, NULL, NULL);
The CUlaunchAttributeID enum is defined as:
typedef enum CUlaunchAttributeID_enum { CU_LAUNCH_ATTRIBUTE_IGNORE = 0, CU_LAUNCH_ATTRIBUTE_ACCESS_POLICY_WINDOW = 1, CU_LAUNCH_ATTRIBUTE_COOPERATIVE = 2, CU_LAUNCH_ATTRIBUTE_SYNCHRONIZATION_POLICY = 3, CU_LAUNCH_ATTRIBUTE_CLUSTER_DIMENSION = 4, CU_LAUNCH_ATTRIBUTE_CLUSTER_SCHEDULING_POLICY_PREFERENCE = 5, CU_LAUNCH_ATTRIBUTE_PROGRAMMATIC_STREAM_SERIALIZATION = 6, CU_LAUNCH_ATTRIBUTE_PROGRAMMATIC_EVENT = 7, CU_LAUNCH_ATTRIBUTE_PRIORITY = 8, CU_LAUNCH_ATTRIBUTE_MEM_SYNC_DOMAIN_MAP = 9, CU_LAUNCH_ATTRIBUTE_MEM_SYNC_DOMAIN = 10, CU_LAUNCH_ATTRIBUTE_PREFERRED_CLUSTER_DIMENSION = 11, CU_LAUNCH_ATTRIBUTE_LAUNCH_COMPLETION_EVENT = 12, CU_LAUNCH_ATTRIBUTE_DEVICE_UPDATABLE_KERNEL_NODE = 13, } CUlaunchAttributeID;
and the corresponding CUlaunchAttributeValue union as :
typedef union CUlaunchAttributeValue_union { CUaccessPolicyWindow accessPolicyWindow; int cooperative; CUsynchronizationPolicy syncPolicy; struct { unsigned int x; unsigned int y; unsigned int z; } clusterDim; CUclusterSchedulingPolicy clusterSchedulingPolicyPreference; int programmaticStreamSerializationAllowed; struct { CUevent event; int flags; int triggerAtBlockStart; } programmaticEvent; int priority; CUlaunchMemSyncDomainMap memSyncDomainMap; CUlaunchMemSyncDomain memSyncDomain; struct { unsigned int x; unsigned int y; unsigned int z; } preferredClusterDim; struct { CUevent event; int flags; } launchCompletionEvent; struct { int deviceUpdatable; CUgraphDeviceNode devNode; } deviceUpdatableKernelNode; } CUlaunchAttributeValue;
Setting CU_LAUNCH_ATTRIBUTE_COOPERATIVE to a non-zero value causes the kernel launch to be a cooperative launch, with exactly the same usage and semantics of cuLaunchCooperativeKernel.
Setting CU_LAUNCH_ATTRIBUTE_PROGRAMMATIC_STREAM_SERIALIZATION to a non-zero values causes the kernel to use programmatic means to resolve its stream dependency — enabling the CUDA runtime to opportunistically allow the grid’s execution to overlap with the previous kernel in the stream, if that kernel requests the overlap.
CU_LAUNCH_ATTRIBUTE_PROGRAMMATIC_EVENT records an event along with the kernel launch. Event recorded through this launch attribute is guaranteed to only trigger after all block in the associated kernel trigger the event. A block can trigger the event through PTX launchdep.release or CUDA builtin function cudaTriggerProgrammaticLaunchCompletion(). A trigger can also be inserted at the beginning of each block’s execution if triggerAtBlockStart is set to non-0. Note that dependents (including the CPU thread calling cuEventSynchronize()) are not guaranteed to observe the release precisely when it is released. For example, cuEventSynchronize() may only observe the event trigger long after the associated kernel has completed. This recording type is primarily meant for establishing programmatic dependency between device tasks. The event supplied must not be an interprocess or interop event. The event must disable timing (i.e. created with CU_EVENT_DISABLE_TIMING flag set).
CU_LAUNCH_ATTRIBUTE_LAUNCH_COMPLETION_EVENT records an event along with the kernel launch. Nominally, the event is triggered once all blocks of the kernel have begun execution. Currently this is a best effort. If a kernel B has a launch completion dependency on a kernel A, B may wait until A is complete. Alternatively, blocks of B may begin before all blocks of A have begun, for example:
If B can claim execution resources unavailable to A, for example if they run on different GPUs.
If B is a higher priority than A.
Exercise caution if such an ordering inversion could lead to deadlock. The event supplied must not be an interprocess or interop event. The event must disable timing (i.e. must be created with the CU_EVENT_DISABLE_TIMING flag set).
Setting CU_LAUNCH_ATTRIBUTE_DEVICE_UPDATABLE_KERNEL_NODE to 1 on a captured launch causes the resulting kernel node to be device-updatable. This attribute is specific to graphs, and passing it to a launch in a non-capturing stream results in an error. Passing a value other than 0 or 1 is not allowed.
On success, a handle will be returned via ::CUlaunchAttributeValue::deviceUpdatableKernelNode::devNode which can be passed to the various device-side update functions to update the node’s kernel parameters from within another kernel. For more information on the types of device updates that can be made, as well as the relevant limitations thereof, see ::cudaGraphKernelNodeUpdatesApply.
Kernel nodes which are device-updatable have additional restrictions compared to regular kernel nodes. Firstly, device-updatable nodes cannot be removed from their graph via cuGraphDestroyNode. Additionally, once opted-in to this functionality, a node cannot opt out, and any attempt to set the attribute to 0 will result in an error. Graphs containing one or more device-updatable node also do not allow multiple instantiation.
CU_LAUNCH_ATTRIBUTE_PREFERRED_CLUSTER_DIMENSION allows the kernel launch to specify a preferred substitute cluster dimension. Blocks may be grouped according to either the dimensions specified with this attribute (grouped into a “preferred substitute cluster”), or the one specified with CU_LAUNCH_ATTRIBUTE_CLUSTER_DIMENSION
attribute (grouped into a “regular
cluster”). The cluster dimensions of a “preferred substitute cluster” shall be an integer multiple greater than zero of the regular cluster dimensions. The device will attempt - on a best-effort basis - to group thread blocks into preferred clusters over grouping them into regular clusters. When it deems necessary (primarily when the device temporarily runs out of physical resources to launch the larger preferred clusters), the device may switch to launch the regular clusters instead to attempt to utilize as much of the physical device resources as possible.
Each type of cluster will have its enumeration / coordinate setup as if the grid consists solely of its type of cluster. For example, if the preferred substitute cluster dimensions double the regular cluster dimensions, there might be simultaneously a regular cluster indexed at (1,0,0), and a preferred cluster indexed at (1,0,0). In this example, the preferred substitute cluster (1,0,0) replaces regular clusters (2,0,0) and (3,0,0) and groups their blocks.
This attribute will only take effect when a regular cluster dimension has been specified. The preferred substitute The preferred substitute cluster dimension must be an integer multiple greater than zero of the regular cluster dimension and must divide the grid. It must also be no more than
maxBlocksPerCluster, if it is set in the kernel’s__launch_bounds__. Otherwise it must be less than the maximum value the driver can support. Otherwise, setting this attribute to a value physically unable to fit on any particular device is permitted.The effect of other attributes is consistent with their effect when set via persistent APIs.
See cuStreamSetAttribute for
See cuFuncSetAttribute for
Kernel parameters to
fcan be specified in the same ways that they can be using cuLaunchKernel.Note that the API can also be used to launch context-less kernel CUkernel by querying the handle using cuLibraryGetKernel() and then passing it to the API by casting to CUfunction. Here, the context to launch the kernel on will either be taken from the specified stream CUlaunchConfig::hStream or the current context in case of NULL stream.
See also
cuCtxGetCacheConfig, cuCtxSetCacheConfig, cuFuncSetCacheConfig, cuFuncGetAttribute, ::cudaLaunchKernel, ::cudaLaunchKernelEx, cuLibraryGetKernel, cuKernelSetCacheConfig, cuKernelGetAttribute, cuKernelSetAttribute
Note
This function uses standard default stream semantics.
Note
Note that this function may also return error codes from previous, asynchronous launches.
- Parameters
config – - Config to launch
f – - Function CUfunction or Kernel CUkernel to launch
kernelParams – - Array of pointers to kernel parameters
extra – - Extra options
- Returns
CUDA_SUCCESS, CUDA_ERROR_DEINITIALIZED, CUDA_ERROR_NOT_INITIALIZED, CUDA_ERROR_INVALID_CONTEXT, CUDA_ERROR_INVALID_HANDLE, CUDA_ERROR_INVALID_IMAGE, CUDA_ERROR_INVALID_VALUE, CUDA_ERROR_LAUNCH_FAILED, CUDA_ERROR_LAUNCH_OUT_OF_RESOURCES, CUDA_ERROR_LAUNCH_TIMEOUT, CUDA_ERROR_LAUNCH_INCOMPATIBLE_TEXTURING, CUDA_ERROR_COOPERATIVE_LAUNCH_TOO_LARGE, CUDA_ERROR_SHARED_OBJECT_INIT_FAILED, CUDA_ERROR_NOT_FOUND
-
CUresult cuMultiKernelCooperativeDomainCreate(CUcontext ctx, CUmultiKernelCooperativeDomain *domain_out, CUmultiKernelCooperativeDomain domain, cuuint64_t tag, void *commObj, CUhostCommunicationsVtable *commVtable, unsigned int localCount, void *localId)
Creates a new multi-kernel cooperative domain.
Creates a new multi-kernel cooperative domain. See CUmultiKernelCooperativeDomain for what these domains are used for. This is a collective operation and as such uses a CUhostCommunicationsVtable and may use an existing CUmultiKernelCooperativeDomain. This domain is nullable for purposes of bootstrapping, similar for example to MPI_COMM_WORLD. This default domain is still subject to domain ordering requirements, excepting for
tagas described below.- Parameters
ctx – - The context in which to instantiate this domain. Every context which wishes to use a domain must make a separate call to this API even within the same process.
domain_out – - Location to store the new domain.
domain – - Optional input domain. Note that operations in a given domain must be in a well-defined order, including the null domain. See CUmultiKernelCooperativeDomain. The exception to this is calls to this method when distinct tags are provided.
tag – - Optional tag to disambiguate domains created from the same parent domain in an unordered fashion. Note that this tag is not passed to methods on the communicator object. If using tags, it is recommended that the communicator object also implicitly wrap the tag. Note that distinct communicator objects can wrap the same underlying handles, and that underlying communications stacks must support issuing tagged operations in unordered fashion without risk of deadlock.
commObj – - The communicator object to use for coordinating the domain creation.
commVtable – - The vtable associated with the communicator object.
localCount – - How many participants to expect for this operation in the local process. In-process coordination will be handled locally by CUDA. Calls to the communicator object are only made for inter-process operations and each operation is initiated only once per process.
localId – - Used for matching up local calls. The same ID should not be used for different collective operations that may race.
- Returns
CUDA_SUCCESS, CUDA_ERROR_DEINITIALIZED, CUDA_ERROR_INVALID_CONTEXT
-
CUresult cuMultiKernelCooperativeDomainDestroy(CUcontext ctx, CUmultiKernelCooperativeDomain domain, void *commObj, CUhostCommunicationsVtable *commVtable, unsigned int localCount, void *localId)
Destroys a multi-kernel cooperative domain.
Destroys a multi-kernel cooperative domain. It is not necessary to wait for kernels launched against this domain to complete before calling this method. This is a collective operation and as such uses a CUhostCommunicationsVtable.
See also
- Parameters
ctx – - The context the domain handle belogns to. Every context which participated in the creation must make a separate call to this API even within the same process.
domain – - The domain to destroy. This is also the domain in which this operation must be ordered, and is necessarily the last operation against the domain.
commObj – - The communicator object to use for coordinating the domain creation.
commVtable – - The vtable associated with the communicator object.
localCount – - How many participants to expect for this operation in the local process. In-process coordination will be handled locally by CUDA. Calls to the communicator object are only made for inter-process operations and each operation is initiated only once per process.
localId – - Used for matching up local calls. The same ID should not be used for different collective operations that may race.
- Returns
CUDA_SUCCESS, CUDA_ERROR_DEINITIALIZED, CUDA_ERROR_INVALID_CONTEXT