Why is my simulation signal logging output is empty in parfor loop (but not in for loop)?
조회 수: 3 (최근 30일)
이전 댓글 표시
I am running a simulation in Rapid Accelerator mode with the intention of running it in a parfor loop. I am running MATLAB r2015a (64 bit) on Windows 7.
Following the Simulink documentation for rapid simulation in a parfor loop, I build the model with
load_system(myModel)
rtp = Simulink.BlockDiagram.buildRapidAcceleratorTarget(myModel);
Then I duplicate rtp with repmat and alter the relevant parameters in each.
rtp = repmat(rtp,numRuns,1);
for ii = 1:numRuns
% Change the parameters in rtp(ii)
end
I run the model with the sim command inside the loop
(par)for ii = 1:numRuns
simout = sim(myModel,'SimulationMode','rapid',...
'RapidAcceleratorUpToDateCheck','off',...
'RapidAcceleratorParameterSets',rtp(ii))
logsout = simout.get('logsout')
% Do some analysis on the data in logsout
end
If I use a for loop, logsout is the Simulink.SimulationData.Dataset that I expect. If I use a parfor loop, logsout is an empty array.
I don't have a model I can share that duplicates this behavior, and the documentation doesn't mention it so I expect it doesn't happen for all models. Have other users experienced this? Is there something I should have set to allow signal logging in the parfor loop?
댓글 수: 0
답변 (2개)
Walter Roberson
2016년 5월 17일
You need to index logsout by your loop control variable for MATLAB to recognize that it is a sliced output variable.
댓글 수: 2
Walter Roberson
2016년 6월 28일
I suggest you experiment by pushing the bulk of the code in the loop into a function. When sim() is invoked then To Workspace by default writes into the workspace of the function that invoked sim(), or to the base workspace if sim() is invoked outside of any function (including if it is invoked from the command menus.) If I recall correctly there is also an option to configure To Workspace to write to the base workspace, which is effectively a form of global variable and the value of global variables (including the base workspace) are not copied back and forth to parfor workers. Likewise if you have a From Workspace that is expecting to get a value from the workspace of the function or the base workspace, that is not going to work in parfor (except for values you wrote there with To Workspace for the same worker.)
Anyhow, pushing the code into a function will provide a clear workspace delineation for sim() which can help to reduce confusion.
Tanguy
2017년 6월 1일
편집: Tanguy
2017년 6월 1일
Hi all,
I had the exact same bug (empty output in parfor loop). It appeared that parfor loop doesn't use the same pathdef than for loop... And a buggy function in shadow was used instead (two functions with the exact same name in my Matlab path). I had this bug with Matlab R2016b . linux.
Hope it can help
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!