Parallel patternsearch and parallelization in objective function

4 views (last 30 days)
Hi all,
I am trying to use patternsearch to minimize a very expensive objective function. Inside the objective function, I call a legacy fortran executable with two separate sets of input text files, wait for it to complete, and read in the results from an output text file. The value of my objective function is a weighted average of those two separate evaluations. One evaluation takes approx. 10 minutes.
I have 4 parameters to determine through patternsearch, hence I need 8 cores for NUPS and useparallel=true.
However, I would like to parallelize the execution of my legacy fortran executable, too. I tried to simply create a 16 worker parallel pool and use a logic such as:
opts = optimoptions("patternsearch",UseParallel=true,Algorithm="nups", CompletePoll="on");
x0=[.9 .5 .2 .25];
fixed_para = [1 2 3 4 5 6 7]; %some fixed parameter values
xopt = patternsearch(@(x) objective_parallel(x),x0,opts);
function val = objective_parallel(x)
parfor i = 1:2
paraset(i,:) = [fixed_para , x]; %full set of parameters
%create unique subfolder
wrkr = getCurrentTask();
folder = [num2str(wrkr.ID),"_",num2str(i)];
if ~isfolder(folder)
copyfile('legacyfortran',folder)
end
%write input file for executable
fid = fopen([folder,'\input.txt'], 'wt');
fprintf(fid,'%s', paraset(i,:));
fclose(fid);
%execute the fortran code using input.txt in folder and wait for termination
system(['pushd ',folder,' && START /high /wait legacyfortran.exe && popd']);
%read in results
valtemp(i) = load(folder,'\fortranout.txt']);
end
val = 2.\(valtemp(1) + valtemp(2));
end
However, I am running into problems. Apparently, not all fortran codes were executed as output files are missing (the corresponding subfolder with input files and executables exist, however).
Error using poll/nups_poll/evaluate_parallel_or_vectorized
Unable to find file or directory '16_1\fortranout.txt'.
Error in poll/nups_poll (line 204)
evaluate_parallel_or_vectorized();
Error in poll (line 63)
nups_poll();
Error in pfminbnd (line 61)
[successPoll,nextIterate,optimState] = poll(FUN,X,Iterate,MeshSize,linConstr, ...
Error in patternsearch (line 294)
[X,FVAL,EXITFLAG,OUTPUT] = pfminbnd(FUN,X0,optimState,linConstr,Iterate, ...
Error in calibrate5g (line 143)
patternsearch(@(x) patternsearch(@(x) objective_parallel(x),x0,opts);
I hope the "minimal" working example explains the logic well enough.
Thank you for your help!
Florian
  2 Comments
Edric Ellis
Edric Ellis on 20 Jun 2023
As a first debugging step, you could try capturing any standard output from your system command, like this:
[status, output] = system(. . .);
outfile = fullfile(folder, 'fortranout.txt');
if ~isfile(outfile)
error('Output file %s is missing. Command status: %d, output:\n%s', outfile, status, output)
end
Florian
Florian on 20 Jun 2023
Thank you! That does makes a lot of sense. I do lose track of obvious sources of problems with the whole setup being a bit too legacy for my taste. (But it's not in my power to change that.)

Sign in to comment.

Answers (0)

Community Treasure Hunt

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

Start Hunting!