Skip to content

Matlab

In this example we will show how to run matlab on our clusters.

Matlab code

First of all we to save this matlab script to be saved as parallel_64cores.m .

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
% Parallel computation using 64 cores

% Start the parallel pool with 64 workers
parpool('local', 64); %(1)!

% Define a simple task to distribute across the cores
n = 1e7;  % Number of iterations for demonstration
results = zeros(1, 64);  % Preallocate results for efficiency

disp('Starting parallel computation on 64 cores...');

% Use parfor to perform a parallel loop
parfor i = 1:64
    % Example computation: summing random numbers
    rng(i);  % Ensure different random seed per worker
    results(i) = sum(rand(1, n));  % Simulate work
end

disp('Parallel computation finished.');

% Shut down the parallel pool
delete(gcp('nocreate'));

% Display results (optional)
disp('Results:');
disp(results);
disp('');

  1. Start the parallel pool with 64 workers

submission script

Our submission script (subm.bash) will allocate the 64 cores on a single node and run matlab.

#!/bin/bash

#SBATCH --job-name="ML 1N" 
#(1)!
#SBATCH --output=%j.out
#SBATCH --error=%j.err 

#SBATCH --mail-user=bt123456@uni-bayreuth.de
#SBATCH --mail-type=ALL
#SBATCH --nodes=1 
#(2)!
#SBATCH --ntasks-per-node=1 
#SBATCH --cpus-per-task=64 
#(3)!
#SBATCH --time=00:05:00

# load env for mcr
module load matlab/R2024b #(4)!

#run your command
matlab -nosplash -nodesktop -nodisplay < parallel_64cores.m #(5)!

  1. Define input and output by jobid as separate files.
  2. As we use only shared memory we allocate 64 core for one task.
  3. Please don’t set to high times here. To many users allocating to long job will result in a bad cluster utilization because of long waiting time for remaining idle nodes. Please use checkpointing if possible
  4. Load our matlab module.
  5. Run the matlab script.

submission and evaluation

Next step is to submit our job. Maybe for a job this short we should use the dev-partition:

sbatch --parition=dev subm.bash

If the job is done the output file (<jobid>.out) should look like:

[bt123456@festus01 mltest]$ cat 7582.out 

                            < M A T L A B (R) >
                  Copyright 1984-2024 The MathWorks, Inc.
             R2024b Update 1 (24.2.0.2740171) 64-bit (glnxa64)
                             September 20, 2024


To get started, type doc.
For product information, visit www.mathworks.com.

>> >> >> >> Starting parallel pool (parpool) using the 'Processes' profile ...
Connected to parallel pool with 64 workers.
>> >> >> >> >> >> Starting parallel computation on 128 cores...
>> >> >> >>>>>>>>>> >> Parallel computation finished.
>> >> >> Parallel pool using the 'Processes' profile is shutting down.
>> >> >> Results:
>>    1.0e+06 *

  Columns 1 through 7

    5.0019    4.9993    4.9994    5.0011    4.9998    5.0007    4.9992

  Columns 8 through 14

    4.9990    5.0006    4.9977    4.9992    5.0002    4.9992    4.9992

  Columns 15 through 21

    4.9991    5.0010    4.9993    4.9992    5.0003    5.0002    4.9986

  Columns 22 through 28

    4.9983    4.9996    4.9997    5.0004    4.9997    5.0004    5.0000

  Columns 29 through 35

    5.0000    5.0007    5.0000    5.0008    5.0009    4.9997    4.9998

  Columns 36 through 42

    5.0004    4.9995    5.0002    5.0011    4.9989    5.0010    5.0012

  Columns 43 through 49

    5.0007    4.9981    4.9994    5.0011    5.0012    4.9993    4.9984

  Columns 50 through 56

    4.9990    5.0011    5.0004    4.9998    5.0003    4.9985    4.9988

  Columns 57 through 63

    5.0004    5.0005    5.0008    4.9995    4.9988    4.9994    5.0004

  Column 64

    5.0018

>> >>