Running multiple simulations with Simulink and data dictionaries using parfor (NOT parsim)

Hello,
I want to run some simulations using Simulink via MATLAB. The number of simulations is quite big and thus I have thought of using parfor for this exercise. I don't want to use parsim, because I have created a class interface with the Simulink model and its data (in data dictionaries) which I want to utilize and maintain in my workflow.
To be more specific, I have a Simulink model composed of model references and multiple data dictionaries and I am able, through the simulation model class interface that I have created, to run it, extract KPIs from each simulation and also change its data dictionary parameters.
I will focus in the commands that I have been using to change the parameters and run simulation inside the simulation model interface class and the code that I have been trying to make functional for parfor before I delve into the struggles that I am currently facing.
So the interface class contains several properties like the Model Name, the simulation parameters (which is just a snapshot of the data dictionary for the model) and simulation output which contains the simulation KPIs.
For setting the data dictionary parameters, I have created a method that essentially uses the following basic commands:
dictionaryObj = Simulink.data.dictionary.open(dictionaryPath);
sectionObj = getSection(dictionaryObj,'Design Data');
setValue(getEntry(sectionObj,paramName),value);
close(dictionaryObj);
The user just selects the paramName and the value to update the data dictionary.
The simulation is run by the appropriate method which essentially includes the
sim
command.
Using the above class, I am able to run successfully simulations using a for loop to iterate through the different parameters that I want to change for each simulation scenario. However, when I replace this for loop with a parfor I am seeing a very strange behaviour in my results.
The code that I have been using thus far looks like the following:
%... create the different data for the different simulation scenarios...
parpool('local')
parfor ii = 1:numel(numSimulations)
if bdIsLoaded('Name of simulation model')
close_system('Name of simulation model',0); %To ensure that there is no overlap between each simulation from each worker
end %if
load_system('Name of simulation model');
dataDictionaryPath = 'path to the parent data dictionary';
% Close parent data dictionary before any change
if exist(dataDictionaryPath,'file')
Simulink.data.dictionary.closeAll('-discard');
else
error('Data dictionary not found!')
end %if
% Instantiate the simulation model interface
localObj = InterfaceModel('Name of simulation mode');
try
localObj = localObj.changeParameter('name of parameter in the data dictionary',[A(1,ii),B(1,ii),C(1,ii)]);
localObj = localObj.runSimulation();
catch
error('Error!')
end %try-catch
logObjects{i} = localObj;
end %parfor
If the following code is run in a for loop it runs as expected; looping through all the different parameters and the logObjects contains all the results. If I though put it inside the parfor, I seem to get the same results for all iterations. I tried printing the A or B or C value using the send and the afterEach functions and it seems like its properly iterating through the data that define the simulation scenarios. However, I am confused on the process to update data dictionaries and run simulink simulations inside a parfor loop. I believe the issue is coming from the way that values of the data dictionary/simulink model copies are shared among the different workers.
Could someone indicate to me possible causes of this problem and provide me with an example of running simulink simulations with data dictionaries using a parfor loop?
Many thanks!

 Accepted Answer

I guess the answer can be found in Simulink.data.dictionary.setupWorkerCache and spmd.
This contains an example of how to properly setup the workers to access the data dictionary, eliminating any issues when sharing copies of the data dictionary object.

More Answers (0)

Categories

Asked:

on 4 Dec 2024

Answered:

on 5 Dec 2024

Community Treasure Hunt

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

Start Hunting!