bfGetPlane within a Matlab parfor loop (for parallelization purposes)

Hi, I am running code on Matlab to detects particles on each frame of my .nd2 files.
My code looks like this:

reader = bfGetReader(fname);
for k = image_range
series1_plane1 = bfGetPlane(reader, k);
<do some computation on this frame, independently to the other frames>

In order to post-treat a given movie faster, or in order to process several movies at a time (without opening several instances of Matlab), I would like to use a parfor loop in Matlab. Thing is, the bfReader does not seem to be compatible with parfor use.
Does anyone have a solution (or at least a work-around that does not require to open additional instances of Matlab)?
Thank you!

Hi @LeChat,

You’re not alone. This is a fairly common request, see for example this thread on the previous forum, but…

You are right. The readers are not currently thread safe.

The solution is to create a reader for each thread. There are steps you can take to expedite this if you happen to be using a format which is slow to initialize, though that’s not likely the case for AVIs.

You can find an example of how to do this under (latest).


1 Like

What I usually do in this situation is load the frames into a cell array before the parfor and then distribute the loaded image data across the workers. If memory becomes an issue, it may be necessary to use parfeval to more directly manage jobs.