Matlab
======

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
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:


.. code-block:: bash

   [krause@master ~] echo "module load matlab/R2014b; matlab -r main" | qsub -d.

.. 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`.

.. 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
	them with :file:`rm -rf .mcrCache*`.

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

   [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 ...]
   Generating file "/home/mpib/krause/matlab/tools/spm_exec/readme.txt".Gen
   Generating file "/home/mpib/krause/matlab/tools/spm12/../spm_exec/run_spm12.sh".
   >>


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.

Fieldtrip
---------

To properly add the fieldtrip toolbox we have to jump through some more hoops.
For now the only reliable and flexible way is to run :program:`mcc()` from
within a Matlab session and make sure to run :program:`ft_defaults()` first.
Also, some of the provided mex files won't work out of the box, so we have to
recompile them using :program:`ft_compile_mex()`. This however stumbles over
some external C file called :file:`CalcMD5.c` using non-standard comments.

The following Matlab Script has been successfully used to create a compiled
script from a :file:`main.m` file, which relies on internal fieldtrip
functions.

.. code-block:: matlab
   % setup path
   basepath='/home/mpib/krause/matlab/tools/ConMemEEGTools/'
   addpath([basepath, '/fieldtrip-20150930'])
   ft_defaults()

   % re-compile mex functions (this has to be done only once per fieldtrip version)
   % "fix" the CalcMD5.c file
   system(['sed -i ''s#//.*##g'' ', basepath, '/fieldtrip-20150930/external/fileexchange/CalcMD5.c'])
   % and compile
   ft_compile_mex(true)

   % build the runtime environment
   mcc('-m', 'main.m')

.. _`MATLAB Distributed Computing Server`: http://de.mathworks.com/help/mdce/index.html
