Matlab and parallel computing
As the improvement in CPUs slows, manufacturers are putting more of them in each machine, but maximising their use isn't always easy. Using several machines simultaneously is harder still. Matlab supports parallel computing in several ways. For some features the user needs merely to enable them. In other situations programs may need adjustments or a toolbox bought.
Threads
The matlab process is capable of splitting into "threads" which can run concurrently. All the threads have access to the same variables. The best performance is likely to be when there are as many threads as cores, which is the default.
Users can't control when matlab splits into "threads" - it does so when it can and when its worthwhile. On our linux servers (which have 4 quad-core CPUs, so the 'Automatic' option sets the number of threads to 4) the graph shows the speed-up factors for a range of tasks. Note that some operations aren't speeded up, whereas others are over 3 times faster (the y axis running from 0 to 3.5). Element-wise computations on big matrices might benefit most.
showdemo('multithreadedcomputations') runs a demo.
Parallel Toolbox
Using the Parallel Toolbox give you control over what code will be parallelised, but you'll need to change your code. Unless you consider the inter-process-communication overheads, you might find that the parallelised code is slower (by orders of magnitude sometimes) than the original.
The Parallel Toolbox has a matlabpool function to set the number of machines ("labs"). Each core ("local worker") can host a lab. You can run as many as four MATLAB workers on your local machine in addition to your MATLAB client session. The Distributed Computing Server product lets you to run extra workers on a remote cluster of computers.
Once you've set up a pool, programs can then use parfor which is like for except that the iterations may be farmed out to different CPUs. In the following code for example, one CPU could handle i=1:1000 while another could deal with i=1001:2000, etc
parfor i=1:10000 x(i)=x(i)*2; end
The single program multiple data (spmd) construct in the toolbox lets you define a block of code that runs in parallel on all the labs (workers) in the MATLAB pool.
Code changes
For code to benefit from parallel execution, iterations shouldn't have to run in sequence or depend on each other. Code can sometimes be re-written to make this so - you might even be able to get rid of loops completely. parfor tries to alert you when your code will get the wrong answers when parallelised, but doesn't always succeed.
Other Options
You might even be able to use a graphics accelerator board to speed matlab up. Options include MATLAB plug-in for CUDA (CUDA is a library that uses an nVidia board)
See Also
- Parallel MATLAB with openmp mex files (Mike Croucher and David Szotten,)
- MatlabMPI (from MIT)
- pMatlab: Parallel Matlab Toolbox (from MIT)
- multicore (Matlab Central)
- Parallel Computing Toolbox (Mathworks)
- Distributed Computing Server (Mathworks)
- Improving Optimization Performance with Parallel Computing (Mathworks)
- Batch Job (Matlab Central)