What Is the CUDA Driver API? Control Contexts, Modules, and Kernel Execution
The CUDA Driver API is the lower-level interface exposed by NVIDIA’s GPU driver for explicitly managing devices, execution contexts, code modules, memory, and kernel launches. It takes more setup than the CUDA Runtime API, but helps when an application needs deliberate context or module control, or a feature exposed only through the Driver API. Most applications can still achieve full performance without calling it directly, so “lower level” does not automatically mean “faster.”
What Is the CUDA Driver API? Control Contexts, Modules, and Kernel Execution
The CUDA Driver API is the lower-level interface exposed by NVIDIA’s GPU driver for explicitly managing devices, execution contexts, code modules, memory, and kernel launches. It takes more setup than the CUDA Runtime API, but helps when an application needs deliberate context or module control, or a feature exposed only through the Driver API. Most applications can still achieve full performance without calling it directly, so “lower level” does not automatically mean “faster.”
What is the CUDA Driver API?
The Driver API is not the Toolkit compiler or Runtime library. It is an interface to the cuda dynamic library installed with the NVIDIA device driver. Its functions generally start with cu, and they use opaque handles for objects. For example, CUdevice identifies a CUDA device; CUcontext groups work and resources on a device; CUmodule is loaded GPU code; and CUfunction identifies a kernel within a module.
The Runtime API is implemented on top of the Driver API. Runtime can hide initialization and default context management, while direct Driver API use makes those decisions explicit.
When should you choose Driver API over Runtime API?
The APIs overlap, but differ in how they manage initialization, contexts, modules, and launches. Choose by required control and integration needs, not presumed speed.
| Criterion | CUDA Runtime API | CUDA Driver API |
|---|---|---|
| Initialization and resources | Usually handles initialization and default context/module management implicitly | Makes initialization, current context, and module loading/release explicit |
| Kernel launch | Concise syntax for typical CUDA C++ kernels | Uses calls to specify a function handle, launch configuration, and arguments |
| Good fit | Common GPU computation and simpler applications | Context control, dynamic module loading, some advanced Driver-only interfaces, or library integration |
| Cost and performance | Less boilerplate; sufficient performance for most applications | More control, but state, lifetimes, and errors become your responsibility; no automatic speedup |
Some advanced interfaces appear in the Driver API first or only there. For a simple kernel, however, explicit context and module management adds code and failure modes. First check whether Runtime API meets the need; choose Driver API when that control is a real requirement.
How do contexts affect resources and threads?
A CUDA context is sometimes compared to a CPU process, but it is not an operating-system process. Driver API resources and work belong to a context, and each context has its own address space. A CUdeviceptr must therefore be interpreted in the context where it was created; it cannot simply be reused in another context.
Most Driver API calls require the right context to be current on the calling host thread. A host thread can have only one current context at a time. Moving a context between threads requires detaching it and binding it according to the API rules. Destroying a context makes its associated modules, function handles, streams, events, and ordinary device allocations unusable.
This matters with plugins and libraries. Independently created contexts consume resources and incur switching costs, and one context cannot directly use another context’s resources. NVIDIA warns that multiple Driver API contexts for the same GPU in one process can carry substantial resource and performance costs. Consider sharing an application-owned context or connecting to the Runtime’s per-device primary context when integrating libraries.
How are modules and kernels loaded and launched?
Prepare kernel code as PTX or as a GPU-architecture-specific binary called cubin. The Driver API loads the code as a CUmodule in the current context, then resolves a kernel name to a CUfunction. A compatible driver can JIT-compile PTX for a target GPU; a cubin is built for a specific architecture.
To launch a kernel, cuLaunchKernel takes an explicit grid and block configuration, shared-memory size, stream, and arguments. Unlike Runtime’s <<<...>>> syntax, the Driver API does not wrap these details for you, so argument pointers and alignment must be correct. A launch can be asynchronous with respect to the host. Synchronize before the CPU consumes results and check the returned errors.
What does the flow look like for vector addition?
Consider a vector-add example like NVIDIA’s documented vectorAddDrv sample. This is a conceptual walkthrough of resource order; it was not run on a device or measured.
Prepare input arrays A and B on the CPU, initialize the Driver API, select a device, and create a context. Load a precompiled PTX module into that context and obtain the vector-add kernel’s CUfunction. Allocate device memory, copy A and B to it, and prepare output storage. Launch with the grid, block, device pointers, and length arguments; wait for completion; copy the result to the CPU; and check each output element against A[i] + B[i].
Failures can be isolated by stage. If device selection or context creation fails, check driver initialization, the selected GPU, and the context current on the calling thread. If module loading fails, check the file path, PTX version or cubin target, and driver support. If execution fails, inspect the CUresult, record its error name and description, then check launch arguments and copy sizes. An asynchronous error may surface in a later synchronization or API call, so one successful final call does not prove the whole sequence succeeded.
Driver and Runtime APIs can be mixed, but context ownership must be clear. When a Driver API context is current on a host thread, Runtime calls on that thread generally use it; without one, Runtime uses or initializes the device’s primary context. Because libraries can share that primary context, one plugin calling cudaDeviceReset() can disrupt other plugins. Do not reset a shared context you do not own, and follow the API-version mixing rules.
What is a safe Driver API workflow?
Once direct Driver API control is justified, use this order. Check every CUresult and recover from the stage that failed instead of misdiagnosing later errors.
Confirm the need for context or module control; check whether Runtime API is sufficient and prepare development tools supported by the target OS, GPU, and driver.
Build device code as PTX or cubin for the target architecture, then confirm the execution host driver supports that code format.
Initialize the Driver API, select a CUDA device, and create a context or bind the application-owned context to the calling host thread.
Load the module and obtain its function handle; allocate and copy memory in that same context, then launch with correct configuration and arguments. Synchronize before reading and validate the result.
When work and libraries are finished, release modules, memory, and other resources under the same ownership rules, then release the context. On failure, check current-context state, target architecture, asynchronous completion, and API versions mixed together.
The expected outcome is a kernel submitted in the selected context and a result that can be checked. For CUDA_ERROR_INVALID_CONTEXT, check that a context is current on the calling thread, has not been destroyed, and owns the handles in use. For a module error, check code format and target GPU. For a wrong result, isolate argument layout, memory sizes, and synchronization.
References
[CUDA Programming Guide: The CUDA Driver API](https://docs.nvidia.com/cuda/cuda-programming-guide/03-advanced/driver-api.html)
[CUDA Driver API Reference Manual](https://docs.nvidia.com/cuda/cuda-driver-api/)
[CUDA Programming Guide: CUDA Platform](https://docs.nvidia.com/cuda/cuda-programming-guide/01-introduction/cuda-platform.html)
[CUDA Runtime API: Interactions with the CUDA Driver API](https://docs.nvidia.com/cuda/cuda-runtime-api/group__CUDART__DRIVER.html)
[CUDA Driver API: Rules for Version Mixing](https://docs.nvidia.com/cuda/cuda-driver-api/version-mixing-rules.html)