Search code examples
matlabparallel-processingparfor

How do I run MATLAB scripts in parallel (i.e. on multiple cores and without using parfor)?


I am looking for options to run MATLAB scripts I have in parallel without too much hassle.

Is there an example or a simple scenario on how to proceed in this case?


Solution

  • Note below MacOSX/Linux are assumed. But parallel will work inside envs like msys on Windows

    This is an example on how to start MATLAB jobs in parallel on your computer (without using parfor or the need to rely on Parallel Toolbox license):

    seq 1 4 | parallel 'matlab -singleCompThread -nojvm -r "myprint({}); exit"'
    

    where myprint.m is the following MATLAB function (placed in current working directory):

    cat myprint.m 
    function myprint(text)
       disp(text)
    end
    

    This behaviour can be replicated using Octave: http://www.gnu.org/software/octave/

    seq 1 4 | parallel 'octave --eval "myprint({}); exit"'
    

    parallel should be installed. More information about the tool can be found at the GNU project homepage: http://www.gnu.org/software/parallel/man.html

    Some short videos displaying the most common usage are available at: http://www.youtube.com/playlist?list=PL284C9FF2488BC6D1. If you prefer a video in Ogg/Theora format: http://tinyogg.com/watch/TORaR/ and http://tinyogg.com/watch/hfxKj/

    Alternative method - compiling the MATLAB code

    Another way to avoid relying on the Parallel Toolbox license is to compile to the code as the following

    mcc -m myprint.m
    

    If everything worked fine (i.e. compiled without any errors), you will see a myprint executable and a bash script. To run the script, simply do this in the terminal:

    run_myprint.sh
    

    Find where is your MATLAB installed (either using the $MATLABROOT bash variable or $MCR [MATLAB runtime folder]). You can now start your program:

    ./run_myprint.sh /usr/local/MATLAB/R2010b 2
    

    You will see

    ------------------------------------------
    Setting up environment variables
    ---
    LD_LIBRARY_PATH is .:/usr/local/MATLAB/R2010b/runtime/glnxa64:/usr/local/MATLAB/R2010b/bin/glnxa64:/usr/local/MATLAB/R2010b/sys/os/glnxa64:/usr/local/MATLAB/R2010b/sys/java/jre/glnxa64/jre/lib/amd64/native_threads:/usr/local/MATLAB/R2010b/sys/java/jre/glnxa64/jre/lib/amd64/server:/usr/local/MATLAB/R2010b/sys/java/jre/glnxa64/jre/lib/amd64/client:/usr/local/MATLAB/R2010b/sys/java/jre/glnxa64/jre/lib/amd64
    2
    

    The parallel bash version running the compiled MATLAB code in parallel looks like this:

    seq 1 4 | parallel './run_myprint.sh /usr/local/MATLAB/R2010b {}'
    

    A word of advice

    If your problem and hence your code is embarrassingly parallel, keep it that way.

    parfor is only truly necessary to uncouple dependencies in your data:

    Check here for more details: http://en.wikipedia.org/wiki/Embarrassingly_parallel