Difference between revisions of "Matlab"

From bwHPC Wiki
Jump to: navigation, search
(Blanked the page)
(Tag: Blanking)
(8 intermediate revisions by 2 users not shown)
Line 1: Line 1:
{| width=600px class="wikitable"
 
|-
 
! Description !! Content
 
|-
 
| module load
 
| math/matlab
 
|-
 
| License
 
| [https://de.mathworks.com/pricing-licensing/index.html?intendeduse=edu&prodcode=ML Academic License/Commercial]
 
|-
 
| Citing
 
| n/a
 
|-
 
| 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]
 
|-
 
| Graphical Interface
 
| No
 
|}
 
 
= 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>
 
 
[https://cis-hpc.uni-konstanz.de/prod.cis/ Cluster Information System CIS]
 
</big>
 
<br>
 
{{#widget:Iframe
 
|url=https://cis-hpc.uni-konstanz.de/prod.cis/bwUniCluster/math/matlab
 
|width=90%
 
|height=1000
 
|border=0
 
}}
 
<br>
 
In order to check what MATLAB versions are installed on the system, run the following command:
 
<pre>$ module avail math/matlab</pre>
 
Typically, several MATLAB versions might be available.
 
 
The default version can be accessed by loading the appropriate module:
 
<pre>$ module load math/matlab</pre>
 
 
Other versions of MATLAB (if available) can be loaded as:
 
<pre>$ module load math/matlab/<version></pre>
 
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):
 
<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:
 
<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).
 
 
The following command will execute a MATLAB script on a single thread:
 
<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).
 
 
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:10px;padding:1px;border:1px dotted #808080">$MATLAB_EXA_DIR</span> after having loaded the module.
 
 
The help page of the MATLAB module may provide more version specific information:
 
<pre>$ module help math/matlab</pre>
 
 
= General performance tips for MATLAB =
 
== Vectorization ==
 
== 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.
 
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.
 
 
The performance benefit of preallocation is illustrated in the following example code:
 
{{bwFrameA|
 
<source lang="Matlab">
 
 
% 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
 
 
</source>
 
}}
 
 
On a compute node, the result may look like this:
 
 
<pre>
 
 
>>prealloc
 
Without preallocation:
 
Elapsed time is 2.879446 seconds.
 
With preallocation:
 
Elapsed time is 0.097557 seconds.
 
 
</pre>
 
 
In this case, 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."
 
 
= 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]]
 

Revision as of 02:43, 12 October 2020