MATLAB: Help with Grid search using parallel computation (substituting for loops using ‘parfor’)

grid searchMATLABparallel computingparfor

Hello,
I am completely new to Matlab parallel computing. I am trying to implement a 4-dimensional grid search method for a non-linear objective function (it's kind of a least squares estimator problem, where my objective function captures the randomization error in an experiment). I've created a separate file 'ObjFn.m' where I define the objective function I want to minimize. I've attached a snippet from the code I wrote (which works correctly) below.
% Input variables -- fixed/given
a=6;
b=7;
c=5;
d=3;
p=0.5;
% w,x,y,z - variables I want to loop over to perform the grid search.
% w_hat, x_hat, y_hat, z_hat -- estimates which minimize the objective function.
% Initializing the minimum to be very large

ObjFn_min = 1e+10;
for y = 0:a
for w=0:b
for z=0:c
for x=0:d
if ObjFn(a,b,c,d,y,w,z,x,p) < ObjFn_min
ObjFn_min = ObjFn(a,b,c,d,y,w,z,x,p);
y_hat = y;
w_hat = w;
z_hat = z;
x_hat = x;
end
end
end
end
end
However, eventually I want to be able to compute the objective function for much larger grids than in the example above and hence plan on using the parallel computing features. I tried modifying the above code by replacing the first for-loop with a parfor-loop, as follows:
% Initializing the minimum to be very large
ObjFn_min = 1e+10;
parfor y = 0:a
for w=0:b
for z=0:c
for x=0:d
if ObjFn(a,b,c,d,y,w,z,x,p) < ObjFn_min
ObjFn_min = ObjFn(a,b,c,d,y,w,z,x,p);
y_hat = y;
w_hat = w;
z_hat = z;
x_hat = x;
end
end
end
end
end
I get the following error message:
"An UndefinedFunction error was thrown on the workers for 'ObjFn_min'. This might be because the file containing 'ObjFn_min' is not accessible on the workers.
Caused by: Undefined function or variable 'ObjFn_min'."
My understanding is that this is happening because 'ObjFn_min' here is a temporary variable which aren't allowed in parfor-loops. However, I am unable to think of a way to work around the problem. What should I change here so that the latter code with the one parfor-loop essentially gives me the same result as the first one with all four for-loops? Any help here would be appreciated.
Thanks for your help!

Best Answer

Effectively, you are trying to do a min() reduction. Unfortunately, min reductions are not supported even if you called min() directly, and are not recognized when you code in the manner you do.
You should create
N = a+1;
p_ObjFn_min = zeros(N,1);
p_y_hat = zeros(N,1);
p_w_hat = zeros(N,1);
and so on. Then inside your parfor, initialize
ObjFn_min = inf;
y_hat = nan;
w_hat = nan;
z_hat = nan;
x_hat = nan;
and then your for w loop. After the end of your for w loop,
p_Objfn_min(y+1) = Objfn_min;
p_y_hat(y+1) = y_hat;
p_w_hat(y+1) = w_hat;
p_z_hat(y+1) = z_hat;
p_x_hat(y+1) = x_hat;
and after the parfor loop, min(p_Objfn_min) finding the index and index the p_* variables at that index.