Find reason for error msg of Matlab parallel calculation: Variable 't0' has been deleted from base workspace.

9 ビュー (過去 30 日間)
Hi, all. Just starting doing parallel calculation with Matlab. Below are part of the code,
global t0 t1
......
......
t0 = 0;
t1 = 1;
CoreNum = 2;
parpool('local',CoreNum)
parfor counter = 1:2
uinput = upars_option(counter,:);
vinput = vpars_option(counter,:);
ruinput = ru_option(counter,:);
rvinput = rv_option(counter,:)
[uopt_rec,vopt_rec] = run_idp(uinput,vinput,ruinput,rvinput);
uopt_rec_mat(counter,:)= uopt_rec;
vopt_rec_mat(counter,:)= vopt_rec;
end
delete(gcp('nocreate'))% Close the calculation pool
Here upars_option, vpars_option, ru_option, and rv_option are all matrices recording initial guess values for the parameters. They are used row by row in function run_idp in each loop/calculation pool. Run_idp is a function which invokes several other functions to perform an algorithm. Results are recorded in matrices [uopt_rec, vopt_rec]. I run the run_idp with regular ‘for’ loop where parallel calculation is not involved. It turns out all right so function run_idp should be OK.
But run parallel calculation code pop out with an error msg,
Error using gridpt (line 10)
Error evaluating expression 't0' for 'StartTime' specified in the Configuration
Parameters dialog for block diagram 'input_check'.
Error in run_idp (line 65)
g_rec0 = gridpt(allu,allv);
Caused by:
Error using gridpt (line 10)
Unrecognized function or variable 't0'.
Error using gridpt (line 10)
Variable 't0' has been deleted from base workspace.
'gridpt' is a function which is invoked inside function 'run_idp'. ‘t0’ is defined as a global variable in all scripts involved, which is a variable that invoked by Simulink model input_check.slx. This ‘t0’ is assigned with a value before running parallel calculation(t0 = 0 in base workspace before even creating a calculation pool) and also defined and given a value in function gridpt. Part of the code of function ‘gridpt.m’ is shown as,
function out = gridpt(in1,in2)
global t0 t1 upars vpars tspan
g_rec0 = cell(3,5);
t0 = 0;
t1 = 1;
for i = 1:3
upars = in1(i,:);
vpars = in2(i,:);
out = sim('input_check'); % Line 10
……
……
end
Could you check what makes the error?
  2 件のコメント
Raymond Norris
Raymond Norris 2020 年 7 月 7 日
Hi,
Global variables are not permitted within parfor loops. Imagine parfor calls a function that modified the global variable. Which of the X workers wins? Instead, if feasible, consider passing values, in and out of the function.
I don't use Simulink, but have you seen parsim? Perhaps there's a lot more in your code then just gridpt, but might be worth looking into.
Raymond
Hainan Wang
Hainan Wang 2020 年 7 月 9 日
Hi, Raymond
Thank you so much! Your answer solved my question perfectly!
I am now loading parameters to model workspace, that avoiding the use of global variable.

サインインしてコメントする。

回答 (0 件)

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

製品


リリース

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by