Help with Grid search using parallel computation (substituting for loops using 'parfor')

3 次查看(过去 30 天)
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!

采纳的回答

Walter Roberson
Walter Roberson 2019-4-17
编辑:Walter Roberson 2019-4-17
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.
  2 个评论
Srajal Nayak
Srajal Nayak 2019-4-17
Hello Walter,
Thanks a lot for your answer. This works perfectly! Also, thanks for pointing me to the reduction variable link. It was helpful in understanding the things that parfor won't allow me to do. I appreciate your help.
Cheers!
Walter Roberson
Walter Roberson 2019-4-17
Ah, I was wrong, it does support min() reduction -- but it would probably still be awkward to figure out how to record the variables.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Parallel for-Loops (parfor) 的更多信息

产品


版本

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by