You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 8 Next »

Page Content

MATLAB Modules

On Linux Cluster, you may list all available MATLAB versions:

> module avail matlab

MATLAB can be used by loading its module. We highly recommend to choose a particular – better: the newest – version, e. g.:

> module load matlab/R2019b-generic

Supported Releases of MATLAB and MATLAB Compiler Runtime

MATLAB Release (Patch level)

MATLAB Compiler Runtime

Comment
R2019b
> module load matlab/R2019b-generic
> module load matlab-mcr/R2019b-generic
R2019brecommended version
R2019a (Update 5)
> module load matlab/R2019a_Update5-generic
> module load matlab-mcr/R2019a-generic
R2019a


Trouble Loading MATLAB Module

  • The MATLAB module is not visible and, thus, cannot be loaded. Try to get access to the current software stack via following command. Then continue using "module avail" or "module load".

    > module use -p /lrz/sys/spack/staging/20.1.1/modules/x86_64/linux-sles15-x86_64
  • Access to the module is still not possible. Try to load an old software stack:

    > module use -p /lrz/sys/spack/release/19.2/modules/x86_avx2/linux-sles12-x86_64

    This is a fallback solution! Old software stacks are not supported anymore! Some MATLAB features might not work because according configurations refer to the CoolMUC-2 system used before March 2020!

Useful MATLAB Commands

Please consider: All MATLAB commandline arguments are case-sensitive!

  • Start MATLAB GUI:

    > matlab
  • Start MATLAB without desktop but allow GUI and graphics output:

    > matlab -nodesktop
  • Start MATLAB without any GUI support:

    > matlab -nodisplay
  • Some intrinsic MATLAB functions automatically exploit multithreading. MATLAB can be forced to disable this feature, which is important on login nodes:

    > matlab -singleCompThread
  • Run a MATLAB script or function, e. g. myfunc.m:

    > matlab -r myfunc

Interactive MATLAB Jobs in a Nutshell – the Convenient Way

Depending on the purpose, there are different possibilities to use MATLAB interactively:

  • MATLAB computations with focus on visualisation: We recommend to use our Remote Visualisation System.
  • Pure MATLAB computations: Interactive MATLAB sessions may be started on compute nodes of CoolMUC-2 or CoolMUC-3 by employing interactive Slurm jobs (see Slurm documentation).

Constraints

  • Interactive jobs depend on the availability of compute resources. Matlab may not start immediately.
  • Matlab will run on 1 compute node.
  • The time limit is set to 2 hours (= maximum time for interactive Slurm sessions).

How to Use it

  1. Load desired MATLAB module.

  2. For convenience, load the interactive-MATLAB module in order to use the loaded MATLAB version:

    > # use CoolMUC-2:
    > module load matlab-inter/coolmuc-2
    > 
    > # use CoolMUC-3:
    > module load matlab-inter/coolmuc-3
  3. Start interactive MATLAB session. This command will implicitly submit an interactive Slurm job. Probably you have to wait if the interactive partition is fully occupied.

    > matlab-inter
  4. Exiting MATLAB session will automatically finish the Slurm job.

Common Batch Jobs

Batch jobs are used for all MATLAB production runs. Usually, the resources, consumed by MATLAB applications, are limited to a single CPU core (pure serial job) or a single compute node (parallel job involving either multithreading or the Parallel Computing Toolbox [PCT]). Following table lists job examples for various cases which can be used on CoolMUC-2.


Slurm job scriptMatlab script

Serial batch job

No parallelization at all, MATLAB is intended to run on a single core.
Please use the "serial" cluster of the Linux Cluster!

matmul_serial.slurm
#!/bin/bash
#SBATCH -o ./out/matlab_job.%j.%N.out
#SBATCH -e ./out/matlab_job.%j.%N.err
#SBATCH -D ./
#SBATCH -J matlab_serial_batch_job
#SBATCH --get-user-env
#SBATCH --export=NONE
#SBATCH --clusters=serial
#SBATCH --partition=serial_std
#SBATCH --nodes=1
#SBATCH --tasks-per-node=1
#SBATCH --cpus-per-task=1
#SBATCH --mem=10000
#SBATCH --time=0:30:00
# As needed, remove/adjust memory requirement "--mem" in MB

module load slurm_setup
module load matlab/R2019b-generic

# Example: matrix-matrix multiplication C = A*B
#          with A of size NROWA x NCOLA and
#          B of size NROWB x NCOLB
NROWA=1000
NCOLA=2000
NROWB=2000
NCOLB=5000

# Run MATLAB
# => Using option -r don't add file extension .m to the function call!
# => MATLAB commandline arguments are case-sensitive!
matlab -nodisplay -singleCompThread \
       -r "matmul_serial([$NROWA $NCOLA], [$NROWB $NCOLB]);"
matmul_serial.m
function [C, comptime] = matmul_serial(size_A, size_B)

%===============================================================================
% MATLAB EXAMPLE: SERIAL HELLO WORLD
%                 -> matrix-matrix multiplication C = A*B
%
% INPUT
%   size_A, size_B ... 2-element row vectors defining sizes of A and B
% OUTPUT
%   C ................ result
%   comptime ......... computation time (matrix product only)
%===============================================================================

%===============================================================================
% Check input
%===============================================================================
if nargin~=2
    error('Invalid number of input arguments!');
end
if size_A(2)~=size_B(1)
    error(sprintf('Dimension mismatch of A (%d columns) and B (%d rows)!',...
                  size_A(2), size_B(1)));
end

%===============================================================================
% Work
%===============================================================================
% Hello message from compute node
fprintf('Hello from MATLAB process PID=%d running on node %s!\n',...
        feature('getpid'),...
        strtrim(evalc('system(''hostname'');')));

% generate well-defined matrices
NA = prod(size_A);
NB = prod(size_B);
A = reshape( linspace( 1,NA, NA), size_A );
B = reshape( linspace(NB, 1, NB), size_B );

% compute
tic;
C = A*B;
comptime = toc;
fprintf('serial computation of matrix-matrix product:\n');
fprintf('\ttime = %.2f s\n', comptime);

Parallel job using multithreading

MATLAB will run on a single compute node.
Please use the partition "cm2_tiny" in cluster "cm2_tiny"!

matmul_mthread.slurm
#!/bin/bash
#SBATCH -o ./out/matlab_job.%j.%N.out
#SBATCH -e ./out/matlab_job.%j.%N.err
#SBATCH -D ./
#SBATCH -J matlab_threading_batch_job
#SBATCH --get-user-env
#SBATCH --export=NONE
#SBATCH --clusters=cm2_tiny
#SBATCH --partition=cm2_tiny
#SBATCH --nodes=1
#SBATCH --tasks-per-node=1
#SBATCH --cpus-per-task=14
#SBATCH --time=00:30:00

module load slurm_setup
module load matlab/R2019b-generic

# Example: matrix-matrix multiplication C = A*B
#          with A of size NROWA x NCOLA and
#          B of size NROWB x NCOLB
NROWA=1000
NCOLA=2000
NROWB=2000
NCOLB=5000

# Run MATLAB
# => Using option -r don't add file extension .m to the function call!
# => MATLAB commandline arguments are case-sensitive!
matlab -nodisplay \
       -r "matmul_mthread([$NROWA $NCOLA], [$NROWB $NCOLB]);"
matmul_mthread.m
function [C, comptime] = parallel_mthread(size_A, size_B)

%===============================================================================
% MATLAB EXAMPLE: PARALLEL HELLO WORLD USING MULTITHREADING
%                 -> matrix-matrix multiplication C = A*B
%
% INPUT
%   size_A, size_B ... 2-element row vectors defining sizes of A and B
% OUTPUT
%   C ................ result
%   comptime ......... computation time (matrix product only)
%===============================================================================

%===============================================================================
% Check input
%===============================================================================
if nargin~=2
    error('Invalid number of input arguments!');
end
if size_A(2)~=size_B(1)
    error(sprintf('Dimension mismatch of A (%d columns) and B (%d rows)!',...
                  size_A(2), size_B(1)));
end

%===============================================================================
% Manage multithreading
%===============================================================================
% Get number of threads depending on job type (batch job or interactive job).
% In batch jobs 1 MATLAB task will use "nw" threads. 
%
% obtain number of threads from Slurm environment variables
cluster = getenv('SLURM_CLUSTER_NAME');
if strcmp(cluster, 'inter')
    % interactive job
    nw = str2num(getenv('SLURM_JOB_CPUS_PER_NODE'));
elseif strcmp(cluster, 'cm2') || ...
       strcmp(cluster, 'mpp3') || ...
       strcmp(cluster, 'ivymuc')
    % batch job
    nw = str2num(getenv('SLURM_CPUS_PER_TASK'));
else
    % default
    nw = 1;
end
% set threads
maxNumCompThreads(nw);

%===============================================================================
% Work
%===============================================================================
fprintf('Hello from MATLAB process PID=%d running on node %s!\n',...
        feature('getpid'),...
        strtrim(evalc('system(''hostname'');')));

% generate well-defined matrices
NA = prod(size_A);
NB = prod(size_B);
A = reshape( linspace( 1,NA, NA), size_A );
B = reshape( linspace(NB, 1, NB), size_B );

% compute
tic;
C = A*B;
comptime = toc;
fprintf('parallel computation (multithreading) of matrix-matrix product:\n');
fprintf('\tnumber of threads = %d\n', nw);
fprintf('\ttime = %.2f s\n', comptime);

Parallel batch job using Parallel Computing Toolbox (PCT)

MATLAB will run on a single compute node.
Please use the partition "cm2_tiny" in cluster "cm2_tiny"!

matmul_pct.slurm
#!/bin/bash
#SBATCH -o ./out/matlab_job.%j.%N.out
#SBATCH -e ./out/matlab_job.%j.%N.err
#SBATCH -D ./
#SBATCH -J matlab_pct_batch_job
#SBATCH --get-user-env
#SBATCH --export=NONE
#SBATCH --clusters=cm2_tiny
#SBATCH --partition=cm2_tiny
#SBATCH --nodes=1
#SBATCH --tasks-per-node=4
#SBATCH --cpus-per-task=1
#SBATCH --time=00:30:00

# IMPORTANT
# Default settings of Intel MPI module may disrupt 
# functionality of Parallel-Computing-Toolbox!
# Do one of the following solutions:  
# (1) Unload module mpi.intel:
module rm mpi.intel
# (2) If Intel MPI module is mandatory, add next line
# export KMP_AFFINITY=granularity=thread,none

module load slurm_setup
module load matlab/R2019b-generic

# Example: matrix-matrix multiplication C = A*B
#          with A of size NROWA x NCOLA and
#          B of size NROWB x NCOLB
NROWA=1000
NCOLA=2000
NROWB=2000
NCOLB=5000

# Run MATLAB
# => Using option -r don't add file extension .m to the function call!
# => MATLAB commandline arguments are case-sensitive!
matlab -nodisplay -singleCompThread \
       -r "matmul_pct([$NROWA $NCOLA], [$NROWB $NCOLB]);"

matmul_pct.m
function [Cglob, comptime] = matmul_pct(size_A, size_B)

%===============================================================================
% MATLAB EXAMPLE: PARALLEL HELLO WORLD USING PCT TOOLBOX
%                 -> matrix-matrix multiplication C = A*B
%
% INPUT
%   size_A, size_B ... 2-element row vectors defining sizes of A and B
% OUTPUT
%   C ................ result
%   comptime ......... computation time (matrix product only)
%===============================================================================

%===============================================================================
% Check input
%===============================================================================
if nargin~=2
    error('Invalid number of input arguments!');
end
if size_A(2)~=size_B(1)
    error(sprintf('Dimension mismatch of A (%d columns) and B (%d rows)!',...
                  size_A(2), size_B(1)));
end

%===============================================================================
% Verify that no parallel pool is initialized by creating/deleting a dummy pool
%===============================================================================
if ~isempty(gcp('nocreate'))
    poolobj = gcp('nocreate');
    delete(poolobj);
end

%===============================================================================
% Start parallel pool
%===============================================================================
% Get number of workers depending on job type. Start parallel pool via "local"
% cluster object.
%
% obtain number of tasks from Slurm environment variables
cluster = getenv('SLURM_CLUSTER_NAME');
if strcmp(cluster, 'inter')
    % interactive job
    nw = str2num(getenv('SLURM_JOB_CPUS_PER_NODE'));
elseif strcmp(cluster, 'cm2') || ...
       strcmp(cluster, 'serial') || ...
       strcmp(cluster, 'mpp3') || ...
       strcmp(cluster, 'ivymuc')
    % batch job
    nw = str2num(getenv('SLURM_NTASKS'));
else
    % default
    nw = 1;
end

% disallow Threading
if maxNumCompThreads > 1
    maxNumCompThreads(1);
    warning('MultiThreading: number of threads has been set to 1!');
end

% create a local cluster object
pc = parcluster('local');
% set number of workers
pc.NumWorkers = nw;
% set the JobStorageLocation to SCRATCH (default: HOME -> not recommended)
pc.JobStorageLocation = strcat(getenv('SCRATCH'));
% start the parallel pool
poolobj = parpool(pc, nw);

%===============================================================================
% Work
%===============================================================================
spmd
    fprintf('Hello from MATLAB process PID=%d running on node %s!\n',...
            feature('getpid'),...
            getenv('HOSTNAME'));
end

% generate well-defined matrices
NA = prod(size_A);
NB = prod(size_B);
A = reshape( linspace( 1,NA, NA), size_A );
B = reshape( linspace(NB, 1, NB), size_B );

% distribute data to workers and do parallel computation
spmd
    Aloc = codistributed(A, codistributor2dbc([nw 1]));
    Bloc = codistributed(B, codistributor2dbc([1 nw]));
    tic;
    Cloc = Aloc*Bloc;
    t = toc;
end

comptime = max(cell2mat(t(:)));
fprintf('parallel computation (MPI) of matrix-matrix product:\n');
fprintf('\tnumber of tasks (MATLAB workers) = %d\n', nw);
fprintf('\ttime = %.2f s\n', comptime);

Cglob = gather(Cloc);

%===============================================================================
% Close parallel pool
%===============================================================================
delete(poolobj);

  • No labels