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

11 views (last 30 days)
Robert on 17 May 2016
Edited: Tanguy on 1 Jun 2017
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
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)
I run the model with the sim command inside the loop
(par)for ii = 1:numRuns
simout = sim(myModel,'SimulationMode','rapid',...
logsout = simout.get('logsout')
% Do some analysis on the data in logsout
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.
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 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

Community Treasure Hunt

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

Start Hunting!