Matlab: Difference between revisions

From bwHPC Wiki
Jump to navigation Jump to search
No edit summary
Line 13: Line 13:
|-
|-
| Links
| Links
| [https://de.mathworks.com/products/matlab/ Matlab Homepage] | [https://de.mathworks.com/index.html?s_tid=gn_logo Mathworks Homepage] | [https://de.mathworks.com/support/?s_tid=gn_supp Support and more]
| [https://de.mathworks.com/products/matlab/ MATLAB Homepage] | [https://de.mathworks.com/index.html?s_tid=gn_logo MathWorks Homepage] | [https://de.mathworks.com/support/?s_tid=gn_supp Support and more]
|-
|-
| Graphical Interface
| Graphical Interface
| No
| No
|}
|}




= Description =
= Description =
MATLAB is a high-level language and interactive environment for numerical computation, visualization and programming.
<br>
<br>
= Versions and Availability =
A list of versions currently available on all bwHPC-C5-Clusters can be obtained from the
<br>
<big>


MATLAB is a high-level programming language and interactive computing environment for numerical calculation and data visualization.
[https://cis-hpc.uni-konstanz.de/prod.cis/ Cluster Information System CIS]

</big>
= Availability =
<br>

MATLAB is available on all bwHPC-Clusters along with a collection of toolboxes. A complete list of versions currently installed on the bwHPC-Clusters can be obtained from the Cluster Information System (CIS).

{{#widget:Iframe
{{#widget:Iframe
|url=https://cis-hpc.uni-konstanz.de/prod.cis/bwUniCluster/math/matlab
|url=https://cis-hpc.uni-konstanz.de/prod.cis/bwUniCluster/math/matlab
|width=90%
|width=50%
|height=1000
|height=750
|border=0
|border=0
}}
}}

<br>
= Adding MATLAB to Your Environment =
In order to check what MATLAB versions are installed on the system, run the following command:

In order to check which versions of MATLAB are installed on the compute cluster, run the following command:
<pre>$ module avail math/matlab</pre>
<pre>$ module avail math/matlab</pre>
Typically, several MATLAB versions might be available.


In general, several MATLAB versions should be returned.
The default version can be accessed by loading the appropriate module:
The default version can be accessed by loading the appropriate module:
<pre>$ module load math/matlab</pre>
<pre>$ module load math/matlab</pre>


Other versions of MATLAB (if available) can be loaded as:
Other MATLAB versions (if available) can be loaded according to:
<pre>$ module load math/matlab/<version></pre>
<pre>$ module load math/matlab/<version></pre>
with <version> specifying the desired version.
with <version> specifying the desired version.


An interactive MATLAB session with graphical user interface (GUI) can be started with the command
An interactive MATLAB session with graphical user interface (GUI) can be started with the command (requires X11 forwarding enabled for your ssh login):
(requires X11 forwarding enabled for your ssh login):
<pre>$ matlab</pre>
<pre>$ matlab</pre>


Since graphics rendering can be very slow on remote connections, the preferable way is to run the MATLAB command line interface without GUI:
Since graphics rendering can be very slow on remote connections, the preferable way is to run the MATLAB command line interface without GUI:
<pre>$ matlab -nodisplay</pre>
<pre>$ matlab -nodisplay</pre>
In general it is not advisable to invoke an interactive MATLAB session on a login node of the cluster. The recommended way to run a long-duration interactive MATLAB session is to submit an interactive job and start MATLAB from within the dedicated compute node assigned to you by the queueing system (consult the specific cluster system users guide on how to submit interactive jobs).


Note: It is not advisable to invoke an interactive MATLAB session on a login node of the cluster.
The following command will execute a MATLAB script on a single thread:
The recommended way to run a long-duration interactive MATLAB session is to submit an interactive job and start MATLAB from within the dedicated compute node assigned to you by the queueing system (consult the specific cluster users guide on how to submit interactive jobs).
<pre>matlab -nodisplay -singleCompThread -r script >result.out 2>&1</pre>
The output of this session will then be redirected to the file result.out.
The<span style="background:#edeae2;margin:10px;padding:1px;border:1px dotted #808080">-singleCompThread</span>option prevents the creation of multiple threads.
Most of the time, running MATLAB in single-threaded mode (as described above) will
meet your needs. But if you have very mathematically intense computations that might
benefit from the built-in multi-threading provided by MATLAB's BLAS and FFT implementation,
then you can experiment with running in multi-threaded mode by omitting the
<span style="background:#edeae2;margin:10px;padding:1px;border:1px dotted #808080">-singleCompThread</span>option (but see below on how to control the number of threads).


The following command will execute a MATLAB script or function named "example" on a single thread:
As with all processes that require more than a few minutes to run, non-trivial
<pre>$ matlab -nodisplay -singleCompThread -r example >result.out 2>&1</pre>
MATLAB jobs must be submitted to the cluster queuing system.


The output of this session will be redirected to the file result.out. The option <span style="background:#edeae2;margin:2px;padding:1px;border:1px dotted #808080">-r</span> executes the MATLAB statement interactively. The option <span style="background:#edeae2;margin:2px;padding:1px;border:1px dotted #808080">-singleCompThread</span> limits MATLAB to single computational thread. Most of the time, running MATLAB in single-threaded mode will meet your needs. But if you have mathematically intense computations that benefit from the built-in multithreading provided by MATLAB's BLAS and FFT implementation, then you can experiment with running in multi-threaded mode by omitting this option (see section 4.1 - Implicit Threading).
Example batch scripts are available in the directory pointed to by the environment variable <span style="background:#edeae2;margin:10px;padding:1px;border:1px dotted #808080">$MATLAB_EXA_DIR</span> after having loaded the module.


As with all processes that require more than a few minutes to run, non-trivial MATLAB jobs must be submitted to the cluster queuing system. Example batch scripts are available in the directory pointed to by the environment variable <span style="background:#edeae2;margin:2px;padding:1px;border:1px dotted #808080">$MATLAB_EXA_DIR</span>.
The help page of the MATLAB module may provide more version specific information:

The help page of the MATLAB module provides more version specific information:
<pre>$ module help math/matlab</pre>
<pre>$ module help math/matlab</pre>


= General performance tips for MATLAB =
= Parallel Computing Using MATLAB =

== Vectorization ==
Parallelization of MATLAB jobs is realized via the built-in multithreading provided by MATLAB's BLAS and FFT implementation and the parallel computing functionality of MATLAB's Parallel Computing Toolbox (PCT). The MATLAB Parallel/Distributed Computing Server is not available on the bwHPC-Clusters.
== Preallocation ==

In MATLAB data structures (such arrays or matrices) are dynamic in size, i.e. MATLAB will automatically resize the structure on demand. Although this seems to be very convenient, MATLAB needs to allocate a new chunk of contiguous memory and copy over the data to the new block of memory as the array or matrix grows in a loop. This may take a significant amount of extra time during execution of the program.
== Implicit Threading ==
Code performance can often be drastically improved by preallocating memory for the final expected size of the array or matrix before actually starting the processing loop. In order to preallocate an array (or matrix) of numbers, you can use the [https://www.mathworks.de/de/help/matlab/ref/zeros.html zeros] function. In order to preallocate an array of strings, you can use the [https://www.mathworks.de/de/help/matlab/ref/cell.html cell] function.

A large number of built-in MATLAB functions may utilize multiple cores automatically without any code modifications required. This is referred to as implicit multithreading and must be strictly distinguished from explicit parallelism provided by the Parallel Computing Toolbox (PCT) which requires specific commands in your code in order to create threads.

Implicit threading particularly takes place for linear algebra operations (such as the solution to a linear system A\b or matrix products A*B) and FFT operations. Many other high-level MATLAB functions do also benefit from multithreading capabilities of their underlying routines. However, the user can still enforce single-threaded mode by adding the command line option <span style="background:#edeae2;margin:2px;padding:1px;border:1px dotted #808080">-singleCompThread</span>.

Whenever implicit threading takes place, MATLAB will detect the total number of cores that exist on a machine and by default makes use of all of them. This has very important implications for MATLAB jobs in HPC environments with shared-node job scheduling policy (i.e. with multiple users sharing one compute node). Due to this behaviour, a MATLAB job may take over more compute resources than assigned by the queueing system of the cluster (and thereby taking away these resources from all other users with running jobs on the same node - including your own jobs).

Therefore, when running in multi-threaded mode, MATLAB always requires the user's intervention to not allocate all cores of the machine (unless requested so from the queueing system). The number of threads must be controlled from within the code by means of the <span style="background:#edeae2;margin:2px;padding:1px;border:1px dotted #808080">maxNumCompThreads(N)</span> function (which is supposed to be deprecated) or, alternatively, with the <span style="background:#edeae2;margin:2px;padding:1px;border:1px dotted #808080">feature('numThreads', N)</span> function (which is currently undocumented).

== Using the Parallel Computing Toolbox (PCT) ==

By using the PCT one can make explicit use of several cores on multicore processors to parallelize MATLAB applications without MPI programming. Under MATLAB version 8.4 and earlier, this toolbox provides up to 12 workers (MATLAB computational engines) to execute applications locally on a single multicore node. Under MATLAB version 8.5 and later, the number of workers available is equal to the number of cores on a single node (up to a maximum of 512).

If multiple PCT jobs are running at the same time, they all write temporary MATLAB job information to the same location. This race condition can cause one or more of the parallel MATLAB jobs fail to use the parallel functionality of the toolbox.

To solve this issue, each MATLAB job should explicitly set a unique location where these files are created. This can be accomplished by the following snippet of code added to your MATLAB script.

{{bwFrameA|
<source lang="Matlab">

% create a local cluster object
pc = parcluster('local')

% get the number of dedicated cores from environment
num_workers = str2num(getenv('SLURM_NPROCS'))

% explicitly set the JobStorageLocation to the tmp directory that is unique to each cluster job (and is on local, fast scratch)
parpool_tmpdir = [getenv('TMP'),'/.matlab/local_cluster_jobs/slurm_jobID_',getenv('SLURM_JOB_ID')]
mkdir(parpool_tmpdir)
pc.JobStorageLocation = parpool_tmpdir

% start the parallel pool
parpool(pc,num_workers)

</source>
}}

Note: The code snippet also sets the correct number of parallel workers in MATLAB according to the total number of processes dedicated to the job given by the environment variable <span style="background:#edeae2;margin:2px;padding:1px;border:1px dotted #808080">$SLURM_NPROCS</span> in the job submission file.

= General Performance Tips for MATLAB =

MATLAB data structures (arrays or matrices) are dynamic in size, i.e. MATLAB will automatically resize the structure on demand. Although this seems to be convenient, MATLAB continually needs to allocate a new chunk of memory and copy over the data to the new block of memory as the array or matrix grows in a loop. This may take a significant amount of extra time during execution of the program.

Code performance can often be drastically improved by preallocating memory for the final expected size of the array or matrix before actually starting the processing loop. In order to preallocate an array of strings, you can use MATLAB's build-in cell function. In order to preallocate an array or matrix of numbers, you can use MATLAB's build-in zeros function.

The performance benefit of preallocation is illustrated with the following example code.


The performance benefit of preallocation is illustrated in the following example code:
{{bwFrameA|
{{bwFrameA|
<source lang="Matlab">
<source lang="Matlab">
Line 97: Line 135:
end
end
toc
toc



disp('With preallocation:')
disp('With preallocation:')
Line 113: Line 150:


<pre>
<pre>

>>prealloc
Without preallocation:
Without preallocation:
Elapsed time is 2.879446 seconds.
Elapsed time is 2.879446 seconds.
With preallocation:
With preallocation:
Elapsed time is 0.097557 seconds.
Elapsed time is 0.097557 seconds.

</pre>
</pre>


In this case, the code runs almost 30 times faster with preallocation.
Please recognize that the code runs almost 30 times faster with preallocation.

= Parallel Computing with MATLAB =
== Implicit Threading ==
A large number of built-in MATLAB functions may utilize multiple cores automatically without any code modifications required. This is referred to as implicit multithreading and must be strictly distinguished from explicit parallelism provided by the Parallel Computing Toolbox (PCT) which requires specific commands in your code in order to create threads.

Implicit threading particularly takes place for linear algebra operations (such as the solution to a linear system A\b or matrix products A*B) and FFT operations.
Many other high-level MATLAB functions do also benefit from multithreading capabilities of their underlying routines. However, the user can still enforce single threaded mode by adding the<span style="background:#edeae2;margin:10px;padding:1px;border:1px dotted #808080">-singleCompThread</span>command line option.

Whenever implicit threading takes place, MATLAB will detect the total number of cores that exist on a machine and by default makes use of '''all of them'''. This has very important implications for MATLAB jobs in HPC environments with shared-node job scheduling policy (i.e. with multiple users sharing one compute node). Due to the behaviour described above, a MATLAB job may take over more compute ressources than assigned by the queueing system of the cluster (and thereby taking away these ressources from all other users having jobs running on the same node - including your own jobs).

Therefore, when running in multi-threaded mode, MATLAB always requires the user's intervention to '''not''' allocate all cores of the machine (unless requested so from the queueing system). The number of threads must be controlled from within the code by means of the <span style="background:#edeae2;margin:10px;padding:1px;border:1px dotted #808080">maxNumCompThreads(N)</span>function (which
is supposed to be deprecated) or, alternatively, with the
<span style="background:#edeae2;margin:10px;padding:1px;border:1px dotted #808080">feature('numThreads', N)</span> function (which is currently undocumented).

== Using the Parallel Computing Toolbox ==

By using the Parallel Computing Toolbox (parfor etc.) one can make explicit use of several cores on the same node. However, if the user runs multiple jobs using the Parallel Computing Toolbox at the same time, they all write to the same directory, using conflicting file names, e.g. ".matlab/local_cluster_jobs/R2016a/Job1.in.mat". This can lead to Matlab crashing.

To solve this problem, each Matlab job should explicitly and indivudally set the directory where these files are created. This can be accomplished with the following code added to your Matlab script. As an added bonus, this starts the correct number of parallel matlab workers depending on the ppn (processors/cores per node) given in the MOAB job description file.

Please note that the environment variable for the temporary directory is different on the bwUniCluster.

<pre>
% create a local cluster object
pc = parcluster('local')

% explicitly set the JobStorageLocation to the temp directory that
% is unique to each cluster job (and is on local, fast scratch)
pc.JobStorageLocation = getenv('TMPDIR')

% please use (uncomment) this ONLY for bwUniCluster
% pc.JobStorageLocation = getenv('TMP')

% get the number of dedicated cores from environment
num_workers = str2num(getenv('MOAB_PROCCOUNT'))

% start the parallel pool
parpool (pc, num_workers)
</pre>


<!--
== Explicit Threading using the Parallel Computing Toolbox ==
2014-05-07, RB: To be filled with some content.
-->

= Correct allocation of all processors attributed by MOAB 4.2 =

The command:<br>
''pc.NumWorkers=num_workers;''<br>
should be added after the lines:<br>
''% get the number of dedicated cores from environment<br>
num_workers = str2num(getenv('MOAB_PROCCOUNT'))''<br>
Without this additional line, the max number of workers is stuck at 12. Thus the next command:<br>
''parpool (pc, num_workers)''<br>
can fail to start the parallel pool, returning message:
"You requested a minimum of num_workers workers, but the cluster "local" has the NumWorkers property set to allow a maximum of 12 workers. <br>
To run a communicating job on more workers than this (up to a maximum of 512 for the Local cluster), increase the value of the NumWorkers property for the cluster. <br>
The default value of NumWorkers for a Local cluster is the number of cores on the local machine."

These infos are supplied by: Nicolas Rodriquez

= Remote job submission from your local Matlab GUI =

'''Note: Usage of the Matlab Distributed Compute Server (MDCS) for remote job submission is deprecated and only limited support is provided for legacy installations.'''

Available at: [[bwUniCluster]]

Prequisite: You need a Matlab installation on your desktop computer. For maximum compatibility, your local Matlab version should match the version on the cluster.

Using the MATLAB Distributed Computing Server (MDCS) you can run now computationally intensive MATLAB programs and Simulink models on the bwUniCluster.
You develop your program or model on a multicore desktop computer using Parallel Computing Toolbox and then scale up to many computers by running it on MDCS. The server supports batch jobs, parallel computations, and distributed large data.

You can use the Matlab remote job submission to make the job submission to the HPC clusters as easy as possible for you. Please download the following scripts and follow the steps described in the manual.

The MDCS license comprises a total of 32 workers. Note that one worker is required to manage the batch job and pool of workers. This means a job that needs for example eight workers will consume nine CPU cores.

The following manual (download) describes how you can configure MATLAB within your desktop MATLAB environment to run serial and parallel jobs on the bwUniCluster. This is demonstrated by a simple example scenario. The necessary cluster profiles for R2014b and R2015a can also be downloaded from the links below.

[[Media:GettingStartedWithSerialAndParallelMATLABOnbwGRiD.pdf | Download manual (Installation and usage) ]]

[[Media:BwCluster.remote.r2015a.zip | Download scripts 2015a (Windows/Mac Version)]]

[[Media:BwCluster.remote.r2015a.tar | Download scripts 2015a (Linux Version) ]]

[[Media:BwCluster.remote.r2014b.zip | Download scripts 2014b (Windows/Mac Version) ]]


[[Media:BwCluster.remote.r2014b.tar | Download scripts 2014b (Linux Version) ]]




----
----
[[Category:Mathematics software]][[Category:bwUniCluster]][[Category:BwForCluster_BinAC]][[Category:bwForCluster_MLS&WISO_Production]]
[[Category:Mathematics software]]
[[Category:BwUniCluster]]
[[Category:BwUniCluster_2.0]]
[[Category:BwForCluster_BinAC]]
[[Category:BwForCluster_MLS&WISO_Production]]
[[Category:BwForCluster_Chemistry]]
[[Category:BwForCluster_JUSTUS_2]]

Revision as of 21:06, 27 April 2020

Description Content
module load math/matlab
License Academic License/Commercial
Citing n/a
Links MATLAB Homepage | MathWorks Homepage | Support and more
Graphical Interface No


Description

MATLAB is a high-level programming language and interactive computing environment for numerical calculation and data visualization.

Availability

MATLAB is available on all bwHPC-Clusters along with a collection of toolboxes. A complete list of versions currently installed on the bwHPC-Clusters can be obtained from the Cluster Information System (CIS).

{{#widget:Iframe |url=https://cis-hpc.uni-konstanz.de/prod.cis/bwUniCluster/math/matlab |width=50% |height=750 |border=0 }}

Adding MATLAB to Your Environment

In order to check which versions of MATLAB are installed on the compute cluster, run the following command:

$ module avail math/matlab

In general, several MATLAB versions should be returned. The default version can be accessed by loading the appropriate module:

$ module load math/matlab

Other MATLAB versions (if available) can be loaded according to:

$ module load math/matlab/<version>

with <version> specifying the desired version.

An interactive MATLAB session with graphical user interface (GUI) can be started with the command (requires X11 forwarding enabled for your ssh login):

$ matlab

Since graphics rendering can be very slow on remote connections, the preferable way is to run the MATLAB command line interface without GUI:

$ matlab -nodisplay

Note: It is not advisable to invoke an interactive MATLAB session on a login node of the cluster. The recommended way to run a long-duration interactive MATLAB session is to submit an interactive job and start MATLAB from within the dedicated compute node assigned to you by the queueing system (consult the specific cluster users guide on how to submit interactive jobs).

The following command will execute a MATLAB script or function named "example" on a single thread:

$ matlab -nodisplay -singleCompThread -r example >result.out 2>&1

The output of this session will be redirected to the file result.out. The option -r executes the MATLAB statement interactively. The option -singleCompThread limits MATLAB to single computational thread. Most of the time, running MATLAB in single-threaded mode will meet your needs. But if you have mathematically intense computations that benefit from the built-in multithreading provided by MATLAB's BLAS and FFT implementation, then you can experiment with running in multi-threaded mode by omitting this option (see section 4.1 - Implicit Threading).

As with all processes that require more than a few minutes to run, non-trivial MATLAB jobs must be submitted to the cluster queuing system. Example batch scripts are available in the directory pointed to by the environment variable $MATLAB_EXA_DIR.

The help page of the MATLAB module provides more version specific information:

$ module help math/matlab

Parallel Computing Using MATLAB

Parallelization of MATLAB jobs is realized via the built-in multithreading provided by MATLAB's BLAS and FFT implementation and the parallel computing functionality of MATLAB's Parallel Computing Toolbox (PCT). The MATLAB Parallel/Distributed Computing Server is not available on the bwHPC-Clusters.

Implicit Threading

A large number of built-in MATLAB functions may utilize multiple cores automatically without any code modifications required. This is referred to as implicit multithreading and must be strictly distinguished from explicit parallelism provided by the Parallel Computing Toolbox (PCT) which requires specific commands in your code in order to create threads.

Implicit threading particularly takes place for linear algebra operations (such as the solution to a linear system A\b or matrix products A*B) and FFT operations. Many other high-level MATLAB functions do also benefit from multithreading capabilities of their underlying routines. However, the user can still enforce single-threaded mode by adding the command line option -singleCompThread.

Whenever implicit threading takes place, MATLAB will detect the total number of cores that exist on a machine and by default makes use of all of them. This has very important implications for MATLAB jobs in HPC environments with shared-node job scheduling policy (i.e. with multiple users sharing one compute node). Due to this behaviour, a MATLAB job may take over more compute resources than assigned by the queueing system of the cluster (and thereby taking away these resources from all other users with running jobs on the same node - including your own jobs).

Therefore, when running in multi-threaded mode, MATLAB always requires the user's intervention to not allocate all cores of the machine (unless requested so from the queueing system). The number of threads must be controlled from within the code by means of the maxNumCompThreads(N) function (which is supposed to be deprecated) or, alternatively, with the feature('numThreads', N) function (which is currently undocumented).

Using the Parallel Computing Toolbox (PCT)

By using the PCT one can make explicit use of several cores on multicore processors to parallelize MATLAB applications without MPI programming. Under MATLAB version 8.4 and earlier, this toolbox provides up to 12 workers (MATLAB computational engines) to execute applications locally on a single multicore node. Under MATLAB version 8.5 and later, the number of workers available is equal to the number of cores on a single node (up to a maximum of 512).

If multiple PCT jobs are running at the same time, they all write temporary MATLAB job information to the same location. This race condition can cause one or more of the parallel MATLAB jobs fail to use the parallel functionality of the toolbox.

To solve this issue, each MATLAB job should explicitly set a unique location where these files are created. This can be accomplished by the following snippet of code added to your MATLAB script.

% create a local cluster object
pc = parcluster('local')

% get the number of dedicated cores from environment
num_workers = str2num(getenv('SLURM_NPROCS'))

% explicitly set the JobStorageLocation to the tmp directory that is unique to each cluster job (and is on local, fast scratch)
parpool_tmpdir = [getenv('TMP'),'/.matlab/local_cluster_jobs/slurm_jobID_',getenv('SLURM_JOB_ID')]
mkdir(parpool_tmpdir)
pc.JobStorageLocation = parpool_tmpdir

% start the parallel pool
parpool(pc,num_workers)


Note: The code snippet also sets the correct number of parallel workers in MATLAB according to the total number of processes dedicated to the job given by the environment variable $SLURM_NPROCS in the job submission file.

General Performance Tips for MATLAB

MATLAB data structures (arrays or matrices) are dynamic in size, i.e. MATLAB will automatically resize the structure on demand. Although this seems to be convenient, MATLAB continually needs to allocate a new chunk of memory and copy over the data to the new block of memory as the array or matrix grows in a loop. This may take a significant amount of extra time during execution of the program.

Code performance can often be drastically improved by preallocating memory for the final expected size of the array or matrix before actually starting the processing loop. In order to preallocate an array of strings, you can use MATLAB's build-in cell function. In order to preallocate an array or matrix of numbers, you can use MATLAB's build-in zeros function.

The performance benefit of preallocation is illustrated with the following example code.

% prealloc.m

clear all;

num=10000000;

disp('Without preallocation:')
tic
for i=1:num
    a(i)=i;
end
toc

disp('With preallocation:')
tic
b=zeros(1,num);
for i=1:num
    b(i)=i;
end
toc


On a compute node, the result may look like this:

Without preallocation:
Elapsed time is 2.879446 seconds.
With preallocation:
Elapsed time is 0.097557 seconds.

Please recognize that the code runs almost 30 times faster with preallocation.