Newer
Older
Matlab is a bit of a problem child on the Tardis. While the `MATLAB Distributed
Computing Server`_ product aims to implement a compatibility layer to a number
of PBS based clusters it just doesn't work reliably for a number of reasons.
Because there is only a limited number of shared licenses available it's also
not feasible to run an arbitrary number of matlab sessions in the form of jobs.
A workaround is to "compile" a script and create a standalone redistribution
environment, which does not require a license to run.
Different Matlab versions are available via environment modules. You can list
them with :program:`module avail matlab` and activate a specific version with :program:`module
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
load matlab/<version>`.
Regular sessions
----------------
**If** there are free licenses available and you just need a quick way to spawn a
single Matlab session, there is nothing wrong with just running Matlab as is.
This might especially be useful if you simply need a node with lot's of memory
or if you want to test your code. In an interactive job you could simply enter
"matlab" and it will warn you about there not being a display available and
start in command line mode.
.. code-block:: bash
[krause@master ~] qsub -i -l mem=100gb -q testing
qsub: waiting for job 5395814.master.tardis.mpib-berlin.mpg.de to start
qsub: job 5395814.master.tardis.mpib-berlin.mpg.de ready
[krause@ood-9.tardis.mpib-berlin.mpg.de ~] module load matlab/R2012b
[krause@ood-9.tardis.mpib-berlin.mpg.de ~] matlab
< M A T L A B (R) >
Copyright 1984-2012 The MathWorks, Inc.
R2012a (7.14.0.739) 64-bit (glnxa64)
February 9, 2012
To get started, type one of these: helpwin, helpdesk, or demo.
For product information, visit www.mathworks.com.
>> _
In a job context you would just run :program:`matlab -r main` with main.m containing your script:
[krause@master ~] echo "module load matlab/R2014b; matlab -r main" | qsub -d.
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
.. important:
Always check `http://lipsupport.mpib-berlin.mpgde/licstat`_ to see if there is an available license.
Compiling
---------
Once you leave the testing stage and would like to spawn an arbitrary number of
matlab jobs/processes you have to compile your script with :program:`mcc`.
A reliable pattern is to create a main file :file:`project.m` that contains
a function with the same name and expects some arguments you would like to loop
over. A little like this maybe:
.. code-block:: matlab
function project(subject_id, sigma)
%
% my main program implementing foo
%
% arguments
% ---------
%
% subject_id: a string encoding the subject id
% sigma: a string encoding values for sigma
sigma = str2num(sigma);
repmat(cellstr(subject_id), 1, sigma)
Running :program:`mcc -m project.m` would then "compile" (or rather encrypt and
package) your function and output a system dependent binary named
:file:`project` and a wrapper script :file:`run_project.sh`. To run it you
now have to combine the wrapper script, the location of a Matlab Compile
Runtime or the local installation path of the matlab instance, that was used by
mcc, and a sufficient number of arguments for the function project().
Example:
.. code-block:: matlab
[krause@master ~] mcc -m project.m
[krause@master ~] ./run_project.sh /opt/matlab/interactive 42 5
------------------------------------------
Setting up environment variables
---
LD_LIBRARY_PATH is .:/opt/matlab/interactive/runtime/glnxa64:/opt/matlab/interactive/bin/glnxa64:/opt/matlab/interactive/sys/os/glnxa64:/opt/matlab/interactive/sys/java/jre/glnxa64/jre/lib/amd64/native_threads:/opt/matlab/interactive/sys/java/jre/glnxa64/jre/lib/amd64/server:/opt/matlab/interactive/sys/java/jre/glnxa64/jre/lib/amd64/client:/opt/matlab/interactive/sys/java/jre/glnxa64/jre/lib/amd64
Warning: No display specified. You will not be able to display graphics on the screen.
ans =
'42' '42' '42' '42' '42'
[krause@master ~]
To include toolboxes in your script you have to add them during the compile step
so they get included in your package. Matlab built-in toolboxes like signal
processing or statistics are detected automatically by scanning the functions
used in your script and don't need to be added explicitly.
Example: Suppose you collect your project library in a toolbox called project,
which in turn uses the function `normrnd()` from the statistics package:
.. code-block:: bash
[krause@master ~] cat matlab/tools/project/myrnd.m
function X = myrnd(arg)
X = normrnd(0, 1, arg, arg);
You can then either use the "-a" or the "-I" switch of mcc to add your own toolbox.
+ **-a** will add the functions or directories listed directly to the compiled package/archive
+ **-I** (uppercase i) will add the location to the mcc search path so it get's included implicitly
Both options should work fine. The example below uses mcc from matlab R2014b,
but you can use any version. The important part is to use the same Matlab
version as MCR upon script invocation with :program:`run_project.sh`.
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
.. code-block:: matlab
[krause@master ~] module load matlab/R2014b
[krause@master ~] cat project.m
function project(arg1)
myrnd(str2num(arg1))
[krause@master ~] mcc -m project.m -a matlab/tools/project
[...]
[krause@master ~] ./run_project.sh /opt/matlab/R2014b 3
------------------------------------------
Setting up environment variables
---
LD_LIBRARY_PATH is .:/opt/matlab/R2014b/runtime/glnxa64:/opt/matlab/R2014b/bin/glnxa64:/opt/matlab/R2014b/sys/os/glnxa64:/opt/matlab/R2014b/sys/opengl/lib/glnxa64
ans =
0.5377 0.8622 -0.4336
1.8339 0.3188 0.3426
-2.2588 -1.3077 3.5784
[krause@master ~]
.. note::
You only have to compile your project once and can then use it any number
of times. Matlab extracts your package to a shared hidden folder called
`.mcrCache<Version-Number>`. Those folders sometimes get corrupted by
Matlab, especially when multiple jobs start at exactly the same time. The
only workaround so far is to add a sleep 1s between qsub calls and hope
there is no collision. Also, it makes sense to regularly remove those
directories. But make sure all your jobs have finished before removing
SPM
---
SPM already comes as a pre-compiled version and can, identical to the examples
above, be started with :program:`run_spm8.sh` or :program:`run_spm12.sh`. Usually users are
exporting a number of batch files with the spm gui on their local machine,
change the paths to reflect the names on the tardis and then call
:program:`run_spm12.sh` with the **run** parameter for each batch file.
Example: segmentation for a number of nifti images. The file batch.template contains the string :`%%IMAGE%%` as a placeholder so we can easily replace it with the current image path and create a number of new batches from a single template:
.. code-block:: bash
#!/bin/bash
i=0
for image in tp2/Old/*.nii ; do
fullpath=$PWD/$image
sed "s#%%IMAGE%%#$fullpath#" batch.template > batch_${i}.m
echo "run_spm12.sh /opt/matlab/interactive run $PWD/batch_${i}.m" | qsub -d.
i=$((i+1))
done
Sometimes it ***might be*** necessary to recompile the spm toolbox yourself,
for instance if you need a specific version or if you want to add external
toolboxes to SPM (e.g. cat12).
.. code-block:: matlab
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
[krause@master ~] matlab
Warning: No display specified. You will not be able to display graphics on the screen.
< M A T L A B (R) >
Copyright 1984-2012 The MathWorks, Inc.
R2012a (7.14.0.739) 64-bit (glnxa64)
February 9, 2012
To get started, type one of these: helpwin, helpdesk, or demo.
For product information, visit www.mathworks.com.
>> addpath(genpath('/home/mpib/krause/matlab/tools/spm12'))
>> spm_make_standalone()
[... lot's of output and warnings ...]
Processing /opt/matlab/R2012a/toolbox/matlab/mcc.enc
[... lot's of output and warnings ...]
This should create a folder :file:`spm_exec` below the spm toolbox location
containing the fresh :program:`spm12` and :program:`run_spm12.sh` which you can
then use in your jobs just like above.
.. _`MATLAB Distributed Computing Server`: http://de.mathworks.com/help/mdce/index.html