Problem with using spmd in the objective function of fmincon
Show older comments
I am experiencing trouble with using spmd in the objective function of fmincon. My code is as follows:
load('outervariables.mat');
theta1 = zeros(size(outstruct.x1, 2),1);
%% load data to workers here.
spmd
data = load(['dataworker' num2str(labindex) '.mat']);
end
theta20 = sig.x;
%objective function:
fun = @(theta2)gmmobj(theta2, outstruct, data);
options = optimoptions('fmincon', ...
'SpecifyObjectiveGradient',true, 'Display', 'off', ...
'MaxFunctionEvaluations', 2, ...
'OptimalityTolerance', 1e-14, 'FunctionTolerance', 1e-14);
[x, fval] = fmincon(fun, theta20, [], [], [], [], [], [], [], options);
The objective function gmmobj() is coded as
function [f, grad] = gmmobj(theta2, outstruct, data)
spmd
[mktmval, jac] = meanutil_jacEQN(theta2, data.struct);
end
delta = cell2mat(mktmval(:));
jacobian = cell2mat(jac(:));
clear mktmval jac;
% the following deals with cases were the min algorithm drifts into region where the objective is not defined
if max(isnan(delta)) == 1
f = 1e+10
else
temp1 = outstruct.x1'*outstruct.IVs;
temp2 = delta'*outstruct.IVs;
theta1 = (temp1/outstruct.W*temp1')\(temp1/outstruct.W*temp2');
%theta1 = (temp1*temp1')\(temp1*temp2');
clear temp1 temp2
gmmresid = delta - outstruct.x1*theta1;
temp1 = gmmresid'*outstruct.IVs;
f = temp1/outstruct.W*temp1';
%f = temp1*temp1';
clear temp1
%save gmmresid gmmresid
end
if nargout > 1
grad = 2*jacobian'*outstruct.IVs/outstruct.W*outstruct.IVs'*gmmresid;
end
disp(['GMM objective: ' num2str(f)])
end
When I call the gmmobj() function on its own, it runs fine. But once I start running the optimization with "fmincon", I get the following output and error:
GMM objective: 36532.679
Error using gmmobj (line 13)
Error detected on workers 1 2 3 4 5 6.
Error in @(theta2)gmmobj(theta2,outstruct,data)
Error in barrier
Error in fmincon (line 834)
[X,FVAL,EXITFLAG,OUTPUT,LAMBDA,GRAD,HESSIAN] = barrier(funfcn,X,A,B,Aeq,Beq,l,u,confcn,options.HessFcn, ...
Caused by:
Error using levenbergMarquardt (line 16)
Objective function is returning undefined values at initial point. fsolve cannot continue.
Note that line 13 of the "gmmobj" function is where the "spmd" block starts. The gmmobj() function was obviously evaluated once, but on the second iteration, fmincon doesn't seem to recognize the spmd block. Can someone help me with making this work?
5 Comments
Walter Roberson
on 27 Aug 2020
By the way,
if max(isnan(delta)) == 1
can be written as
if any(isnan(delta))
Walter Roberson
on 27 Aug 2020
Please post meanutil_jacEQN and your .mat for testing.
Claire Fang
on 27 Aug 2020
Walter Roberson
on 27 Aug 2020
Could you explain more why you are doing
spmd
[mktmval, jac] = meanutil_jacEQN(theta2, data.struct);
end
You are not (in any obvious way) using different data depending upon iterations or spmd lab number, so the same calculation would seem to be carried out on all of the workers.
Claire Fang
on 28 Aug 2020
Accepted Answer
More Answers (2)
Matt J
on 28 Aug 2020
Clearly, the evaluation of this line was unsuccessful due to an error within meanutil_jacEQN
[mktmval, jac] = meanutil_jacEQN(theta2, data.struct);
My recommendation would be to replace, for debugging purposes, the spmd line with an equivalent for-loop implementation
for i=1:numel(data.struct)
[mktmval{i}, jac{i}] = meanutil_jacEQN(theta2, data{i}.struct);
end
That way you can use the debugger to observe the cause of the error.
9 Comments
Claire Fang
on 28 Aug 2020
Edited: Claire Fang
on 28 Aug 2020
Matt J
on 28 Aug 2020
The idea is to replace the parallel spmd with an equivalent calculation that can be run serially. Please make the fixes necesary to get this working.
data=cell2mat(data(:));
for i=1:numel(data)
[mktmval{i}, jac{i}] = meanutil_jacEQN(theta2, data(i).struct);
end
Claire Fang
on 28 Aug 2020
Matt J
on 28 Aug 2020
If it runs fine in a serial for- loop, I would try replacing the ordinary for-loop with a parfor loop. This should avoid whatever issues that your meanutil_jacEQN function is having with spmd composite data types.
Claire Fang
on 28 Aug 2020
Matt J
on 28 Aug 2020
Using parallel.pool.Constant
Claire Fang
on 28 Aug 2020
Matt J
on 28 Aug 2020
Very strange then, that you didn'st see that in the serial version...
Claire Fang
on 28 Aug 2020
Claire Fang
on 28 Aug 2020
0 votes
Categories
Find more on Parallel Computing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!