====== Compiling C++ applications with the GCC Module ======
This guide explains how to load the GCC (GNU Compiler Collection) module, compile a standard C++ program safely on a compute node, and submit it as a production job on the UNITE cluster.
===== IMPORTANT: Compilation Policy =====
^ WARNING: Do not run compilation commands directly on the Login Node! ^
| The login node is strictly intended for managing files, checking job status, and editing text. Heavy processes like compilation can degrade performance for all cluster users. Always request an interactive compute node allocation using **`srun`** before compiling code. |
===== Available Modules =====
The cluster provides multiple versions of the GCC compiler. You can inspect the available modules using the environment modules tool.
To see all available GCC modules, execute:
$ module avail unite/compilers/gcc
===== Step-by-Step Guide =====
==== 1. Request an Interactive Node for Compilation ====
Before compiling, switch from the login node to an active compute node on the **unite** partition using `srun`:
$ srun --partition=unite --cpus-per-task=4 --time=00:30:00 --pty bash
Once the shell prompts change, you are safely logged onto a compute node.
==== 2. Load the GCC Module ====
Load your preferred version of the GCC toolchain. For example, to load version 14.3.0, run:
$ module load unite/compilers/gcc-14.3.0
To verify that the compiler is loaded correctly and check its version, run:
$ g++ --version
==== 3. Create a Sample C++ File ====
Create a simple C++ source file named `hello.cpp`.
#include
#include
#include
int main() {
std::cout << "Hello from the UNITE HPC Cluster!" << std::endl;
// Quick C++14 feature check (vectors and lambda reductions)
std::vector data = {1, 2, 3, 4, 5};
int sum = std::accumulate(data.begin(), data.end(), 0);
std::cout << "Data verification sum: " << sum << " (Expected: 15)" << std::endl;
return 0;
}
==== 4. Compilation ====
Compile the source code using `g++`. It is highly recommended to use optimization flags such as `-O3` for code targeting the cluster processors.
$ g++ -O3 -std=c++17 hello.cpp -o hello_executable
* **`-O3`**: Optimizes the binary for maximum performance execution.
* **`-std=c++17`**: Specifies the standard C++17 library compliance (adjust to `-std=c++20` or `-std=c++23` as needed).
* **`-o hello_executable`**: Specifies the name of the compiled binary output.
You can safely test your compilation right here on this interactive node session by running:
$ ./hello_executable
Once finished, type `exit` to leave the interactive node and return to the login node.
===== Running on the Cluster via Slurm Batch =====
To queue up non-interactive production runs, create a submission script file named `submit_gcc.sh`:
#!/bin/bash
#SBATCH --job-name=gcc_cpp