Parallel Simulation VERY slow?

I have created a Simulink file which runs in 99 seconds. I decided to use parsim in matlab to make it even faster. I used the time step required for the workers to start in different time (and use specific data) and run this faster, but instead it requires 5100 seconds (~85 minutes). My guess would be that each worker uses ALL the data, and possibly runs the entirety of the simulation.
Below is the code I created. What do I need to change so that I can make it faster, or at least working properly, meaning that each worker will use the specified data (for example data between 0.04-0.08) and run this specific time.
% Create a parallel pool if not already available
if isempty(gcp('nocreate'))
parpool; % Create a parallel pool
end
% Load Model
model = 'zigzag_example_matlab';
load_system(model);
% Sweep parameters
sweep = (0.00:0.04:3.84);
numSims = numel(sweep);
% Preallocate SimulationInput array
simIn = Simulink.SimulationInput.empty(numSims, 0);
% Cell array of variable names
variableNames = {'From Workspace', 'From Workspace1', 'From Workspace2', 'From Workspace3', 'From Workspace4', ...
'From Workspace5', 'From Workspace6', 'From Workspace7', 'From Workspace8', 'From Workspace9', ...
'From Workspace10', 'From Workspace11'};
% Create an array of SimulationInput objects and specify the sweep value for each simulation
for idx = 1:numSims
simIn(idx) = Simulink.SimulationInput(model);
% Set 'SampleTime' for each variable
for varIdx = 1:numel(variableNames)
variablePath = sprintf('zigzag_example_matlab/%s', variableNames{varIdx});
simIn(idx) = simIn(idx).setBlockParameter(variablePath, 'SampleTime', num2str(sweep(idx)));
end
end
% Simulate the model
tic
simOut = parsim(simIn, 'TransferBaseWorkspaceVariables', 'on', 'ShowSimulationManager', 'on');
toc

Answers (1)

Hi Dimosthenis Michaloudis,
I understand that you are attempting to utilize the 'parsim' function to run simulations in parallel, aiming to reduce the simulation time. However, it appears that the simulation time using 'parsim' is significantly higher than the normal simulation time.
The 'parsim' function was employed to simulate multiple models specified in the 'SimulationInput' object concurrently. While 'parsim' can effectively reduce the simulation time, it is crucial to structure the model appropriately. Specifically, the simulation should not rely on the results of previous steps. In other words, there should be no dependency between the simulations of the model at different time steps.
Assuming the model is suitable for simulation in parallel, follow the below steps to reduce the runtime,
  • Make sure ‘Parallel Computing Toolbox’ (PCT) is installed, since ‘parsim’ requires PCT to utilize parallelism.
  • In the following line, avoid using 'TransferBaseWorkspaceVariables' option to reduce simulation time. The 'TransferBaseWorkspaceVariablesoption is not recommended for large scale simulations.
simOut = parsim(simIn, 'TransferBaseWorkspaceVariables', 'on', 'ShowSimulationManager', 'on');
  • The following line will not result in simulation starting from the specified time as you would have expected, instead this will change the ‘SampleTime’ for the entire model. Review this line and made necessary changes
simIn(idx) = simIn(idx).setBlockParameter(variablePath, 'SampleTime', num2str(sweep(idx)));
  • Experiment with the number of workers in your parallel pool to find the optimal configuration.
parpool('local', N); % Specify the number of workers, e.g., parpool('local', 4);
For more information on ‘parsim’ and multiple model simulation, refer the following MATLAB documentation,
  1. https://www.mathworks.com/help/parallelcomputing/parpool.html#d126e79413
  2. https://www.mathworks.com/help/simulink/slref/parsim.html#bvnh103-2
  3. https://www.mathworks.com/help/simulink/ug/optimize-estimate-and-sweep-block-parameter-values.html
Hope this is helpful.

7 Comments

Hi Infinite_king,
you were quite helpful. I have reworked my code, although it seems I need the 'TransferBaseWorkspaceVariables' else errors keep appearing in each simulation. Nonetheless, the parsim is way slower than the simple sim command, which shouldn't be the case, correct?
% Create a parallel pool if not already available
if isempty(gcp('nocreate'))
parpool; % Create a parallel pool
end
% Load Model & Simulation Input
model = 'zigzag_example_matlab';
Simulink.BlockDiagram.buildRapidAcceleratorTarget(model);
%step time
Sweep = 0.00:0.04:3.84;
for i = length(Sweep):-1:1
simIn(i) = Simulink.SimulationInput(model);
simIn(i) = simIn(i).setVariable('SampleTime',Sweep(i));
end
%Simulation
tic
simout = parsim(simIn, 'TransferBaseWorkspaceVariables', 'on', 'ShowSimulationManager', 'on');
toc
Hi, it seems there might be a misunderstanding about how 'parsim' works. Can you tell me what the below for loop achieves or what are trying to achieve exactly ?
%step time
Sweep = 0.00:0.04:3.84;
for i = length(Sweep):-1:1
simIn(i) = Simulink.SimulationInput(model);
simIn(i) = simIn(i).setVariable('SampleTime',Sweep(i));
end
Essentially, you're running the same model in parallel but with different sample times. Is that your objective ?
Hi,
for each 0.04 step, the slx file's data change.
To be more specific: I have multiple demand loads in the slx file. One load demands 150 kW, the other 20 kW etc. That's for 0.04s, then the loads change. First one 130 kW, second 50 kW etc. And so on up until 3.84 s.
Each load data exists separately in the workspace in each own 96x2 table. First column is time, second one is kW.
What I try to achieve is that one worker simulates from 0.00s to 0.04s, the 2nd worker simulates 0.04s to 0.08s etc.
The results are correct, but the time is way too slow I think, so I tend to believe I am doing something quite wrong in the code writing.
"What I try to achieve is that one worker simulates from 0.00s to 0.04s, the 2nd worker simulates 0.04s to 0.08s etc'
I suspect that this is not happening. To simplify, 'parsim' will just simulate all the models given to it in parallel. However, in your for loop, you are setting the 'SampleTime' variable to a specific value. Can you answer this question: If you change the 'SampleTime' variable in your model and then run the simulation, does the model behave as expected? Specifically, does it start the simulation from the intended time, and does the simulation only process the data you anticipate it to take?
I've just run the simulation with different SampleTime, and the results were as expected. Again the time was slow though.
Thanks for the help Infinite_king,
I'll contact the support and send them the link here, in case it helps.

Sign in to comment.

Categories

Find more on Run Simulations 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!