Running MATLAB Scripts Using SBATCH

When to Use?

This is information on how to submit a matlab job on SLURM. The file script.sh and the MATLAB file sample.m can be modified based on the user needs to initiate a new job submission.

Procedure

1. Create a new folder called matlab in your home directory.

mkdir matlab && cd matlab

2. Create the script file script.sh and the MATLAB file sample.m and then copy paste the codes in script.sh and sample.m tabs respectively.

nano script.sh

Copy the contents below into the text editor and save it. The resources requested are 1 CPU, 4 GB memory per CPU, 10 minutes of Walltime, and ntasks as 1. Next, load the matlab module and specify the srun command to run the job on the compute node

#!/bin/bash 

#SBATCH --output output-1.out 
#SBATCH --ntasks 1 
#SBATCH --cpus-per-task 1 
#SBATCH --mem-per-cpu 4G 
#SBATCH --time 00:10:00 
#SBATCH --partition cpu

#Load matlab module 
module load matlab/R2024a 

#Specify job steps to run 
srun matlab -batch "sample" 

echo "Job Completed Successfully"

Create the MATLAB file sample.m

nano sample.m

Copy the contents below into the text editor and save it

%Calculating total number of people 

NoOfStudents = 6000; 
TeachingStaff = 150; 
NonTeachingStaff = 20; 

Total = NoOfStudents + TeachingStaff + NonTeachingStaff; 
fprintf('The total people equals %d\n',Total); 

%To close the matlab after the above script is executed 
quit

 

3. Now, both the files are ready and submit the batch script with the following command:

sbatch script.sh

4. After the job has been submitted, you should get an output similar to the one below but with a different job id.

Submitted batch job 19074

5. You can use the command below to check the progress of your submitted job in the queue.

Syntax: squeue -u <username>

squeue -u hpcuser1

Output:

JOBID      PARTITION     NAME     USER    ST       TIME  NODES NODELIST(REASON)
19074       cpu       script.s  hpcuser1 R       0:02      1    c001

6. Once your job has completed and no longer in the queue, you can run the ls command to show the list of files in your working directory.

ls
output-1.out  sample.m  script.sh

Now you’d notice that there is a new file output-1.out and if you view its output with the cat command, you should see something similar to the output below.

cat output-1.out

          
Classroom License -- for classroom instructional use only.

 

 The total people equals 6170
 Job Completed Successfully

 

 

Was this helpful?
0 reviews