fit options in a parfor loop

11 views (last 30 days)
I am performing a fit of data multiple times using a loop. When I use a "for" loop, everything works as expected. However, if I use a "parfor" loop, I get an error. If I remove the fit options (fo), then the parfor loop works as expected. Does anyone know why the parfor loop generates an error when I specify the fit method using the fitoptions?
% Generate some data
Npts = 50;
Ntime = 10;
tau = 10;
x = repmat( (1:Npts)',1,Ntime);
y = exp(-x/tau) + (rand(Npts,Ntime)-0.5)/10;
% Plot the data
figure;
plot(x(:,1),y(:,1),'-o')
title('Data for first time step')
% Fit options
ft = fittype('exp1');
fo = fitoptions( 'Method', 'NonlinearLeastSquares' );
% Pre-allocate the decay constant, tau
tau = NaN(1,Ntime);
parfor q = 1:Ntime
% Perform the exponential fit at time step q
fitresult = fit( x(:,q), y(:,q), ft, fo);
% Save the decay constant, tau
tau(q) = -1/fitresult.b;
end
  2 Comments
Walter Roberson
Walter Roberson on 14 Nov 2019
I confirm that the fo object is arriving empty on the worker, the way you would expect a global variable to look.
The work-around for the moment appears to be to assign fo inside the parfor loop.
Matt J
Matt J on 15 Nov 2019
Edited: Matt J on 15 Nov 2019
Also confirmed in R2018a. Possibly it's because fo is a handle object?
>> ishandle(fo)
ans =
logical
1
Handle objects are supposed to be cloned to the workers, but who knows if there are weird exceptions...

Sign in to comment.

Accepted Answer

Elizabeth Jones
Elizabeth Jones on 19 Nov 2019
Thanks all for the support. I ended up setting the fit method as a character string outside of the loop (near the top, where I usually keep such "manual/hard-coded" values), and then constructing the fitoptions inside the parfor loop.
% Generate some data
Npts = 50;
Ntime = 10;
tau = 10;
x = repmat( (1:Npts)',1,Ntime);
y = exp(-x/tau) + (rand(Npts,Ntime)-0.5)/10;
% Plot the data
figure;
plot(x(:,1),y(:,1),'-o')
title('Data for first time step')
% Fit options
fiteqn = 'exp1';
fitmethod = 'NonlinearLeastSquares';
% Pre-allocate the decay constant, tau
tau = NaN(1,Ntime);
parfor q = 1:Ntime
% Perform the exponential fit at time step q
ft = fittype(fiteqn);
fo = fitoptions( 'Method', fitmethod);
fitresult = fit( x(:,q), y(:,q), ft, fo);
% Save the decay constant, tau
tau(q) = -1/fitresult.b;
end

More Answers (2)

Matt J
Matt J on 15 Nov 2019
Edited: Matt J on 15 Nov 2019
A guess as to why this might be happening is that fitoptions objects don't seem to implement proper save/load behavior, as indicated by this small experiment,
>> fo = fitoptions( 'Method', 'NonlinearLeastSquares' );
>> save fo_file fo
>> S=load('fo_file');
>> isequal(S.fo,fo)
ans =
logical
0
This is signifcant, because parpools use save/load operations to clone objects to the workers, as is alluded here.
However, this does not explain why the version of fo that gets loaded on the workers ends up empty. Reloading a fitoptions object outside a parpool doesn't have that effect:
>> isempty(S.fo)
ans =
logical
0

Edric Ellis
Edric Ellis on 15 Nov 2019
Unfortunately, this is a limitation in the implementation of the fitoptions class. You can work around this either by constructing the fitoptions inside the parfor loop, or you can use parallel.pool.Constant, like this:
% Use the "function handle" form constructor of parallel.pool.Constant to
% ensure that the fitoptions object is constructed on the workers
fo_c = parallel.pool.Constant(@() fitoptions( 'Method', 'NonlinearLeastSquares' ));
parfor ...
...
% Extract the fitoptions from the Constant
fo = fo_c.Value;
% Perform the exponential fit at time step q
fitresult = fit( x(:,q), y(:,q), ft, fo);
end

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!