Matlab clears persistent and global variables in fmincon when activating UseParallel

1 次查看(过去 30 天)
It seems matlab clears all global and persistent variables within the objective function when using "UseParallel" in fmincon. I am trying to track each function call within the objective function (this is because my objective function should generate a specific input file to be called by an exteranl program and give a misfit output).
I use the folowing code to within my objective function to generate the unique file name at each iteration:
persistent count_1
if isempty(count_1)
count=0;
else
t = getCurrentTask();
worker_number = t.ID;
c = worker_number * 10000 + count_1;
count =c;
count_1=count_1+1;
end
count_1=count_1+1;
filename = ['Run_' num2str(cout) '.dat'];
But strangly, the name would not change after the first function call. Have you encoutered this issue?

采纳的回答

Matt J
Matt J 2021-4-6
编辑:Matt J 2021-4-6
This happens because, in parallel computing, it isn't easily definable what it means for a variable to "persist". A peristent variable is normally one that retains its value from the previous function call. But when the objective function calls are all executing simultaneously (or at least non-sequentially), what does "previous" even mean?
With global variables, similar ambiguities arise. What if two parallel-occuring executions of the objective function assign different values to a global variable G that they share with the base workspace? Which of the conflicting values should reside in the base workspace version of G after the execution is complete?
  4 个评论
Matt J
Matt J 2021-4-7
编辑:Matt J 2021-4-7
Another option might be to generate output files in an OutputFcn,
This lets you detect which particular sub-stage of the iteration the code is currently in, and generate some sort of customized output accordingly.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Systems of Nonlinear Equations 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by