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’);
tocI 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 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 matlab, parallel computing toolbox, simulink, simulation MATLAB Answers — New Questions