Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to utilise parallel processing in Matlab

I am working on a time series based calculation. Each iteration of the calculation is independent. Could anyone share some tips / online primers on using utilising parallel processing in Matlab? How can this be specified inside the actual code?

like image 245
Eduardas Avatar asked Oct 30 '10 01:10

Eduardas


People also ask

What is the use of MATLAB parallel server?

MATLAB® Parallel Server™ lets you scale MATLAB programs and Simulink® simulations to clusters and clouds. You can prototype your programs and simulations on the desktop and then run them on clusters and clouds without recoding.

What is parallel computing in MATLAB?

Parallel Computing Toolbox™ lets you solve computationally and data-intensive problems using multicore processors, GPUs, and computer clusters.

What is the use of parallel processing?

Parallel processing is a method in computing of running two or more processors (CPUs) to handle separate parts of an overall task. Breaking up different parts of a task among multiple processors will help reduce the amount of time to run a program.

Does MATLAB support multithreading?

Built-in MultithreadingMultithreaded computations have been on by default in MATLAB since Release 2008a. These functions automatically execute on multiple computational threads in a single MATLAB session, allowing them to execute faster on multicore-enabled machines.


3 Answers

Since you have access to the Parallel toolbox, I suggest that you first check whether you can do it the easy way.

Basically, instead of writing

for i=1:lots
   out(:,i)=do(something);
end

You write

parfor i=1:lots
   out(:,i)=do(something);
end

Then, you use matlabpool to create a number of workers (you can have a maximum of 8 on your local machine with the toolbox, and tons on a remote cluster if you also have a Distributed Computing Server license), and you run the code, and see nice speed gains when your iterations are run by 8 cores instead of one.

Even though the parfor route is the easiest, it may not work right out of the box, since you might do your indexing wrong, or you may be referencing an array in a problematic way etc. Look at the mlint warnings in the editor, read the documentation, and rely on good old trial and error, and you should figure it out reasonably fast. If you have nested loops, it's often best parallelize only the innermost one and ensure it does tons of iterations - this is not only good design, it also reduces the amount of code that could give you trouble.

Note that especially if you run the code on a local machine, you may run into memory issues (which might manifest in really slow execution in parallel mode because you're paging): Every worker gets a copy of the workspace, so if your calculation involves creating a 500MB array, 8 workers will need a total 4GB of RAM - and then you haven't even started counting the RAM of the parent process! In addition, it can be good to only use N-1 cores on your machine, so that there is still one core left for other processes that may run on the computer (such as a mandatory antivirus...).

like image 172
Jonas Avatar answered Oct 22 '22 10:10

Jonas


Mathworks offers its own parallel computing toolbox. If you do not want to purchase that, there a few options

  • You could write your own mex file and use pthreads or OpenMP.
  • However make sure you do not call any Mex api in the parallel part of the code, because they arent thread safe
  • If you want coarser grained parallelism via MPI you can try pmatlab
  • Same with parmatlab

Edit: Adding link Parallel MATLAB with openmp mex files

I have only tried the first.

like image 5
srean Avatar answered Oct 22 '22 08:10

srean


Don't forget that many Matlab functions are already multithreaded. By careful programming you may be able to take advantage of them -- check the documentation for your version as the Mathworks seem to be increasing the range and number of multithreaded functions with each new release. For example, it seems that 2010a has multithreaded ffts which may be useful for time series processing.

If the intrinsic multithreading is not what you need, then as @srean suggests, the Parallel Computing Toolbox is available. For my money (or rather, my employers' money) it's the way to go, allowing you to program in parallel in Matlab, rather than having to bolt things on. I have to admit, too, that I'm quite impressed by the toolbox and the facilities it offers.

like image 2
High Performance Mark Avatar answered Oct 22 '22 09:10

High Performance Mark