5  Submit your job using the scheduler

Author

Gabriel Ortega-Solís

6 Note

This tutorial assumes that you successfully configured VSCode and have already created a Mamba environment on the HPC cluster. If VSCode is properly working, you can just open a terminal manually and click on the run button in every code cell, and the code will be sent to your currently active terminal. If VSCode is not configured yet, you can copy and paste the code snippets in this tutorial into a terminal. Probably you’ll need Ctrl+Shift+V to paste in the terminal.

Please remember to replace USERNAME with your actual username and skirit.metacentrum.cz with the appropriate login node for your cluster.

The variable $USER is automatically replaced with your actual username on the HPC cluster when you run the code. However, you can also replace it with your username everywhere in the code if you prefer.

7 Goal

Prepare a scheduler script with suitable resources and submit a reproducible batch job with qsub.

8 Connect to the login node through SSH

Open a terminal on your personal computer and connect to the HPC cluster’s login node using SSH. For example:

Replace USERNAME with your actual username and skirit.metacentrum.cz with the appropriate login node for your cluster.

9 Scheduler settings

In this section you can find the basic settings you should consider before launching your job. Modify them according to your requirements. There is a tradeoff to remember: if you request a lot of resources (e.g., many CPU cores, a lot of memory, or a long walltime), your job may take longer to start because it needs to wait for those resources to become available. On the other hand, if you request too few resources, your job may run out of memory or time and fail. So, it’s important to find a balance based on the expected needs of your job.

9.1 General rule-of-thumb strategy for requesting HPC resources

Run a small test job in your computer or in an interactive session on the cluster with a subset of the data (1-5% the whole dataset) and modest resources (1–4 cores, 8–16 GB RAM, 10–30 minutes walltime).

Measure:

  • Elapsed time.

  • Max memory usage.

  • CPU utilisation (are all cores busy or mostly idle?).

If runtime scales roughly linearly with the size of your dataset, increase the required time accordingly and add ~10–20% safety margin (e.g., if the test used 10 minutes for 5% of the data, you might need 4 hours for the full dataset).

For memory, take the maximum memory usage observed during the test, increase with input size if memory grows with data, then add ~20–50% safety margin if you’re unsure (e.g., if the test used 1 GB for 5% of the data, you might need 24 GB for the full dataset).

9.2 Settings to modify before launching your HPC job

Modify these settings according to the requirements of your job script. The quoted example values are just placeholders you should replace.

# Write a name for your job.
JOB_NAME="demo_job"

# Number of CPU cores requested for the job.
NCPUS=4

# Total memory requested for the job.
MEM="8gb"

# Maximum wall-clock runtime allowed for the job in hours:minutes:seconds.
WALLTIME="01:00:00"

# Name of the script that will contain the job commands.
SCRIPT_TO_RUN="run_job.sh"

# Name of the submission script passed to the HPC scheduler.
SCHEDULER_SCRIPT="submit_job.pbs"

# Name of the Mamba environment used by the job.
MAMBA_ENV="/storage/brno2/home/$USER/mamba/envs/geostats"

# Working directory where scripts are created and the job is launched.
WORK_DIR="/storage/brno2/home/$USER/HPC-VSCode-Mamba_SciSeminar"

10 Move to the work folder and verify everything is in place

cd $WORK_DIR
ls -lh

11 Write the bash script to run your job

This section assumes you already tested your script and know it works. Here we just write the script to a file, but you can also create it with a text editor and copy-paste the content. The important thing is that the script contains all the commands needed to run your job, including loading packages/libraries, accessing datasets, and saving outputs.

cat > "$SCRIPT_TO_RUN" <<EOF
#!/bin/bash

set -euo pipefail

module add mambaforge
mamba activate "$MAMBA_ENV"

###########################################################################
# Example of R commands
###########################################################################

Rscript - <<'RSCRIPT'

###########################################################################
# PASTE YOUR R CODE BELOW THIS LINE.
###########################################################################


library(terra)

r <- rast("COP90.tif")
v <- vect("South.gpkg")

cropped <- crop(r, v, mask = TRUE)

mean_val <- global(cropped, "mean", na.rm = TRUE)
print(mean_val)

writeRaster(cropped, "cropped.tif", overwrite = TRUE)

###########################################################################
# DO NOT MODIFY BELOW THIS LINE.
###########################################################################

RSCRIPT

EOF

12 Job submission

IMPORTANT: Do not modify the sections below unless you know what you are doing!! The following code cells should run as is because they take arguments from the settings section above. If you modify them, make sure to understand what they do and how they work together to submit your job to the HPC cluster.

12.1 Give execution permission to your job script

chmod +x "$SCRIPT_TO_RUN"

12.2 Write submission script

cat > "$SCHEDULER_SCRIPT" <<EOF
#!/bin/bash
#PBS -N $JOB_NAME
#PBS -l select=1:ncpus=$NCPUS:mem=$MEM
#PBS -l walltime=$WALLTIME
#PBS -j oe

cd "\$PBS_O_WORKDIR"
bash "$SCRIPT_TO_RUN"
EOF

chmod +x "$SCHEDULER_SCRIPT"

12.3 Submit job

‘qsub’ is the function that will pick your PBS submission script and send it to the HPC scheduler system. The scheduler will then manage the execution of your job based on the resources you requested and the current load on the cluster. After you run this command, you should see a message confirming that your job has been submitted, along with a job ID that you can use to track its progress.

qsub "$SCHEDULER_SCRIPT"

13 Optional: check job status

You can check the status of your submitted job using the qstat command followed by your username or job ID. This will show you whether your job is queued, running, or completed. For example:

qstat -u $USER

14 Quit the SSH session

After submitting your job, you can exit the SSH session on the login node. Your job will continue to run on the compute nodes even after you disconnect. You can reconnect later to check the status of your job or to retrieve the output files.

exit

15 Copy output files back to local

Once your job has completed, you can use scp to copy the output files from the HPC cluster back to your local machine. Remember to replace USERNAME with your actual username and skirit.metacentrum.cz with the appropriate login node for your cluster.

scp [email protected]:/storage/brno2/home/USERNAME/HPC-VSCode-Mamba_SciSeminar/cropped.tif Data/