Semaphores and locks in MATLAB

Amelio Vazquez-Reina picture Amelio Vazquez-Reina · Jun 20, 2011 · Viewed 7.7k times · Source

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:

  1. MAT_1 processes images sequentially, reading them one by one using imread, and outputs the result for each image using imwrite.
  2. 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:

  1. How would you approach this problem? Do I need to use semaphores or locks to prevent race conditions?
  2. Does MATLAB provide any mechanism to lock files? (i.e. something similar to 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?

EDIT :

  • As @yoda points out below, the Parallel Computing Toolbox (PCT) allows for blocking calls between MATLAB workers, which is great. That said, I am particularly interested in solutions that do not require the PCT.
  • 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.

Answers :

  • For a solution that allows for the implementation of semaphores but does not rely on the PCT see Jonas' answer below
  • For other good approaches to the problem, see Yoda's answer below

Answer

Andrew Smart picture Andrew Smart · Apr 5, 2014

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