MATLAB on ALICE and SHARK
The availability of MATLAB on ALICE and SHARK differs because MATLAB is a paid-for licensed software
ALICE
Leiden University has a campus license for MATLAB and the software is available on ALICE as a module.
You can find a list of available versions with
module avail MATLAB
but we recommend that you use the most recent one. Older versions might not work anymore.
module load MATLAB/2022b
SHARK
MATLAB on SHARK is not available for all SHARK users. Please check that your group is covered by the license. Check with the SHARK team.
module avail MATLAB
Choose a module and add it to your environment by loading it, e.g.,:
moduel load statistical/MATLAB/R2021a
Preparations
It is always a good idea to start by looking at the load of the cluster when you want to submit a job. Also, it helps to run some short, resource-friendly tests to see if your set up is working and you have a correct batch file.
The “testing”-partition on ALICE or the “short” partition on SHARK can be used for such purpose. The examples in this tutorial are save to use on those partitions.
Here, we will assume that you have already created a directory called user_guide_tutorials
in your $HOME
from the previous tutorials. For this job, let's create a sub-directory and change into it:
mkdir -p $HOME/user_guide_tutorials/first_matlab_job cd $HOME/user_guide_tutorials/first_matlab_job
A simple MATLAB job
In this example, we will create a very basic MATLAB script that will just print out some information and then run it through a slurm batch job
Preparations
The MATLAB script
We will use the following Python script for this example and save it as test_matlab_simple.py
.
% Example for a simple MATLAB script fprintf('MATLAB script started\n'); % getting the number of cores set for the job cpus = str2num(getenv("SLURM_CPUS_PER_TASK")); fprintf('Number of CPUS from Slurm job: %g\n', cpus); % Just saying hello here fprintf('Hello World from MATLAB\n'); fprintf('MATLAB script finished\n');
For demonstration purposes, the script shows how to read the number of cores set for the Slurm job. The fprintf statements will write everything out to the Slurm output file.
The Slurm batch file
The next step is to create the corresponding Slurm batch file which we will name test_matlab_simple.slurm
. We will make use of the testing partition on ALICE or the short partition on SHARK. Make sure to change the partition and resources requirements for your production jobs. The running time and amount of memory have already been set in a way that fits to the resources that this job needs. If you do not know this, it is best to use a conservative estimate at first and then reduce the resource requirements.
ALICE
#!/bin/bash #SBATCH --job-name=test_matlab_simple #SBATCH --output=%x_%j.out #SBATCH --mail-user="<your_email_address>" #SBATCH --mail-type="ALL" #SBATCH --mem=1G #SBATCH --time=00:05:00 #SBATCH --partition=testing #SBATCH --ntasks=1 #SBATCH --cpus-per-task=1 # load modules (assuming you start from the default environment) # we explicitly call the modules to improve reproducibility # in case the default settings change module load MATLAB/2022b echo "[$SHELL] #### Starting MATLAB test" echo "[$SHELL] ## This is $SLURM_JOB_USER on $HOSTNAME and this job has the ID $SLURM_JOB_ID" # get the current working directory export CWD=$(pwd) echo "[$SHELL] ## current working directory: "$CWD # Run the file echo "[$SHELL] ## Run MATLAB script" # there different ways to start a matlab script # here we use -batch # Just the name of the script without ".m" matlab -batch test_matlab_simple echo "[$SHELL] ## Script finished" echo "[$SHELL] #### Finished Python test. Have a nice day"
SHARK
#!/bin/bash #SBATCH --job-name=test_matlab_simple #SBATCH --output=%x_%j.out #SBATCH --mail-user="<your_email_address>" #SBATCH --mail-type="ALL" #SBATCH --mem=1G #SBATCH --time=00:05:00 #SBATCH --partition=short #SBATCH --ntasks=1 #SBATCH --cpus-per-task=1 # load modules (assuming you start from the default environment) # we explicitly call the modules to improve reproducibility # in case the default settings change module load statistical/MATLAB/R2021a echo "[$SHELL] #### Starting MATLAB test" echo "[$SHELL] ## This is $SLURM_JOB_USER on $HOSTNAME and this job has the ID $SLURM_JOB_ID" # get the current working directory export CWD=$(pwd) echo "[$SHELL] ## current working directory: "$CWD # Run the file echo "[$SHELL] ## Run MATLAB script" # there different ways to start a matlab script # here we use -batch # Just the name of the script without ".m" matlab -batch test_matlab_simple echo "[$SHELL] ## Script finished" echo "[$SHELL] #### Finished Python test. Have a nice day"
where you should replace <your_email_address>
by an actual e-mail address of yours.
The batch file will also print out some information to the Slurm output file. To separate the output from what the Python script will produce, we use [$SHELL]
here.
While there are different ways to run MATLAB script non-interactively, here we have used the option -batch
. It automatically prevents MATLAB from trying to start the GUI. It also does not add the splash screen output and returns a proper exit code for the MATLAB script.
Job submission
Let us submit this Python job to slurm:
sbatch test_matlab_simple.slurm
Immediately after you have submitted this job, you should see something like this:
[me@<node_name> first_matlab_job]$ sbatch test_matlab_simple.slurm Submitted batch job <job_id>
Job output
In the directory where you launched your job, there should be new file created by Slurm: test_Python_simple_<jobid>.out
. It contains all the output from your job which would have normally written to the command line. Check the file for any possible error messages. The content of the file should look something like this:
[/bin/bash] #### Starting MATLAB test [/bin/bash] ## This is schulzrf on nodelogin01 and this job has the ID 1970891 [/bin/bash] ## current working directory: /home/<username>/user-guide-tutorials/first_matlab_job [/bin/bash] ## Run MATLAB script MATLAB script started Number of CPUS from Slurm job: 1 Hello World from MATLAB MATLAB script finished [/bin/bash] ## Script finished [/bin/bash] #### Finished MATLAB test. Have a nice day
The running time might differ when you run it.
You can get a quick overview of the resources actually used by your job by running:
seff <job_id>
The output from seff
will probably look something like this:
Job ID: <jobid> Cluster: <cluster_name> User/Group: <user_name>/<group_name> State: COMPLETED (exit code 0) Cores: 1 CPU Utilized: 00:00:11 CPU Efficiency: 84.62% of 00:00:13 core-walltime Job Wall-clock time: 00:00:13 Memory Utilized: 1.30 MB Memory Efficiency: 0.13% of 1.00 GB