MATLAB functions with built in parallel computing toolbox support

September 11th, 2011 | Categories: Making MATLAB faster, matlab, parallel programming, programming | Tags:

So, you’re the proud owner of a new license for MATLAB’s parallel computing toolbox (PCT) and you are wondering how to get some bang for your buck as quickly as possible.  Sure, you are going to learn about constructs such as parfor and spmd but that takes time and effort.  Wouldn’t it be nice if you could speed up some of your MATLAB code simply by saying ‘Turn parallelisation on’?

It turns out that The Mathworks have been adding support for their parallel computing toolbox all over the place and all you have to do is switch it on (Assuming that you actually have the parallel computing toolbox of course).  For example say you had the following call to fmincon (part of the optimisation toolbox) in your code

[x, fval] = fmincon(@objfun, x0, [], [], [], [], [], [], @confun,opts)

To turn on parallelisation across 2 cores just do

matlabpool 2;
opts = optimset('fmincon');
opts = optimset('UseParallel','always');
[x, fval] = fmincon(@objfun, x0, [], [], [], [], [], [], @confun,opts);

That wasn’t so hard was it?  The speedup (if any) completely depends upon your particular optimization problem.

Why isn’t parallelisation turned on by default?
The next question that might occur to you is ‘Why doesn’t The Mathworks just turn parallelisation on by default?’ After all, although the above modification is straightforward, it does require you to know that this particular function supports parallel execution via the PCT. If you didn’t think to check then your code would be doomed to serial execution forever.

The simple answer to this question is ‘Sometimes the parallel version is slower‘. Take this serial code for example.

objfun = @(x)exp(x(1))*(4*x(1)^2+2*x(2)^2+4*x(1)*x(2)+2*x(2)+1);
confun = @(x) deal( [1.5+x(1)*x(2)-x(1)-x(2); -x(1)*x(2)-10], [] );
tic;
[x, fval] = fmincon(objfun, x0, [], [], [], [], [], [], confun);
toc

On the machine I am currently sat at (quad core running MATLAB 2011a on Linux) this typically takes around 0.032 seconds to solve. With a problem that trivial my gut feeling is that we are not going to get much out of switching to parallel mode.

objfun = @(x)exp(x(1))*(4*x(1)^2+2*x(2)^2+4*x(1)*x(2)+2*x(2)+1);
confun = @(x) deal( [1.5+x(1)*x(2)-x(1)-x(2); -x(1)*x(2)-10],[] );

%only do this next line once.  It opens two MATLAB workers
matlabpool 2;

opts = optimset('fmincon');
opts = optimset('UseParallel','always');

tic;
[x, fval] = fmincon(objfun, x0, [], [], [], [], [], [], confun,opts);
toc

Sure enough, this increases execution time dramatically to an average of 0.23 seconds on my machine.  There is always a computational overhead that needs paying when you go parallel and if your problem is too trivial then this overhead costs more than the calculation itself.

So which functions support the Parallel Computing Toolbox?

I wanted a web-page that listed all functions that gain benefit from the Parallel Computing Toolbox but couldn’t find one.  I found some documentation on specific toolboxes such as Parallel Statistics but nothing that covered all of MATLAB in one place.  Here is my attempt at producing such a document.  Feel free to contact me if I have missed anything out.

This covers MATLAB 2011b and is almost certainly incomplete. I’ve only covered toolboxes that I have access to and so some are missing.  Please contact me if you have any extra information.

Bioinformatics Toolbox

Global Optimisation

Image Processing

Optimisation Toolbox

Simulink

  • Running parallel simulations
  • You can increase the speed of diagram updates for models containing large model reference hierarchies by building referenced models that are configured in Accelerator mode in parallel whenever conditions allow.  This is covered in the documentation.

Statistics Toolbox

Other articles about parallel computing in MATLAB from WalkingRandomly

  1. September 11th, 2011 at 18:17
    Reply | Quote | #1

    That’s pretty cool. I still think that for computers equipped with an NVIDIA GPU the CUDA-kernel interface is far and away the most useful feature of the PCT. I hope you have had a chance to try it.

    Just my opinion of course. :-) Once again, nice post.

  2. September 12th, 2011 at 05:39
    Reply | Quote | #2

    Thanks Stu. I read your post on the CUDA-kernel interface

    http://noiceinmyscotchplease.blogspot.com/2011/06/matlab-cuda-and-me.html

    and agree that it looks like a very useful piece of functionality. Not had need to use it myself yet but I am sure the time will come :)

    BTW, I added your post to my list of useful external links about GPU computing at http://www.walkingrandomly.com/?p=3730. Great tutorial!

  3. September 12th, 2011 at 16:02
    Reply | Quote | #3

    Thank you Mike!

  4. Gaurav Sharma
    September 12th, 2011 at 18:24
    Reply | Quote | #4

    Hi Mike,

    >> … a web-page that listed all functions that gain benefit from the Parallel Computing Toolbox.

    http://www.mathworks.com/products/parallel-computing/builtin-parallel-support.html

  5. September 13th, 2011 at 06:52
    Reply | Quote | #5

    Thanks Gaurav

    I looked for that but never found it! We’ve got differing levels of detail so hopefully writing this page wasn’t a waste of my time.

    Cheers,
    Mike

  6. MySchizoBuddy
    September 17th, 2011 at 21:14
    Reply | Quote | #6

    Stanford has a new online course videos title “programming Massively Parallel processors with cuda” http://itunes.apple.com/itunes-u/programming-massively-parallel/id384233322#ls=1

  7. Soheil
    October 3rd, 2011 at 16:31
    Reply | Quote | #7

    Great information.

    Thanks!