eyalroz/cuda-api-wrappers
Thin, unified, C++-flavored wrappers for the CUDA APIs
cuda-api-wrappers is a C++ library that makes NVIDIA's CUDA programming interfaces easier and safer to use. Instead of calling the raw C-style CUDA functions directly, you get modern C++ wrappers that handle error checking, resource management, and provide more intuitive names and structures.
What it does
The library wraps several CUDA APIs:
- The low-level Driver API
- The higher-level Runtime API
- NVRTC (for compiling CUDA code at runtime)
- PTX compiler (since CUDA 11.1)
- nvFatbin (since CUDA 12.4)
- NVTX (profiling tools)
It's designed for people who would otherwise use these APIs directly, giving them a more consistent and modern C++ experience.
Key features
- Header-only: No separate compilation step – just include the headers.
- Exceptions on errors: Functions throw exceptions instead of returning error codes, so you don't have to check every call.
- RAII resource management: Objects like devices, streams, events, and kernels automatically clean up when they go out of scope.
- No raw handles: You work with typed proxy objects instead of numeric IDs and pointers.
- Convenience methods: Plain data structures (like device properties) get helpful operators and methods.
- Namespacing: Related functionality is grouped logically (e.g.,
cuda::memory::host::allocate). - Lightweight: No hidden work or heavy inheritance; wrappers compile down to the underlying CUDA calls.
- Free license: 3-BSD.
Why it exists
The original CUDA APIs have several pain points:
- They are C-style, requiring manual error checking and pointer management.
- You often need to use both Runtime and Driver APIs together, which is tricky.
- Resource cleanup is easy to forget.
- Many similar-looking functions are easy to confuse.
The library aims to fix these issues while staying faithful to the underlying CUDA behavior – it doesn't force new abstractions or conventions.
Requirements
- CUDA 9.0 or later (11.x recommended)
- A C++11-capable compiler
- CMake 3.25+ (for building with CMake)
- An NVIDIA GPU with Unified Virtual Addressing (Fermi or newer)
How to use it
You can get the library via:
- Downloading a release tarball and installing it
- Using Conan or vcpkg package managers
- Using CMake's
FetchContentto pull it directly into your project
Then, in your CMake file, you link against targets like cuda-api-wrappers::runtime-and-driver, cuda-api-wrappers::nvrtc, or cuda-api-wrappers::nvtx.
If you don't use CMake, you can just add the src/ directory to your include path and link the necessary CUDA libraries yourself.
Coverage
Most of the Runtime, Driver, NVRTC, and NVTX APIs are wrapped, but not all. Missing features are listed as GitHub issues. Graphics interop (OpenGL, Direct3D, etc.) is not covered, and there are no short-term plans to add it.
Example code
The README shows snippets like:
auto compilation_output = my_program.compile();
auto module = cuda::module::create(context, compilation_output);
auto my_kernel = module.get_kernel(mangled_kernel_name);
auto gpu_side_buffer = cuda::memory::make_unique_span<float>(device, n);
cuda::memory::copy(gpu_side_buffer, host_side_buffer);
auto launch_config = cuda::launch_config_builder().overall_size(n).block_size(256).build();
cuda::launch(my_kernel, launch_config, gpu_side_buffer.data());
This shows how clean and readable the wrappers make CUDA code.
Examples and help
The project includes example programs, including modified CUDA samples and per-module coverage examples. You can build them with CMake. If you find bugs or want to contribute, the README invites you to file issues or contact the author.
In short, cuda-api-wrappers is a modern C++ wrapper library that makes CUDA programming more pleasant and less error-prone, while staying true to the original APIs.
Related
- Project
- Project
- Project
- Project
- Project