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 endend
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 largeObjFn_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 endend
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