Run CUDA Without a GPU Using Software – Step‑by‑Step Guide
In this article
Yes, you can execute CUDA programs on a machine that has no NVIDIA GPU by using a software emulator that translates PTX code to run on the host CPU. The most reliable free option today is the open‑source project GPUOcelot, which provides a PTX‑to‑CPU backend and integrates with the standard CUDA toolchain.
What You’ll Need
- A supported Linux distribution (Ubuntu 20.04 LTS or newer is used in the example). Windows users can use the Windows Subsystem for Linux (WSL2) or a Linux virtual machine.
- The NVIDIA CUDA Toolkit (any recent version; the guide uses CUDA 12.1). The toolkit supplies
nvcc, the CUDA runtime libraries, and header files. - Git and a C++ compiler (gcc/g++). GPUOcelot builds with standard CMake, so
cmakeis also required. - Internet access to download source packages.
Step‑by‑Step
- Install the CUDA Toolkit. On Ubuntu run:
sudo apt-get update sudo apt-get install nvidia-cuda-toolkit
The package placesnvccin/usr/binand the libraries under/usr/lib/cuda. Verify the installation withnvcc --version. - Install build dependencies for GPUOcelot. Execute:
sudo apt-get install git cmake build-essential libelf-dev libboost-all-dev
These packages provide the compiler, CMake, and supporting libraries needed for the emulator. - Clone the GPUOcelot repository. In your home directory run:
git clone https://github.com/gtcasl/ocelot.git cd ocelot
The repository contains the source code and CMake build scripts. - Build GPUOcelot. Create a build folder and compile:
mkdir build && cd build cmake .. -DCMAKE_BUILD_TYPE=Release make -j$(nproc)
When the build finishes, the executableocelot-runwill be located inbuild/bin. - Add the emulator to your PATH. This makes it easy to call from any directory:
echo "export PATH=\$HOME/ocelot/build/bin:\$PATH" >> ~/.bashrc source ~/.bashrc
- Write a simple CUDA program. Save the following as
vector_add.cu:#include <cuda_runtime.h> #include <stdio.h> __global__ void add(int *a, int *b, int *c, int n) { int idx = blockIdx.x * blockDim.x + threadIdx.x; if (idx < n) c[idx] = a[idx] + b[idx]; } int main() { const int N = 1<<20; // one million elements size_t bytes = N * sizeof(int); int *h_a = (int*)malloc(bytes); int *h_b = (int*)malloc(bytes); int *h_c = (int*)malloc(bytes); for (int i = 0; i < N; ++i) { h_a[i] = i; h_b[i] = 2*i; } int *d_a, *d_b, *d_c; cudaMalloc(&d_a, bytes); cudaMalloc(&d_b, bytes); cudaMalloc(&d_c, bytes); cudaMemcpy(d_a, h_a, bytes, cudaMemcpyHostToDevice); cudaMemcpy(d_b, h_b, bytes, cudaMemcpyHostToDevice); add<<<(N+255)/256, 256>>>(d_a, d_b, d_c, N); cudaMemcpy(h_c, d_c, bytes, cudaMemcpyDeviceToHost); printf("c[0]=%d, c[N‑1]=%d\n", h_c[0], h_c[N-1]); cudaFree(d_a); cudaFree(d_b); cudaFree(d_c); free(h_a); free(h_b); free(h_c); return 0; }This program adds two integer vectors on the GPU. We will run it on the CPU emulator. - Compile the program for PTX only. Use
nvccwith the-ptxflag to generate a PTX file, then link it with the emulator runtime:nvcc -ptx vector_add.cu -o vector_add.ptx nvcc -c vector_add.cu -o vector_add.o nvcc vector_add.o -L$HOME/ocelot/build/lib -locelot -o vector_add_cpu
The first command createsvector_add.ptx. The second compiles the host code into an object file. The final line links the host object with the Ocelot library (-locelot) which provides the PTX‑to‑CPU translation layer. - Run the executable. Simply execute:
./vector_add_cpu
Because the binary is linked against Ocelot, the CUDA kernel is executed on the host CPU. You should see output similar to:c[0]=0, c[1048575]=3145725
The numbers confirm that the kernel performed the addition correctly. - Optional: Use the
ocelot-runhelper. Ocelot also ships a command‑line wrapper that loads a PTX file directly, useful for quick tests without recompiling:ocelot-run vector_add.ptx
The wrapper expects the PTX to contain amainfunction that follows the CUDA runtime API conventions. For more complex projects, linking as shown in the previous step is recommended. - Validate performance. CPU emulation is considerably slower than a real GPU. You can measure execution time with the standard
timecommand:time ./vector_add_cpu
Compare the result with a machine that has an NVIDIA GPU to understand the overhead.
Common Mistakes
- Skipping the PTX generation step. Ocelot works with PTX, not with a fully linked
.cubin. If you compile only withnvcc -cubin, the emulator will not find the intermediate representation and will abort. - Linking against the wrong library. The CUDA runtime library (
-lcudart) cannot execute PTX on the CPU. You must link with Ocelot’s-locelot(or use the wrapper) so the PTX interpreter is available. - Running on Windows without WSL2. GPUOcelot does not provide native Windows binaries. Attempting to run the Linux build directly on Windows will result in “cannot execute binary file”. Use WSL2 or a Linux VM.
- Forgetting to export the PATH. If
ocelot-runis not in yourPATH, the command will return “command not found”. Re‑source.bashrcor use the full path. - Mismatched CUDA version. The PTX produced by a newer CUDA release may use ISA features not yet supported by Ocelot. If you see “unsupported PTX version” errors, install an older CUDA toolkit (e.g., 11.8) and re‑compile.
Tips
- When debugging, add
cudaDeviceSynchronize(); cudaGetLastError();after kernel launches. Ocelot reports errors through the same CUDA API calls, making debugging identical to a real GPU. - Use
-O2or higher optimization flags for the host code; the CPU side benefits heavily from compiler optimizations, while PTX execution speed is limited by the interpreter. - If you need to run many kernels, consider building a static version of Ocelot (
-DBUILD_STATIC=ONin CMake) to avoid the overhead of dynamic library loading. - For large projects, keep PTX files in a dedicated
ptx/directory and add a Makefile rule that automatically regenerates PTX whenever a.cusource changes. - Remember that libraries such as cuBLAS, cuFFT, or cuDNN do not have CPU fall‑backs in Ocelot. If your code depends on those, you will need to replace them with CPU equivalents (e.g., OpenBLAS, FFTW) before emulation.
Troubleshooting
If the program fails to start or crashes, follow this checklist:
- Check that
nvccproduced PTX. Verify the file size ofvector_add.ptx. A zero‑byte file indicates that the compilation step failed; re‑run thenvcc -ptxcommand and inspect the console for errors. - Confirm Ocelot is in the library path. Run
ldd vector_add_cpuand look for a line containinglibocelot.so. If it says “not found”, add the build directory toLD_LIBRARY_PATH:export LD_LIBRARY_PATH=$HOME/ocelot/build/lib:$LD_LIBRARY_PATH
- Inspect runtime error messages. Ocelot prints detailed diagnostics to
stderr. Common messages include “unsupported PTX version” or “illegal memory access”. Adjust the CUDA toolkit version or simplify the kernel accordingly. - Verify CPU architecture compatibility. Ocelot’s interpreter is built for x86_64. If you are on ARM (e.g., Apple Silicon or Raspberry Pi), the binary will not run. In that case, consider using a Docker container that emulates x86_64 or switch to a different emulator such as “LLVM‑based PTX‑to‑CPU” which has experimental ARM support.
- Re‑build Ocelot after system updates. Kernel or glibc updates can break the binary compatibility of the shared library. Delete the
buildfolder and repeat the CMake/Make steps. - If all else fails, test with a minimal kernel. Replace your kernel with a simple
__global__ void hello(){}that does nothing. If this runs, the problem lies in kernel logic rather than the emulation layer.
Frequently Asked Questions
Can I run any CUDA program with GPUOcelot?
GPUOcelot supports most PTX‑1.3 features, but very recent PTX extensions (e.g., cooperative groups, tensor‑core intrinsics) are not yet implemented. Simple arithmetic, memory operations, and control flow work reliably.
Do I need an NVIDIA driver installed?
No. The emulator runs entirely on the CPU and does not query the GPU driver stack. You can even run on a headless server without any NVIDIA hardware.
Is the performance comparable to a real GPU?
CPU emulation is typically 10‑100× slower than a modern GPU, depending on the kernel’s computational intensity and memory access pattern. It is useful for functional testing, algorithm development, and CI pipelines, but not for production‑level speed.
Can I use cuBLAS or cuDNN with the emulator?
Those libraries rely on GPU‑specific hardware acceleration and are not provided by Ocelot. Replace them with CPU equivalents (e.g., OpenBLAS, Eigen, FFTW) when you plan to run under emulation.
Will this work on Windows without WSL?
GPUOcelot currently ships only Linux binaries. On Windows you must use WSL2, a Linux VM, or a Docker container that runs a Linux base image.
Is there a commercial alternative?
NVIDIA offers a cloud‑based CUDA runtime (e.g., NVIDIA NGC) where you can rent GPU instances. That approach provides real hardware rather than emulation, but it incurs cost and requires internet access.