# Notes on Matlab Parallel Programming

Recently, I am doing research which involve extensive computation with Matlab. Luckily, most of this computation can be done in a parallel fashion, and this can be instantly accomplished with the Matlab parallel toolbox. During using this toolbox, I summarized some notes that need to be remembered, to accelerate future work.

This post will be constantly updated during my use of Matlab parallel programming. Update log:

- Aug. 22, 2012 - matlabpool, parfor

### matlabpool

matlabpool command is essential if you want to use the Matlab parallel programming features. It opens several workers (several Matlab processes), and uses the current Matlab process as the coordinator to run parallel jobs. The structure is a star-shape, where the current process is the “supervisor” of the workers it manages. [1]

To use the command, simply typing matlabpool or
matlabpool open will do the job. This uses the default
configuration named “local”, which deploys *n* workers on the local
computer, where *n* is the number of cores of your CPU.

Always remember to close your workers before exiting Matlab. In my experience, if you do not close them, the processes will be killed, but the task remains. To close the Matlab pool, type matlabpool close or matlabpool close force. The latter is *strongly recommended* in case of accidentally unfinished or user-breaked tasks. If you do not free previous tasks in the Matlab pool, the performance of the current task will be affected, even if you’ve already exited Matlab once without closing the pool.

So, in my experience, it is recommended to always use:

matlabpool close force; matlabpool open;if you want to work with Matlab paralell programming efficiently (and locally).

The implementation of matlabpool involves network communication, serialization and deserialization, offering the possibility of using configurations other than “local” to work on remote or even multiple remote computers. I’ll cover that when it’s time for me to use one, perhaps.

### parfor

The Matlab paralell FOR-loop is perhaps its most common and easy-to-use paralell feature. To write a parfor loop, simply rewrite a for loop which has a loop variable that is a *integral and consecutive row vector*. More hint can be found in Matlab’s help documents [2] and [3].

One hint that I think important is: always keep your I/O variables SLICED where possible, or the communication overhead may be efficiency killer.

### References

*Unless otherwise mentioned, you are free to share my posts under the Creative Commons Attribution-ShareAlike 4.0 International License (CC BY-SA 4.0).*blog comments powered by Disqus