Skip to content
matlab.rst 8.55 KiB
Newer Older
Michael Krause's avatar
Michael Krause committed
Matlab
======

Michael Krause's avatar
Michael Krause committed
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.
Michael Krause's avatar
Michael Krause committed

Michael Krause's avatar
Michael Krause committed
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.
Michael Krause's avatar
Michael Krause committed

Michael Krause's avatar
Michael Krause committed
Different Matlab versions are available via environment modules.  You can list
Michael Krause's avatar
Michael Krause committed
them with :program:`module avail matlab` and activate a specific version with :program:`module
Michael Krause's avatar
Michael Krause committed
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.

	>> _


Michael Krause's avatar
Michael Krause committed
In a job context you would just run :program:`matlab -r main` with main.m containing your script:
Michael Krause's avatar
Michael Krause committed


.. code-block:: bash

Michael Krause's avatar
Michael Krause committed
   [krause@master ~] echo "module load matlab/R2014b; matlab -r main" | qsub -d.
Michael Krause's avatar
Michael Krause committed

.. 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
Michael Krause's avatar
Michael Krause committed
: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().
Michael Krause's avatar
Michael Krause committed

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 ~]


Michael Krause's avatar
Michael Krause committed
To include toolboxes in your script you have to add them during the compile step
Michael Krause's avatar
Michael Krause committed
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
Michael Krause's avatar
Michael Krause committed

Michael Krause's avatar
Michael Krause committed
   [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.

Michael Krause's avatar
Michael Krause committed
+ **-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
Michael Krause's avatar
Michael Krause committed
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`.
Michael Krause's avatar
Michael Krause committed

.. 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
Michael Krause's avatar
Michael Krause committed
	of times.  Matlab extracts your package to a shared hidden folder called
Michael Krause's avatar
Michael Krause committed
	`.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
Michael Krause's avatar
Michael Krause committed
	them with :file:`rm -rf .mcrCache*`.
Michael Krause's avatar
Michael Krause committed

SPM
---

SPM already comes as a pre-compiled version and can, identical to the examples
Michael Krause's avatar
Michael Krause committed
above, be started with :program:`run_spm8.sh` or :program:`run_spm12.sh`.  Usually users are
Michael Krause's avatar
Michael Krause committed
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.


Michael Krause's avatar
Michael Krause committed
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:
Michael Krause's avatar
Michael Krause committed

.. 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
Michael Krause's avatar
Michael Krause committed

Michael Krause's avatar
Michael Krause committed
   [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