Why is my simulation signal logging output is empty in parfor loop (but not in for loop)?

7 views (last 30 days)
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?

Answers (2)

Walter Roberson
Walter Roberson on 17 May 2016
You need to index logsout by your loop control variable for MATLAB to recognize that it is a sliced output variable.
  2 Comments
Robert
Robert on 17 May 2016
Thanks for your response!
I should have been more clear. I look for a particular signal in logsout and count the instances for which it is true using nnz. I keep a running sum of these as the output of my loop. I don't need to save simout beyond the end of the iteration.
When I try to access that signal in the parfor loop, I get an error because simout.get('logsout') returns [].
Walter Roberson
Walter Roberson on 28 Jun 2016
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.

Sign in to comment.


Tanguy
Tanguy on 1 Jun 2017
Edited: Tanguy on 1 Jun 2017
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

Categories

Find more on Configure and View Diagnostics in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!