MATLAB: Difference between revisions

From Alliance Doc
Jump to navigation Jump to search
(Updated instructions for Matlab 2022a)
No edit summary
 
(27 intermediate revisions by 6 users not shown)
Line 6: Line 6:


<!--T:54-->
<!--T:54-->
'''1) Running MATLAB directly''', but that requires a license. You may either:
<b>1) Running MATLAB directly</b>, but that requires a license. You may either
* Run MATLAB on [[Béluga/en| Béluga]], [[Cedar]] or [[Narval/en| Narval]], all of which have a license available for any student, professor or academic researcher.
* run MATLAB on [[Béluga/en| Béluga]], [[Cedar]] or [[Narval/en| Narval]], all of which have a license available for any student, professor or academic researcher;
* Use an external license, i.e. one owned by your institution, faculty, department, or lab. See ''[[#Using_an_external_license|Using an external license]]'' below.
* use an external license, i.e., one owned by your institution, faculty, department, or lab. See <i>[[#Using_an_external_license|Using an external license]]</i> below.


<!--T:55-->
<!--T:55-->
'''2) Compiling your MATLAB code''' by using the MATLAB Compiler <tt>mcc</tt> and by running the generated executable file on any cluster. You can use this executable without license considerations.
<b>2) Compiling your MATLAB code</b> by using the MATLAB Compiler <code>mcc</code> and by running the generated executable file on any cluster. You can use this executable without license considerations.


<!--T:56-->
<!--T:56-->
Line 21: Line 21:
<!--T:21-->
<!--T:21-->
<pre>
<pre>
[name@cluster ~]$ module load matlab/2018a
[name@cluster ~]$ module load matlab/2023b.2
[name@cluster ~]$ matlab -nodisplay -nojvm -batch "disp(license())"
[name@cluster ~]$ matlab -nojvm -nodisplay -batch license


<!--T:30-->
<!--T:30-->
Line 33: Line 33:


<!--T:39-->
<!--T:39-->
If you get the message ''This version is newer than the version of the license.dat file and/or network license manager on the server machine'', try an older version of MATLAB in the <code>module load</code> line.
If you get the message <i>This version is newer than the version of the license.dat file and/or network license manager on the server machine</i>, try an older version of MATLAB in the <code>module load</code> line.


<!--T:40-->
<!--T:40-->
Line 44: Line 44:
|lang="bash"
|lang="bash"
|contents=
|contents=
# MATLAB license server file
# MATLAB license server specifications
SERVER <ip address> ANY <port>
SERVER <ip address> ANY <port>
USE_SERVER
USE_SERVER
}}
}}
Put this file in the <tt>$HOME/.licenses/</tt> directory where the IP address and port number correspond to the values for your campus license server. Next you will need to ensure that the license server on your campus is reachable by our compute nodes. This will require our technical team to get in touch with the technical people managing your license software. Please write to [[Technical support | technical support]] so that we can arrange this for you.
Put this file in the <code>$HOME/.licenses/</code> directory where the IP address and port number correspond to the values for your campus license server. Next you will need to ensure that the license server on your campus is reachable by our compute nodes. This will require our technical team to get in touch with the technical people managing your license software. Please write to [[Technical support | technical support]] so that we can arrange this for you.


<!--T:29-->
<!--T:29-->
Line 54: Line 54:
For product information, visit http://www.mathworks.com.
For product information, visit http://www.mathworks.com.


= Preparing your <tt>.matlab</tt> folder = <!--T:31-->
= Preparing your <code>.matlab</code> folder = <!--T:31-->
Because the /home directory is accessible in read-only mode on some clusters' compute nodes, users should create a <tt>.matlab</tt> symbolic link that makes sure that the MATLAB profile and job data will be written to the /scratch space instead.
Because the /home directory is accessible in read-only mode on some compute nodes, you need to create a <code>.matlab</code> symbolic link that makes sure that the MATLAB profile and job data will be written to the /scratch space instead.


<!--T:32-->
<!--T:32-->
Line 67: Line 67:
</pre>
</pre>


= Available Toolboxes = <!--T:23-->
= Available toolboxes = <!--T:23-->
To see a list of the MATLAB toolboxes available with the license and cluster you're using, you can use the following command:
To see a list of the MATLAB toolboxes available with the license and cluster you're using, you can use the following command:
<pre>
<pre>
[name@cluster ~]$  module load matlab
[name@cluster ~]$  module load matlab
[name@cluster ~]$  matlab -nodisplay -nojvm -batch "ver"
[name@cluster ~]$  matlab -nojvm -batch "ver"
</pre>  
</pre>
= Running a serial or parallel MATLAB code =
 
= Running a serial or parallel MATLAB code = <!--T:57-->


<!--T:4-->
<!--T:4-->
'''Important:''' Any significant MATLAB calculation (takes more than about 5 minutes or a gigabyte of memory) must be submitted to the scheduler. For instructions on using the scheduler, please see the [[Running jobs]] page.
<b>Important:</b> Any significant MATLAB calculation (takes more than about 5 minutes or a gigabyte of memory) must be submitted to the scheduler. For instructions on using the scheduler, please see the [[Running jobs]] page.


<!--T:27-->
<!--T:27-->
Line 129: Line 130:
<!--T:16-->
<!--T:16-->
# Choose a version of MATLAB by loading a module:
# Choose a version of MATLAB by loading a module:
module load matlab/2018a
module load matlab/2023b.2
# Remove -singleCompThread below if you are using parallel commands:
# Remove -singleCompThread below if you are using parallel commands:
matlab -nodisplay -singleCompThread -r "cosplot"
matlab -singleCompThread -batch "cosplot"
}}
}}


<!--T:17-->
<!--T:17-->
Submit the job using <tt>sbatch</tt>:
Submit the job using <code>sbatch</code>:
{{Command|sbatch matlab_slurm.sl}}
{{Command|sbatch matlab_slurm.sl}}


Line 183: Line 184:
<!--T:38-->
<!--T:38-->
References:
References:
* FAS Research Computing, [https://www.rc.fas.harvard.edu/resources/documentation/software/matlab-pct-simultaneous-job-problem/ MATLAB Parallel Computing Toolbox simultaneous job problem]
* FAS Research Computing, [https://www.rc.fas.harvard.edu/resources/documentation/software/matlab-pct-simultaneous-job-problem/ <i>MATLAB Parallel Computing Toolbox simultaneous job problem</i>].
* MathWorks, [https://www.mathworks.com/matlabcentral/answers/97141-why-am-i-unable-to-start-a-local-matlabpool-from-multiple-matlab-sessions-that-use-a-shared-preferen ... from multiple MATLAB sessions that use a shared preference directory]
* MathWorks, [https://www.mathworks.com/matlabcentral/answers/97141-why-am-i-unable-to-start-a-local-matlabpool-from-multiple-matlab-sessions-that-use-a-shared-preferen <i>Why am I unable to start a local MATLABPOOL from multiple MATLAB sessions that use a shared preference directory using Parallel Computing Toolbox 4.0 (R2008b)?</i>]


= Using the Compiler and Runtime libraries = <!--T:26-->  
= Using the Compiler and Runtime libraries = <!--T:26-->  


<!--T:13-->
<!--T:13-->
'''Important:''' Like any other intensive job, you must always run MCR code within a job submitted to the scheduler. For instructions on using the scheduler, please see the [[Running jobs]] page.
<b>Important:</b> Like any other intensive job, you must always run MCR code within a job submitted to the scheduler. For instructions on using the scheduler, please see the [[Running jobs]] page.


<!--T:5-->
<!--T:5-->
Line 199: Line 200:


<!--T:10-->
<!--T:10-->
This will produce a binary named <tt>cosplot</tt>, as well as a wrapper script. To run the binary on our servers, you will only need the binary. The wrapper script named <tt>run_cosplot.sh</tt> will not work as is on our servers because MATLAB assumes that some libraries can be found in specific locations. Instead, we provide a different wrapper script called <tt>run_mcr_binary.sh</tt> which sets the correct paths.  
This will produce a binary named <code>cosplot</code>, as well as a wrapper script. To run the binary on our servers, you will only need the binary. The wrapper script named <code>run_cosplot.sh</code> will not work as is on our servers because MATLAB assumes that some libraries can be found in specific locations. Instead, we provide a different wrapper script called <code>run_mcr_binary.sh</code> which sets the correct paths.  


<!--T:18-->
<!--T:18-->
Line 210: Line 211:


<!--T:28-->
<!--T:28-->
then, in your submission script ('''not on the login nodes'''), use your binary as so:
then, in your submission script (<b>not on the login nodes</b>), use your binary as so:
<code>run_mcr_binary.sh cosplot</code>
<code>run_mcr_binary.sh cosplot</code>


<!--T:12-->
<!--T:12-->
You will only need to run the <tt>setrpaths.sh</tt> command once for each compiled binary. The <tt>run_mcr_binary.sh</tt> will instruct you to run it if it detects that it has not been done.
You will only need to run the <code>setrpaths.sh</code> command once for each compiled binary. The <code>run_mcr_binary.sh</code> will instruct you to run it if it detects that it has not been done.


= Using the MATLAB Parallel Server = <!--T:41-->
= Using the MATLAB Parallel Server = <!--T:41-->
MATLAB Distributed Computing Server (MDCS) became MATLAB Parallel Server in the recent years. This technology is only worthwhile if you need more workers in your parallel MATLAB job than available CPU cores on a single compute node. While the regular MATLAB installation (see above sections) allows you to run parallel jobs within one node (up to 64 workers per job, depending on which node and cluster), the MATLAB Parallel Server is the licensed MathWorks solution for running a parallel job on more than one node. For the moment, the use of the MATLAB Parallel Server is only supported on the [[Béluga/en | Béluga]] and [[Narval/en | Narval]] clusters.
MATLAB Parallel Server is only worthwhile <b>if you need more workers in your parallel MATLAB job than available CPU cores on a single compute node</b>. While a regular MATLAB installation (see above sections) allows you to run parallel jobs within one node (up to 64 workers per job, depending on which node and cluster), the MATLAB Parallel Server is the licensed MathWorks solution for running a parallel job on more than one node.


<!--T:42-->
<!--T:42-->
This solution allows the submission of MATLAB parallel jobs from a local MATLAB interface on your computer. Some configuration is required in order to submit jobs remotely to the Slurm scheduler. Please follow instructions in the sections below.
This solution usually works by submitting MATLAB parallel jobs from a local MATLAB interface on your computer. <b>Since May 2023, some mandatory security improvements have been implemented on all clusters. Because MATLAB uses an SSH mode that is no longer permitted, job submission from a local computer is no longer possible until MATLAB uses a new connection method. There is currently no workaround.</b>


== Slurm plugin for MATLAB == <!--T:43-->
== Slurm plugin for MATLAB == <!--T:43-->
On your computer:
<b>The procedure below no longer works because the Slurm plugin is no longer available and because of the SSH issue described above.</b> The configuration steps are kept until a workaround is found:
# Have MATLAB R2022a or newer installed, '''including the Parallel Computing Toolbox'''.
# Have MATLAB R2022a or newer installed, <b>including the Parallel Computing Toolbox</b>.
# Go to the [https://www.mathworks.com/matlabcentral/fileexchange/52807-parallel-computing-toolbox-plugin-for-matlab-parallel-server-with-slurm MathWorks Slurm Plugin page], '''download and run''' the <tt>*.mlpkginstall</tt> file. (i.e. click on the blue ''Download'' button on the right side, just above the ''Overview'' tab.)
# Go to the MathWorks Slurm Plugin page, <b>download and run</b> the <code>*.mlpkginstall</code> file. (i.e., click on the blue <i>Download</i> button on the right side, just above the <i>Overview</i> tab.)
# Enter your MathWorks credentials; if the configuration wizard does not start, run in MATLAB
# Enter your MathWorks credentials; if the configuration wizard does not start, run in MATLAB
#:<tt>parallel.cluster.generic.runProfileWizard()</tt>
#:<code>parallel.cluster.generic.runProfileWizard()</code>
# Give these responses to the configuration wizard:
# Give these responses to the configuration wizard:
#* Select '''Unix''' (which is usually the only choice)
#* Select <b>Unix</b> (which is usually the only choice)
#* Shared location: '''No'''
#* Shared location: <b>No</b>
#* Cluster host:
#* Cluster host:
#** For Béluga: '''beluga.computecanada.ca'''
#** For Béluga: <b>beluga.computecanada.ca</b>
#** For Narval: '''narval.computecanada.ca'''
#** For Narval: <b>narval.computecanada.ca</b>
#* Username (optional): Enter your Compute Canada username (the identity file can be set later, if needed)
#* Username (optional): Enter your Alliance username (the identity file can be set later if needed)
#* Remote job storage: '''/scratch'''
#* Remote job storage: <b>/scratch</b>
#** Keep ''Use unique subfolders'' checked
#** Keep <i>Use unique subfolders</i> checked
#* Maximum number of workers: '''960'''
#* Maximum number of workers: <b>960</b>
#* Matlab installation folder for workers: both local and remote versions must match:
#* Matlab installation folder for workers (both local and remote versions must match):
#** For local R2022a: '''/cvmfs/restricted.computecanada.ca/easybuild/software/2020/Core/matlab/2022a'''
#** For local R2022a: <b>/cvmfs/restricted.computecanada.ca/easybuild/software/2020/Core/matlab/2022a</b>
#* License type: '''Network license manager'''
#* License type: <b>Network license manager</b>
#* Profile Name: '''beluga''' or '''narval'''
#* Profile Name: <b>beluga</b> or <b>narval</b>
# Click on ''Create'' and ''Finish'' to finalize the profile.
# Click on <i>Create</i> and <i>Finish</i> to finalize the profile.


== Edit the plugin once installed == <!--T:44-->
== Edit the plugin once installed == <!--T:44-->
In MATLAB, go to the <tt>nonshared</tt> folder (i.e. run the following in the MATLAB terminal):
In MATLAB, go to the <code>nonshared</code> folder (i.e., run the following in the MATLAB terminal):
  cd(fullfile(matlabshared.supportpkg.getSupportPackageRoot, 'parallel', 'slurm', 'nonshared'))
  cd(fullfile(matlabshared.supportpkg.getSupportPackageRoot, 'parallel', 'slurm', 'nonshared'))


<!--T:49-->
<!--T:49-->
Then:
Then:
# Open the '''independentSubmitFcn.m''' file; around line #97 is the line <p> <tt>additionalSubmitArgs = sprintf('--ntasks=1 --cpus-per-task=%d', cluster.NumThreads);</tt> </p><p> Replace this line with</p><p> <tt>additionalSubmitArgs = ccSBATCH().getSubmitArgs();</tt></p>
# Open the <b>independentSubmitFcn.m</b> file; around line #117 is the line <p> <code>additionalSubmitArgs = sprintf('--ntasks=1 --cpus-per-task=%d', cluster.NumThreads);</code> </p><p> Replace this line with</p><p> <code>additionalSubmitArgs = ccSBATCH().getSubmitArgs();</code></p>
# Open the '''communicatingSubmitFcn.m''' file; around line #103 is the line <p> <tt>additionalSubmitArgs = sprintf('--ntasks=%d --cpus-per-task=%d', environmentProperties.NumberOfTasks, cluster.NumThreads);</tt> </p><p> Replace this line with</p><p> <tt>additionalSubmitArgs = ccSBATCH().getSubmitArgs();</tt></p>
# Open the <b>communicatingSubmitFcn.m</b> file; around line #126 is the line <p> <code>additionalSubmitArgs = sprintf('--ntasks=%d --cpus-per-task=%d', environmentProperties.NumberOfTasks, cluster.NumThreads);</code> </p><p> Replace this line with</p><p> <code>additionalSubmitArgs = ccSBATCH().getSubmitArgs();</code></p>
# Open the '''communicatingJobWrapper.sh''' file; around line #20 (after the copyright statement), add the following command and adjust the module version to your local Matlab version:</p><p><tt>module load matlab/2020a</tt></p>
# Open the <b>communicatingJobWrapper.sh</b> file; around line #20 (after the copyright statement), add the following command and adjust the module version to your local Matlab version:</p><p><code>module load matlab/2022a</code></p>


<!--T:50-->
<!--T:50-->
Restart MATLAB and go back to your home directory:
Restart MATLAB and go back to your home directory:
  cd(getenv('HOME'))
  cd(getenv('HOME')) # or cd(getenv('HOMEPATH')) on Windows


== Validation == <!--T:47-->
== Validation == <!--T:47-->
'''Do not''' use the built-in validation tool in the ''Cluster Profile Manager''. Instead, you should try the <tt>TestParfor</tt> example, along with a proper <tt>ccSBATCH.m</tt> script file:
<b>Do not</b> use the built-in validation tool in the <i>Cluster Profile Manager</i>. Instead, you should try the <code>TestParfor</code> example, along with a proper <code>ccSBATCH.m</code> script file:
# Download and extract code samples on GitHub at https://github.com/ComputeCanada/matlab-parallel-server-samples.
# Download and extract code samples on GitHub at https://github.com/ComputeCanada/matlab-parallel-server-samples.
# In MATLAB, go to the newly extracted <tt>TestParfor</tt> directory.
# In MATLAB, go to the newly extracted <code>TestParfor</code> directory.
# Follow instructions in https://github.com/ComputeCanada/matlab-parallel-server-samples/blob/master/README.md.
# Follow instructions in https://github.com/ComputeCanada/matlab-parallel-server-samples/blob/master/README.md.


<!--T:48-->
<!--T:48-->
Note: When the <tt>ccSBATCH.m</tt> is in your current working directory, you may try the ''Cluster Profile Manager'' validation tool, but only the first two tests will work. Other tests are not yet supported.
Note: When the <code>ccSBATCH.m</code> is in your current working directory, you may try the <i>Cluster Profile Manager</i> validation tool, but only the first two tests will work. Other tests are not yet supported.


= External resources = <!--T:51-->
= External resources = <!--T:51-->

Latest revision as of 19:01, 10 September 2024

Other languages:

There are two ways of using MATLAB on our clusters:

1) Running MATLAB directly, but that requires a license. You may either

  • run MATLAB on Béluga, Cedar or Narval, all of which have a license available for any student, professor or academic researcher;
  • use an external license, i.e., one owned by your institution, faculty, department, or lab. See Using an external license below.

2) Compiling your MATLAB code by using the MATLAB Compiler mcc and by running the generated executable file on any cluster. You can use this executable without license considerations.

More details about these approaches are provided below.

Using an external license

We are a hosting provider for MATLAB. This means that we have MATLAB installed on our clusters and can allow you to access an external license to run computations on our infrastructure. Arrangements have already been made with several Canadian institutions to make this automatic. To see if you already have access to a license, carry out the following test:

[name@cluster ~]$ module load matlab/2023b.2
[name@cluster ~]$ matlab -nojvm -nodisplay -batch license

987654
[name@cluster ~]$

If any license number is printed, you're okay. Be sure to run this test on each cluster on which you want to use MATLAB, since licenses may not be available everywhere.

If you get the message This version is newer than the version of the license.dat file and/or network license manager on the server machine, try an older version of MATLAB in the module load line.

Otherwise, either your institution does not have a MATLAB license, does not allow its use in this way, or no arrangements have yet been made. Find out who administers the MATLAB license at your institution (faculty, department) and contact them or your Mathworks account manager to know if you are allowed to use the license in this way.

If you are allowed, then some technical configuration will be required. Create a file similar to the following example:

File : matlab.lic

# MATLAB license server specifications
SERVER <ip address> ANY <port>
USE_SERVER


Put this file in the $HOME/.licenses/ directory where the IP address and port number correspond to the values for your campus license server. Next you will need to ensure that the license server on your campus is reachable by our compute nodes. This will require our technical team to get in touch with the technical people managing your license software. Please write to technical support so that we can arrange this for you.

For online documentation, see http://www.mathworks.com/support. For product information, visit http://www.mathworks.com.

Preparing your .matlab folder

Because the /home directory is accessible in read-only mode on some compute nodes, you need to create a .matlab symbolic link that makes sure that the MATLAB profile and job data will be written to the /scratch space instead.

[name@cluster ~]$ cd $HOME
[name@cluster ~]$ if [ -d ".matlab" ]; then
  mv .matlab scratch/
else
  mkdir -p scratch/.matlab
fi && ln -sn scratch/.matlab .matlab

Available toolboxes

To see a list of the MATLAB toolboxes available with the license and cluster you're using, you can use the following command:

[name@cluster ~]$  module load matlab
[name@cluster ~]$  matlab -nojvm -batch "ver"

Running a serial or parallel MATLAB code

Important: Any significant MATLAB calculation (takes more than about 5 minutes or a gigabyte of memory) must be submitted to the scheduler. For instructions on using the scheduler, please see the Running jobs page.

Consider the following example code:


File : cosplot.m

function cosplot()
% MATLAB file example to approximate a sawtooth
% with a truncated Fourier expansion.
nterms=5;
fourbypi=4.0/pi;
np=100;
y(1:np)=pi/2.0;
x(1:np)=linspace(-2.0*pi,2*pi,np);

for k=1:nterms
 twokm=2*k-1;
 y=y-fourbypi*cos(twokm*x)/twokm^2;
end

plot(x,y)
print -dpsc matlab_test_plot.ps
quit
end


Here is a simple Slurm script that you can use to run cosplot.m:


File : matlab_slurm.sl

#!/bin/bash -l
#SBATCH --job-name=matlab_test
#SBATCH --account=def-someprof # adjust this to match the accounting group you are using to submit jobs
#SBATCH --time=0-03:00         # adjust this to match the walltime of your job
#SBATCH --nodes=1      
#SBATCH --ntasks=1
#SBATCH --cpus-per-task=1      # adjust this if you are using parallel commands
#SBATCH --mem=4000             # adjust this according to the memory requirement per node you need
#SBATCH --mail-user=you@youruniversity.ca # adjust this to match your email address
#SBATCH --mail-type=ALL

# Choose a version of MATLAB by loading a module:
module load matlab/2023b.2
# Remove -singleCompThread below if you are using parallel commands:
matlab -singleCompThread -batch "cosplot"


Submit the job using sbatch:

Question.png
[name@server ~]$ sbatch matlab_slurm.sl

Do not use the -singleCompThread option if you request more than one core with --cpus-per-task. You should also ensure that the size of your MATLAB parpool matches the number of cores you are requesting.

Each time you run MATLAB, it will create a file like java.log.12345 unless you supply the -nojvm option. However, using -nojvm may interfere with certain plotting functions. For further information on command line options -nodisplay, -singleCompThread, -nojvm, and -r, see MATLAB (Linux) on the MathWorks website.

Running multiple parallel MATLAB jobs simultaneously

There is a known issue when two (or more) parallel MATLAB jobs are initializing their parpool simultaneously: multiple new MATLAB instances are trying to read and write to the same .dat file in the $HOME/.matlab/local_cluster_jobs/R* folder, which corrupts the local parallel profile used by other MATLAB jobs. To fix the corrupted profile, delete the local_cluster_jobs folder when no job is running.

There are two main definitive solutions:

  1. Making sure only one MATLAB job at a time will start its parpool. There are many possible technical solutions, but none is perfect:
    • using a lock file (which may remain locked if a previous job has failed),
    • using random delays (which may be equal or almost equal, and still cause the corruption),
    • using always increasing delays (which are wasting compute time),
    • using Slurm options --begin or --dependency=after:JOBID to control the start time (which increases wait time in the queue).
  2. Making sure each MATLAB job creates a local parallel profile in a unique location of the filesystem.

In your MATLAB code:

File : parallel_main.m

% Create a "local" cluster object
local_cluster = parcluster('local')

% Modify the JobStorageLocation to $SLURM_TMPDIR
local_cluster.JobStorageLocation = getenv('SLURM_TMPDIR')

% Start the parallel pool
parpool(local_cluster);


References:

Using the Compiler and Runtime libraries

Important: Like any other intensive job, you must always run MCR code within a job submitted to the scheduler. For instructions on using the scheduler, please see the Running jobs page.

You can also compile your code using MATLAB Compiler, which is included among the modules we host. See documentation for the compiler on the MathWorks website. At the moment, mcc is provided for versions 2014a, 2018a and later.

To compile the cosplot.m example given above, you would use the command

Question.png
[name@yourserver ~]$ mcc -m -R -nodisplay cosplot.m

This will produce a binary named cosplot, as well as a wrapper script. To run the binary on our servers, you will only need the binary. The wrapper script named run_cosplot.sh will not work as is on our servers because MATLAB assumes that some libraries can be found in specific locations. Instead, we provide a different wrapper script called run_mcr_binary.sh which sets the correct paths.

On one of our servers, load an MCR module corresponding to the MATLAB version you used to build the executable:

Question.png
[name@server ~]$ module load mcr/R2018a

Run the following command:

Question.png
[name@server ~]$ setrpaths.sh --path cosplot

then, in your submission script (not on the login nodes), use your binary as so: run_mcr_binary.sh cosplot

You will only need to run the setrpaths.sh command once for each compiled binary. The run_mcr_binary.sh will instruct you to run it if it detects that it has not been done.

Using the MATLAB Parallel Server

MATLAB Parallel Server is only worthwhile if you need more workers in your parallel MATLAB job than available CPU cores on a single compute node. While a regular MATLAB installation (see above sections) allows you to run parallel jobs within one node (up to 64 workers per job, depending on which node and cluster), the MATLAB Parallel Server is the licensed MathWorks solution for running a parallel job on more than one node.

This solution usually works by submitting MATLAB parallel jobs from a local MATLAB interface on your computer. Since May 2023, some mandatory security improvements have been implemented on all clusters. Because MATLAB uses an SSH mode that is no longer permitted, job submission from a local computer is no longer possible until MATLAB uses a new connection method. There is currently no workaround.

Slurm plugin for MATLAB

The procedure below no longer works because the Slurm plugin is no longer available and because of the SSH issue described above. The configuration steps are kept until a workaround is found:

  1. Have MATLAB R2022a or newer installed, including the Parallel Computing Toolbox.
  2. Go to the MathWorks Slurm Plugin page, download and run the *.mlpkginstall file. (i.e., click on the blue Download button on the right side, just above the Overview tab.)
  3. Enter your MathWorks credentials; if the configuration wizard does not start, run in MATLAB
    parallel.cluster.generic.runProfileWizard()
  4. Give these responses to the configuration wizard:
    • Select Unix (which is usually the only choice)
    • Shared location: No
    • Cluster host:
      • For Béluga: beluga.computecanada.ca
      • For Narval: narval.computecanada.ca
    • Username (optional): Enter your Alliance username (the identity file can be set later if needed)
    • Remote job storage: /scratch
      • Keep Use unique subfolders checked
    • Maximum number of workers: 960
    • Matlab installation folder for workers (both local and remote versions must match):
      • For local R2022a: /cvmfs/restricted.computecanada.ca/easybuild/software/2020/Core/matlab/2022a
    • License type: Network license manager
    • Profile Name: beluga or narval
  5. Click on Create and Finish to finalize the profile.

Edit the plugin once installed

In MATLAB, go to the nonshared folder (i.e., run the following in the MATLAB terminal):

cd(fullfile(matlabshared.supportpkg.getSupportPackageRoot, 'parallel', 'slurm', 'nonshared'))

Then:

  1. Open the independentSubmitFcn.m file; around line #117 is the line

    additionalSubmitArgs = sprintf('--ntasks=1 --cpus-per-task=%d', cluster.NumThreads);

    Replace this line with

    additionalSubmitArgs = ccSBATCH().getSubmitArgs();

  2. Open the communicatingSubmitFcn.m file; around line #126 is the line

    additionalSubmitArgs = sprintf('--ntasks=%d --cpus-per-task=%d', environmentProperties.NumberOfTasks, cluster.NumThreads);

    Replace this line with

    additionalSubmitArgs = ccSBATCH().getSubmitArgs();

  3. Open the communicatingJobWrapper.sh file; around line #20 (after the copyright statement), add the following command and adjust the module version to your local Matlab version:

    module load matlab/2022a

Restart MATLAB and go back to your home directory:

cd(getenv('HOME'))  # or cd(getenv('HOMEPATH')) on Windows

Validation

Do not use the built-in validation tool in the Cluster Profile Manager. Instead, you should try the TestParfor example, along with a proper ccSBATCH.m script file:

  1. Download and extract code samples on GitHub at https://github.com/ComputeCanada/matlab-parallel-server-samples.
  2. In MATLAB, go to the newly extracted TestParfor directory.
  3. Follow instructions in https://github.com/ComputeCanada/matlab-parallel-server-samples/blob/master/README.md.

Note: When the ccSBATCH.m is in your current working directory, you may try the Cluster Profile Manager validation tool, but only the first two tests will work. Other tests are not yet supported.

External resources

MathWorks provides a variety of documentation and training about MATLAB.

Some universities also provide their own MATLAB documentation: