Compiling C/C++
Assuming you already have access to the cluster, you can follow these steps to access your desired compiler, move your file to the HPC, schedule your job and run your code.
Load a Compiler Module
In order to load a module, you can use the 'module load' command followed by the name of the specific compiler you'd like to use. For example, in order to load the default GCC compiler you can use the following command:
To compile, we first need to load our compiler as a module:
module load gcc
Then, simply run
gcc main.c
We get an output file called a.out, which we can run like this:
./a.out
Or, we can name it something, like “hello”:
gcc -o hello main.c
./hello
To compile multiple files, we can run:
gcc file1.c file2.c file3.c
To make compilation easier, we can write a makefile, which is a script that simplifies building for various targets. Makefiles are always named “Makefile”, and they look like this:
foo:
gcc -o foo main1.c main2.c
bar:
gcc -o bar main3.c main4.c
all: foo bar
clean:
rm -f foo bar
Here, our targets are *foo* and *bar*, which we can simply compile like this:
make foo
make bar
If we want to build both at the same time:
make all
By default, make builds all targets, so
make
would build both foo and bar.
To clean up the binaries in the project directory, we can run
make clean
Targets can be combined, like
make clean all
to clean first, then rebuild all targets.
In a SLURM job:
You can run your binary just like any other program on the WAVE HPC using SLURM. Let’s use batch to submit a SLURM job:
First, let’s assume we have a project directory called “project” in the home directory of your WAVE user:
/WAVE/users2/unix/<user>/project
Assuming you have source code on your local machine, let’s compile it first:
make myprogram
Then, we can simply copy over the binary to the cluster:
scp myprogram <user>@login.wave.scu.edu:~/project
Then, we can ssh into the login node, and navigate to the project directory:
ssh <user>@login.wave.scu.edu
cd ~/project
Now, we can edit our batch script:
vim job.sh
Here’s what it might look like:
#!/bin/bash
#
#SBATCH --job-name=myprogram
#SBATCH --output=myprogram-%j.out
#
#SBATCH --partition=cmp
#SBATCH --nodes=1
#SBATCH --ntasks=1
#SBATCH --cpus-per-task=6
#SBATCH --mem-per-cpu=4G
#SBATCH --time=02:00:00
#
#SBATCH --mail-user=<user>@scu.edu
#SBATCH --mail-type=END
# run the binary:
./myprogram
For further reference on writing batch scripts for SLURM, navigate to the resource scheduling page: https://wiki.wave.scu.edu/doku.php?id=hpc_userguide:hpc_resource_scheduling
Then, we can queue up our job like usual:
sbatch job.sh