Clear Filters
Clear Filters

Parallel Simulation VERY slow?

19 views (last 30 days)
Dimosthenis Michaloudis
Dimosthenis Michaloudis on 5 Dec 2023
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)

Infinite_king
Infinite_king on 12 Dec 2023
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
Dimosthenis Michaloudis
Dimosthenis Michaloudis on 14 Dec 2023
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 Simulink Environment Customization 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!