cuda.bindings._v2.driver.ctx_create_v4#
- cuda.bindings._v2.driver.ctx_create_v4(
- ctx_create_params,
- unsigned int flags,
- int dev,
Create a CUDA context.
Creates a new CUDA context and associates it with the calling thread. The
flagsparameter is described below. The context is created with a usage count of 1 and the caller ofcuCtxCreate()must callcuCtxDestroy()when done using the context. If a context is already current to the thread, it is supplanted by the newly created context and may be restored by a subsequent call tocuCtxPopCurrent(). A regular CUDA context can be created by settingctx_create_paramsto NULL. A CUDA context can be created with execution affinity. The type and the amount of execution resource the context can use is limited byparamsArrayandnumExecAffinityParamsinexecAffinity. TheparamsArrayis an array ofCUexecAffinityParamand thenumExecAffinityParamsdescribes the size of the paramsArray. If twoCUexecAffinityParamin the array have the same type, the latter execution affinity parameter overrides the former execution affinity parameter. The supported execution affinity types are:.CU_EXEC_AFFINITY_TYPE_SM_COUNTlimits the portion of SMs that the context can use. The portion of SMs is specified as the number of SMs viaCUexecAffinitySmCount. This limit will be internally rounded up to the next hardware-supported amount. Hence, it is imperative to query the actual execution affinity of the context viacuCtxGetExecAffinityafter context creation. Currently, this attribute is only supported under Volta+ MPS.
A CUDA context can be created in CIG(CUDA in Graphics) mode by setting
cigParams. Data from graphics client is shared with CUDA via thesharedDataincigParams. Support for D3D12 graphics client can be determined usingdevice_get_attribute()withCU_DEVICE_ATTRIBUTE_D3D12_CIG_SUPPORTED.sharedDatais a ID3D12CommandQueue handle. Support for Vulkan graphics client can be determined usingdevice_get_attribute()withCU_DEVICE_ATTRIBUTE_VULKAN_CIG_SUPPORTED.sharedDatais a Nvidia specific data blob populated by calling vkGetExternalComputeQueueDataNV().execAffinityParamsandcigParamsare mutually exclusive and cannot both be non-NULL. Setting both to non-NULL values will result in undefined behavior. If bothexecAffinityParamsandcigParamsare NULL, the context will be created as a regular CUDA context. The three LSBs of theflagsparameter can be used to control how the OS thread, which owns the CUDA context at the time of an API call, interacts with the OS scheduler when waiting for results from the GPU. Only one of the scheduling flags can be set when creating a context.CU_CTX_SCHED_SPIN: Instruct CUDA to actively spin when waiting for results from the GPU. This can decrease latency when waiting for the GPU, but may lower the performance of CPU threads if they are performing work in parallel with the CUDA thread.CU_CTX_SCHED_YIELD: Instruct CUDA to yield its thread when waiting for results from the GPU. This can increase latency when waiting for the GPU, but can increase the performance of CPU threads performing work in parallel with the GPU.CU_CTX_SCHED_BLOCKING_SYNC: Instruct CUDA to block the CPU thread on a synchronization primitive when waiting for the GPU to finish work.CU_CTX_BLOCKING_SYNC: Instruct CUDA to block the CPU thread on a synchronization primitive when waiting for the GPU to finish work. Deprecated: This flag was deprecated as of CUDA 4.0 and was replaced withCU_CTX_SCHED_BLOCKING_SYNC.CU_CTX_SCHED_AUTO: The default value if theflagsparameter is zero, uses a heuristic based on the number of active CUDA contexts in the processCand the number of logical processors in the systemP. IfC>P, then CUDA will yield to other OS threads when waiting for the GPU (CU_CTX_SCHED_YIELD), otherwise CUDA will not yield while waiting for results and actively spin on the processor (CU_CTX_SCHED_SPIN). Additionally, on Tegra devices,CU_CTX_SCHED_AUTOuses a heuristic based on the power profile of the platform and may chooseCU_CTX_SCHED_BLOCKING_SYNCfor low-powered devices.CU_CTX_MAP_HOST: Instruct CUDA to support mapped pinned allocations. This flag must be set in order to allocate pinned host memory that is accessible to the GPU.CU_CTX_LMEM_RESIZE_TO_MAX: Instruct CUDA to not reduce local memory after resizing local memory for a kernel. This can prevent thrashing by local memory allocations when launching many kernels with high local memory usage at the cost of potentially increased memory usage. Deprecated: This flag is deprecated and the behavior enabled by this flag is now the default and cannot be disabled. Instead, the per-thread stack size can be controlled withctx_set_limit().CU_CTX_COREDUMP_ENABLE: If GPU coredumps have not been enabled globally withcuCoredumpSetAttributeGlobalor environment variables, this flag can be set during context creation to instruct CUDA to create a coredump if this context raises an exception during execution. These environment variables are described in the CUDA-GDB user guide under the “GPU core dump support” section. The initial attributes will be taken from the global attributes at the time of context creation. The other attributes that control coredump output can be modified by callingcuCoredumpSetAttributefrom the created context after it becomes current. This flag is not supported when CUDA context is created in CIG(CUDA in Graphics) mode.CU_CTX_USER_COREDUMP_ENABLE: If user-triggered GPU coredumps have not been enabled globally withcuCoredumpSetAttributeGlobalor environment variables, this flag can be set during context creation to instruct CUDA to create a coredump if data is written to a certain pipe that is present in the OS space. These environment variables are described in the CUDA-GDB user guide under the “GPU core dump support” section. It is important to note that the pipe namemustbe set withcuCoredumpSetAttributeGlobalbefore creating the context if this flag is used. Setting this flag implies thatCU_CTX_COREDUMP_ENABLEis set. The initial attributes will be taken from the global attributes at the time of context creation. The other attributes that control coredump output can be modified by callingcuCoredumpSetAttributefrom the created context after it becomes current. Setting this flag on any context creation is equivalent to setting theCU_COREDUMP_ENABLE_USER_TRIGGERattribute totrueglobally. This flag is not supported when CUDA context is created in CIG(CUDA in Graphics) mode.CU_CTX_SYNC_MEMOPS: Ensures that synchronous memory operations initiated on this context will always synchronize. See further documentation in the section titled “API Synchronization behavior” to learn more about cases when synchronous memory operations can exhibit asynchronous behavior.
Context creation will fail with
CUDA_ERROR_UNKNOWNif the compute mode of the device isCU_COMPUTEMODE_PROHIBITED. The functiondevice_get_attribute()can be used withCU_DEVICE_ATTRIBUTE_COMPUTE_MODEto determine the compute mode of the device. Thenvidia-smitool can be used to set the compute mode for * devices. Documentation fornvidia-smican be obtained by passing a -h option to it. Context creation will fail withCUDA_ERROR_INVALID_VALUEif invalid parameter was passed by client to create the CUDA context. Context creation in CIG mode will fail withCUDA_ERROR_NOT_SUPPORTEDif CIG is not supported by the device or the driver.- Parameters:
ctx_create_params (intptr_t) – Context creation parameters. Can be NULL to create a regular CUDA context. See
CUctx_create_paramsfor details.flags (unsigned int) – Context creation flags.
dev (int) – Device to create context on.
- Returns:
Returned context handle of the new context.
- Return type:
intptr_t
See also
cuCtxCreate_v4