Compile with nvcc
You will use the NVIDIA nvcc compiler to compile CUDA codes in C or C++. The nvcc compiler is suffix-sensitive, so be sure to use the proper suffix or file extension. The following table shows the supported suffixes for the nvcc compiler. If your device code is short (the lab exercises in this roadmap are short enough), you can put your host and device codes (CPU and GPU codes) in a single .cu file, then compile it using the nvcc compiler.
| Extension | Type of File |
|---|---|
| .cu | CUDA source file, containing host and device codes |
| .c | C source code |
| .cc, .cxx, .cpp | C++ source code |
The conventional compiler flags can be used to customize compilation. For example, -O sets the optimization level, and the -pg option allows you to instrument the executable for use with the gprof profiler. For more details, see the official nvcc documentation.
In this roadmap, all lab exercises can be compiled using the following command and compiler flags.
The -o flag specifies the name of the output executable. In this example, we have optionally added -arch=compute_100 -code=sm_100 to instruct the compiler to produce object code at Compute Capability 10.0 for Blackwell-based GPUs; this is the family that the Grace Blackwell Superchips on Horizon belong to. Similarly, you may wish to add -arch=compute_90 -code=sm_90 to target the Hopper GPUs on Vista. Without such options, the code is compiled in a more general way that covers a range of NVIDIA architectures.
While it's not strictly necessary to specify the -arch and -code flags with the Compute Capability (CC) of your hardware, doing so allows nvcc to use the low-level instructions that that work best to speed up the program execution time. When these options are omitted, the code will work as expected on other architectures, but at the cost of additional JIT-compiling at run time, which causes a short delay at startup.
Note that on Horizon, Vista, and similar HPC systems, you might first need to load the correct environment module to gain access to the nvcc compiler. The following command loads the default version of CUDA on Horizon or Vista:
CVW material development is supported by NSF OAC awards 1854828, 2321040, 2323116 (UT Austin) and 2005506 (Indiana University)