Preamble
I have put a lot of effort into this contribution to Matlab Central. As I
used the code successfully for myself for quite some time, I am sure that it can
be rather valuable for the one or the other. If you find any errors or bugs,
if you have problems in using the function or if you find the documentation
insufficiently detailed:
Please contact me and give me the chance to help you before giving a bad
rating on Matlab Central!
Contact details at the bottom of this page.
Discussion group
Please take the chance to discuss with other users about the package in the discussion group:
http://groups.yahoo.com/group/multicore_for_matlab/
Thanks
I have spent many hours to develop this package. If you would like to let me know that you appreciate my work, you can do so by leaving a donation.
Introduction
The latest Matlab releases (starting with R2007a) include support for multiple
cores. However, Matlab will only take advantage of multiple cores in evaluating
certain computations like FFT or FIR filtering operations. Matlab will never
be able to determine if, for example, consecutive function calls in a for-loop
are independent of each other.
With this package, I provide some MATLAB-functions realizing parallel processing
on multiple cores on a single machine or on multiple machines that have access
to a common directory.
If you have multiple function calls that are independent of each other and you
can reformulate your code as
resultCell =
cell(size(parameterCell));
for k=1:numel(parameterCell)
resultCell{k} = myfun(parameterCell{k});
end
then, replacing the loop by
resultCell =
startmulticoremaster(@myfun, parameterCell);
allows you to evaluate your loop in parallel. All you need to do is to
start as many additional Matlab sessions as you
want slaves to work and to run
startmulticoreslave;
in those additional Matlab sessions. For example, if you want to evaluate a loop in
parallel with four cores, the function
startmulticoremaster.m must be started in one Matlab session, and the function
startmulticoreslave.m in three other
Matlab sessions.
Please refer to the help comments in file startmulticoremaster.m for possible
input arguments and more details about how the parallelization works.
No special toolboxes are used, no compilation of mex-files is necessary,
everything is programmed in plain and platform-independent Matlab. If one of
your slave sessions dies - don't care, the master session will go on working on
the given task.
Note: The Matlab multithreading capability
(R2007a and higher) might terminate all the advantage gained by using the multicore
package. So make sure that you UNcheck "Enable multihreaded
computation" under File/Preferences/General/Multithreading in all
involved Matlab sessions.
Quick start
For a quick start, first unzip all files into a directory, start Matlab and change
to this directoy. Then have a look at the heavily commented demo function multicoredemo.m. Start multicoredemo.m
in one Matlab session and startmulticoreslave.m in
another Matlab session.
Keywords
Parallel processing, distributed computing, multiple cores.
Functions contained in this package
startmulticoremaster.m
Takes the function handle (or a cell array of function handles) and the
parameters and computes the output. Files containing information about which
function to execute with which parameters are saved in a directory and read
by the slaves. The results saved by the slaves are later read by the master.
startmulticoreslave.m
Loads the files generated by function startmulticoremaster.m and evaluates
the given function. Results are saved in files in the same directory.
multicoredemo.m and testfun.m
To get into the code, have a look at the demo. You can start it without input
arguments for a demo on a multi-core machine, or with a common directory to
use for a multi-machine demo.
setfilesemaphore.m, removefilesemaphore.m and deletewithsemaphores.m
A crucial thing is to avoid that several Matlab processes try to open/delete/write
a file at the same time. Using a simple semaphore technique, exclusive file
access is guaranteed. Also here, no platform-dependent or C-programmed stuff is used.
existfile.m, existfile.c
Check if a file exists. To use the faster mex-file, type
"mex -setup",
select the builtin Lcc compiler and type
"mex existfile.c"
to compile the file. However, the package also works without using the mex-file.
findfiles.m
Returns a list of file names matching a given specification in a cell array.
getusername.m, gethostname.m
Return the user and host name.
tempdir2.m
Return the name of a temporary directory.
datenum2.m, translatedatestr.m
Compensate for some shortcomings in Matlab regarding date conversion.
And some more ...
Known Issues
If processing is cancelled by the user (Ctrl-C), catching Matlab just in saving/deleting
a temporary file, you might get some file access warnings. This is because Matlab
seems not to release the file correctly (at least under Windows). Besides some annoying
warnings, the multicore package should work correctly anyway. To get rid of the warning
messages, you will have to restart the right Matlab process before the file can be deleted.
The communication between the different Matlab sessions, which is done by saving/loading
temporary files into a common directory, causes some overhead. Thus, if your function calls
only need fractions of a second, the overhead may eat the advantage of the parallelization.
However, the multicore package can do several function evaluations after each other in every
process before communicating again. In this way the overhead can be minimized. As a rule
of thumb, the time needed for a bunch of function executions should not be lower than 2
seconds. If this is the case in your application, the MULTICORE package might not be of any
help to you.
Contact
Dr.-Ing. Markus Buehren
Stuttgart, Germany
http://www.markusbuehren.de
http://groups.yahoo.com/group/multicore_for_matlab/
Version
Last modified 05.07.2011
Latest version on Matlab Central.