I am working on a MATLAB project where I would like to have two instances of MATLAB running in parallel and sharing data. I will call these instances MAT_1
and MAT_2
. More specifically, the architecture of the system is:
MAT_1
processes images sequentially, reading them one by one using imread
, and outputs the result for each image using imwrite
.MAT_2
reads the images output by MAT_1
using imread
and outputs its result somewhere else.One of the problems I think I need to address is to guarantee that MAT_2
reads an image output by MAT_1
once MAT_1
has fully finished writing to it.
My questions are:
flock
, but provided by MATLAB directly, and that works on multiple platforms, e.g. Windows & Linux). If not, do you know of any third-party library that I can use to build this mechanism in MATLAB?Why do I require MAT_1
and MAT_2
to run in parallel threads?:
The processing done in MAT_2
is slower on average (and more prone to crashing) than MAT_1
, and the output of MAT_1
feeds other programs and processes (including human inspection) that do not need to wait for MAT_2
to do its job.
I would approach this using semaphores; in my experience the PCT is unreasonably slow at synchronization.
dfacto (another answer) has a great implementation of semaphores for MATLAB, however it will not work on MS Windows; I improved on that work so that it would. The improved work is here: http://www.mathworks.com/matlabcentral/fileexchange/45504-semaphoreposixandwindows
This will be better performing than interfacing with Java, .NET, the PCT, or file locks. This does not use the Parallel Computing Toolbox (PCT), and AFAIK semaphore functionality isn't in the PCT anyway (puzzling that they left it out!). It is possible to use the PCT for synchronization but everything I'd tried in it was unreasonably slow.
To install this high-performance semaphore library into MATLAB, run this within the MATLAB interpreter: mex -O -v semaphore.c
You'll need a C++ compiler installed to compile semaphore.c into a binary MEX-file. That MEX-file is then callable from your MATLAB code as shown in the example below.
Usage example:
function Example()
semkey=1234;
semaphore('create',semkey,1);
funList = {@fun,@fun,@fun};
parfor i=1:length(funList)
funList{i}(semkey);
end
end
function fun(semkey)
semaphore('wait',semkey)
disp('hey');
semaphore('post',semkey)
end