Why is my parallel pool using undefined variables during a 'parsim' simulation?

I am trying to run series of simulations in parallel using function 'parsim' in Simulink Release R2022b and Parallel Computing Toolbox. My call to 'parsim' is as follows:
simOut = parsim(simIn, 'TransferBaseWorkspaceVariables', 'Off', 'AttachedFiles','Vars.mat',...
'SetupFcn', @()evalin('base','load batchVars.mat') );
where 'simIn' is the 'Simulink.SimulationInput' object containing the various models I would like to run, and 'Vars.mat' is a MAT-file containing the data I would like the simulations to use.
The issue is that the results of the simulations do not correspond to the input variables I am providing in 'Vars.mat'. The parallel pool seems to be ignoring them! I even tried not supplying a necessary variable inside 'Vars.mat' hoping that the simulations would error, but they completed successfully with results I cannot relate to the inputs I am providing.
From where are the parallel workers getting the variables they are using?

 Accepted Answer

It is unlikely that the parallel workers are using variables that are undefined to them. Those input variables must be defined somewhere for them. Given that the Name-Value argument 'TransferBaseWorkspaceVariables' is 'off' in the call to 'parsim', the workers will not be reading from the base workspace.
Here are some troubleshooting techniques to help you locate the issue:
  1. Run the models in series using the function 'sim'.
  2. Use a fresh parallel pool. When running several simulations on the same pool, the worker workspaces do not get cleared by default and data simply accumulates.
  3. Check the models' workspaces. You can find more information about them here: https://www.mathworks.com/help/releases/R2022b/simulink/ug/using-model-workspaces.html
  4. Examine the parallel workers' workspaces.
The workers' workspaces can be examined after creating a "diary" for them to record model parameters and settings before and after runs. This procedure uses functions 'setPreSimFcn' and 'setPostSimFcn' of the object 'Simulink.SimulationInput'. Please refer to the following MATLAB Answers post for more infromation:
Additionally, after the simulation is run and before you delete the parallel pool, you can run code for each worker using function 'spmd', documented here:

More Answers (0)

Categories

Products

Release

R2022b

Community Treasure Hunt

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

Start Hunting!