Compiling C/C++ Code
This document describes the compilers and development environments for the C and C++ languages available on the HPC Vega system. On Vega, several different compilers and development tools are available as modules.
The following C/C++ compilers are available:
| Product | Languages | Module Name | Description |
|---|---|---|---|
| Intel oneAPI Compiler | C / C++ / Fortran | oneapi/ |
Intel oneAPI |
| AOCC Compiler | Clang – C and C++ / Flang – Fortran | AOCC/ |
AMD Optimizing C/C++ Compiler |
| PGI Compiler | C / C++ / Fortran / CUDA Fortran / OpenCL / other | NVHPC/ |
Portland Group Compilers |
| GNU GCC Compiler | C / C++ / Objective-C / Objective-C++ / Fortran / Go / other | GCC/ |
GCC, the GNU Compiler Collection (versions prior to 10.2.0 are also available) |
| LLVM Compiler | Clang – C / C++ | LLVM/ |
LLVM Compiler Infrastructure Project |
In addition, the following development tools are available:
| Product | Languages | Module Name | Description |
|---|---|---|---|
| CUDA Toolkit | CUDA C / C / C++ / CUDA Fortran / PyCUDA / other | CUDA/ |
Versions prior to 11.0.2 are also available |
| NVIDIA HPC SDK | C / C++ / Fortran / CUDA / other | NVHPC/ |
Version 20.11 is available |
| CMake | Build system generator for C/C++ | CMake/ |
Multiple versions are available, each built with a specific GCCcore version (see the Module usage section) |
For preparing and editing C/C++ scripts on HPC Vega, the most practical approach is to use the Visual Studio Code (VS Code) editor. You can connect to HPC Vega directly from VS Code via the Remote SSH extension. This allows you to edit files directly on the cluster. This means you can iteratively modify code, review execution results, and submit SLURM jobs from the same workspace, without needing to transfer files between your local computer and the cluster. Instructions for setting up Remote SSH in VS Code can be found here.
For working with C/C++ in Visual Studio Code, it is recommended to install the C/C++ extension and the C/C++ Extension Pack extension.
Installation and Usage
Compilers on HPC Vega are already installed as modules, so local installation is not required.
1. Searching for Available Modules
Search for available compilers using the command:
module avail GCC/
This command lists all versions of the GCC compiler currently installed on the system.
2. Loading a Module
Load a compiler using the command:
module load GCC/<version>
where <version> represents the specific compiler version.
To verify that the gcc and g++ compilers were successfully loaded, use the commands:
gcc --version
g++ --version
The same procedure applies to other compilers (LLVM, oneapi, AOCC, NVHPC, CMake, etc.):
Note: Only one compiler should be loaded in a single terminal session to avoid conflicts between different versions.
Usage Example
The following example demonstrates how to run C++ code on HPC Vega. We will allocate a compute node for the task:
salloc -n 1 --partition=cpu -t 00:30:00
ssh <node>
1. Loading the Compiler Module
module load GCC
2. Preparing the Source File
Upload or create a .cpp file in your desired location.
Example file:
#include <iostream>
#include <string>
using namespace std;
string reverseString(string s){
string reversed = "";
for(int i=1; i <= s.size(); i++){
reversed += s[s.size()-i];
}
return reversed;
}
int main() {
cout << "String:";
string s;
getline(cin, s);
cout << "Reversed string: " << reverseString(s) << endl;
return 0;
}
3. Compiling the Code
Compile the code using the command:
g++ code.cpp -o executable
where:
- g++ is the code compiler
- code.cpp is the file to be compiled
- -o executable specifies the name of the output executable file.
4. Running the Program
Run the executable file using the command:
./executable
Building a Project with CMake
For more complex projects, use CMake instead of gcc or g++. Load the compiler as a module:
module purge
module load CMake/<version>
In the terminal, navigate to the directory containing the code you want to run.
First, create or upload a CMakeLists.txt file and provide the build instructions for the program.
Example file:
cmake_minimum_required(VERSION 3.10)
project(Test)
add_executable(executable code.cpp)
The build will be performed in a separate directory, which we create and navigate into:
mkdir build
cd build
Use CMake to read the CMakeLists.txt file in the parent directory, prepare the build files, and compile the project:
cmake ..
cmake --build .
After the build is complete, you can run the program using the command:
./executable
SBATCH Script
To run computationally intensive tasks, we use the SLURM job scheduler. You can submit your job to the queue using an sbatch script. The example below shows a .sh script that compiles the program with CMake and runs it.
#!/bin/bash
#SBATCH --job-name=cpp_cmake
#SBATCH --partition=cpu
#SBATCH --nodes=1
#SBATCH --ntasks=1
#SBATCH --cpus-per-task=1
#SBATCH --time=01:00:00
#SBATCH --output=cpp_cmake_%j.out
module purge
module load CMake
mkdir -p build
cd build
cmake ..
cmake --build . --parallel $SLURM_CPUS_PER_TASK
# Run the program
./executable
Submitting the job:
sbatch my_script.sh
Checking job status:
squeue --me
More information about submitting jobs with SLURM can be found here.