Compile and run a CUDA kernel
This guide compiles and runs the smallest end-to-end CUDA program: a kernel that adds 1 to every integer in a 128-element array on an NVIDIA GPU and copies the result back to the CPU.
It implements the exact same add-one job shown across platforms in GPU compute, alongside the browser WebGPU version in Draw a triangle and run a compute shader and the AMD ROCm version in Compile and run a HIP kernel.
Verify the driver and compiler
Confirm that both the NVIDIA driver and the CUDA compiler (nvcc) are available:
nvidia-smi
nvcc --version
Having a working driver without the compiler is common: nvidia-smi lists the GPU and prints a CUDA version in its header, while nvcc is not on PATH.
That header version only states the newest CUDA toolkit the installed driver accepts — it does not mean the toolkit is installed.
If nvcc is missing, install a CUDA toolkit release supported by your driver before continuing.
Write the kernel and host program
Save the following listing as add_one.cu:
#include <cstdio>
#include <cuda_runtime.h>
__global__ void add_one(unsigned* data, int n) {
int i = blockIdx.x * blockDim.x + threadIdx.x;
if (i < n) data[i] += 1u;
}
int main() {
const int n = 128;
unsigned host[128];
for (int i = 0; i < n; ++i) host[i] = static_cast<unsigned>(i);
unsigned* device = nullptr;
cudaMalloc(&device, sizeof(host));
cudaMemcpy(device, host, sizeof(host), cudaMemcpyHostToDevice);
add_one<<<(n + 63) / 64, 64>>>(device, n);
cudaError_t err = cudaDeviceSynchronize();
cudaMemcpy(host, device, sizeof(host), cudaMemcpyDeviceToHost);
cudaFree(device);
if (err != cudaSuccess) {
std::fprintf(stderr, "%s\n", cudaGetErrorString(err));
return 1;
}
std::printf("%u %u %u\n", host[0], host[1], host[127]);
return host[0] == 1 && host[1] == 2 && host[127] == 128 ? 0 : 2;
}
The global qualifier marks add_one as a GPU kernel launched from CPU code.
cudaMalloc allocates GPU-managed memory (separate from CPU process mmap), and the two cudaMemcpy calls move the 128 integers to the card and back.
The launch syntax <<<(n + 63) / 64, 64>>> dispatches 2 thread blocks of 64 threads each — matching dispatchWorkgroups(2) in the WebGPU tutorial and vkCmdDispatch(cmd, 2, 1, 1) in the Vulkan compute how-to.
cudaDeviceSynchronize() waits for the kernel to complete and surfaces any launch or execution error.
Compile and run
On Linux:
nvcc add_one.cu -o add_one
./add_one
On Windows (from a shell where both nvcc and the Visual Studio C++ host compiler cl.exe are on PATH), nvcc writes add_one.exe and you run add_one.
A successful run prints:
1 2 128
Exit code 0 confirms all three sampled positions incremented as expected.
If cudaGetErrorString prints an error instead, the most frequent causes are running on a machine without an NVIDIA CUDA device or compiling with a CUDA toolkit newer than the installed driver supports.
A compiled CUDA binary runs only on NVIDIA hardware (including Jetson boards). For AMD GPUs on Linux, see Compile and run a HIP kernel; for cross-vendor native compute without a vendor compute SDK, see Dispatch a Vulkan compute shader; for Linux hardware video decoding, see Decode video with VA-API.