Parallel Simulation VERY slow?
Show older comments
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
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 'TransferBaseWorkspaceVariables”option 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 how to run parallel simulation, refer the following MATLAB documentation, https://www.mathworks.com/help/simulink/ug/running-parallel-simulations.html
- To reduce the simulation time without using ‘parsim’, consider using ‘Performance Advisor’. Refer the following MATLAB documentation for more information, https://www.mathworks.com/help/simulink/ug/consult-the-performance-advisor.html
For more information on ‘parsim’ and multiple model simulation, refer the following MATLAB documentation,
- https://www.mathworks.com/help/parallelcomputing/parpool.html#d126e79413
- https://www.mathworks.com/help/simulink/slref/parsim.html#bvnh103-2
- https://www.mathworks.com/help/simulink/ug/optimize-estimate-and-sweep-block-parameter-values.html
Hope this is helpful.
7 Comments
Dimosthenis Michaloudis
on 12 Dec 2023
Infinite_king
on 12 Dec 2023
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 ?
Dimosthenis Michaloudis
on 12 Dec 2023
Infinite_king
on 13 Dec 2023
"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?
Dimosthenis Michaloudis
on 13 Dec 2023
Infinite_king
on 14 Dec 2023
I suggest you contact MathWorks support, https://www.mathworks.com/support/contact_us.html
Dimosthenis Michaloudis
on 14 Dec 2023
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!