Module Matlab

Module Matlab

Interactive Job

Open an interactive session to a compute node. Adjust the number of CPUs (--cpus-per-task 1) and the amount of memory (--mem 4G) needed for your job. Increase the number of --cpus-per-task only when using code that can run in parallel.

srun --cpus-per-task 1 --mem 4G --pty bash

Load the Matlab module:

module load matlab/R2024a

Run Matlab:

matlab
[user@athena1 ~]$ srun --cpus-per-task 1 --mem 4G --pty bash [user@athena201 ~]$ module load matlab/R2024a [user@athena201 ~]$ matlab MATLAB is selecting SOFTWARE OPENGL rendering. < M A T L A B (R) > Copyright 1984-2023 The MathWorks, Inc. R2024a Update 2 To get started, type doc. For product information, visit www.mathworks.com. >>

Batch Job

Create the Matlab code script file:

Hello.m
disp('Hello, World!');

Create the Slurm job script file.  Adjust the number of CPUs (--cpus-per-task 1) and the amount of memory (--mem 4G) needed for your job. Increase the number of --cpus-per-task only when using code that can run in parallel.

Matlab_slurm_script.sh
#!/bin/bash #SBATCH --job-name Matlab_job #SBATCH --output Matlab_job_output.log #SBATCH --error Matlab_job_error.log #SBATCH --cpus-per-task=1 #SBATCH --mem 4G module load matlab/R2024a matlab -nodisplay -nojvm < hello_world.m

Submit the job to Slurm:

sbatch Matlab_slurm_script.sh

Monitor the execution of your job in the Slurm queue:

squeue -u $USER

The job may show up in the queue as running (R) or pending (PD). When the job completes, it will disappear from the queue and you can check the output.

[user@athena1 ~]$ sbatch Matlab_slurm_script.sh Submitted batch job 371 [user@athena1 ~]$ squeue -u $USER JOBID PARTITION NAME USER ST TIME NODES NODELIST(REASON) ## Job is running with JOBID 371 371 cpu Matlab_j user R 0:02 1 athena201 [user@athena1 ~]$ squeue -u $USER JOBID PARTITION NAME USER ST TIME NODES NODELIST(REASON) ## JOBID 371 completed and exited the queue [user@athena1 ~]$ cat Matlab_job_output.log < M A T L A B (R) > Copyright 1984-2024 The MathWorks, Inc. R2024a Update 2 For online documentation, see https://www.mathworks.com/support For product information, visit www.mathworks.com. >> Hello, World! >>

Multithreaded job

Create the Matlab code script file:

Multithreaded_hello.m
user = getenv('USER'); node = getenv('HOSTNAME'); parpool("threads"); % measure elapsed time to compare to a serial version tic % 10 iterations using 4 worker processes (threads) parfor(i = 1:10, 4) fprintf("%2d) Hello %s, on node %s...\n", i, user, node); end toc

Create the Slurm job script file.  Adjust the number of CPUs (--cpus-per-task 4) and the amount of memory (--mem 16G) needed for your job. Increase the number of --cpus-per-task only when using code that can run in parallel.

Matlab_multithreaded_slurm_script.sh
#!/bin/bash #SBATCH --job-name Matlab_job #SBATCH --output Matlab_job.log #SBATCH --error Matlab_job.log #SBATCH --cpus-per-task=4 #SBATCH --mem 16G module load matlab/R2024a matlab -nodisplay -nojvm < Multithreaded_hello.m

Submit the job to Slurm:

sbatch Matlab_multithreaded_slurm_script.sh

Monitor the execution of your job in the Slurm queue:

squeue -u $USER

The job may show up in the queue as running (R) or pending (PD). When the job completes, it will disappear from the queue and you can check the output.

[user@athena1 ~]$ sbatch run_hello_mt.sh Submitted batch job 381 [user@athena1 ~]$ squeue -u $USER JOBID PARTITION NAME USER ST TIME NODES NODELIST(REASON) ## Job is running with JOBID 381 381 cpu Matlab_j user R 0:12 1 athena201 [user@athena1 ~]$ squeue -u $USER JOBID PARTITION NAME USER ST TIME NODES NODELIST(REASON) ## JOBID 381 completed and exited the queue [user@athena1 ~]$ cat Matlab_job.log < M A T L A B (R) > Copyright 1984-2024 The MathWorks, Inc. R2024a Update 2 For online documentation, see https://www.mathworks.com/support For product information, visit www.mathworks.com. >> >> >> Starting parallel pool (parpool) using the 'threads' profile ... Connected to parallel pool with 4 workers. >> >> >> >> >>>> 1) Hello user, on node athena201... 2) Hello user, on node athena201... 3) Hello user, on node athena201... 4) Hello user, on node athena201... 5) Hello user, on node athena201... 6) Hello user, on node athena201... 7) Hello user, on node athena201... 8) Hello user, on node athena201... 9) Hello user, on node athena201... 10) Hello user, on node athena201... >> Elapsed time is 1.901378 seconds. >> Parallel pool using the 'threads' profile is shutting down.

Matlab via Open OnDemand

Matlab can be run on the cluster using Open OnDemand. Go to https://athena3.hprc.vcu.edu and select the Matlab app.

Configure the requirements for your job (CPUs and memory). Increase the number of CPUs only when using code that can run in parallel. Select the GPU partition and a number of GPUs only when using code that can run on the GPU.

The job will be queued waiting for resources.

When the job starts running you will be able to connect to Matlab.

Matlab will open on a new tab.

When you finish working with Matlab please close the session by clicking on the red "Delete" button on the Open OnDemand tab.