How to use properly parfor?
2 views (last 30 days)
Hello, I'm try to use "parfor" in order to execute 2 parallel actions: write frames to buffer & read frames from buffer.
My code is as follows:
[s,f] = bufferFUNC(i,vid,fps);
vid is my videoinput object. fps is the predefined FrameRate.
my bufferFUNC is as follows:
function [s,f] = bufferFUNC(i,vid,fps)
if i == 1 % read data from buffer
[pos,~,~,~] = Frames2Sound(bufferFrames,fps,'FALSE','FALSE','TRUE','FALSE');
[s , f , ~] = spectrogram(pos(1,:),kaiser(64,7), 62 , 64 , fps);
if i == 2 % write data into buffer
frames = peekdata(vid, get(vid,'FramesAvailable'));
bufferFrames = squeeze(frames);
For some reason, when running in parfor configuration, I receive the following error:
Error using bufferFUNC (line 11)
An UndefinedFunction error was thrown on the workers for 'start'. This might be because the file containing 'start' is not
accessible on the workers. Use addAttachedFiles(pool, files) to specify the required files to be attached. See the
documentation for 'parallel.Pool/addAttachedFiles' for more details.
Undefined function 'start' for input arguments of type 'struct'.
Any idea why this is happening? If I ran the same script but with regular for then no problem... Why the fact that I'm using parfor causing MATLAB not to recognize the function "start"???
Edric Ellis on 25 Feb 2016
I think you need to create vid on the workers, I suspect the object does not correctly transfer from the client to the workers because they are separate MATLAB processes. You could try doing something like:
parfor idx = 1:4
vid = videoinput(...);
% use 'vid'
If that works, you can use parallel.pool.Constant (or WorkerObjWrapper if you're not using R2015b), something like this:
vidWrapper = parallel.pool.Constant(@() videoinput(...), @delete);
parfor idx = 1:2
vid = vidWrapper.Value;
% use 'vid'
% Note that when 'vidWrapper' goes out of scope, 'delete' will be invoked
% on the object that was created.