Run Simulink Model via parsim() and save after each simulation
I am trying to run my simulink model in parallel while saving the results to a matfile after each simulation has completed.
Each worker should take the following action:
Simulate model with simIn( j )
Create matfile for above simulation
Repeate with simIn ( K ) until all simulations are complete.
The below code is my starting point. "test_model" is a simulink model of only a sin wave going into a gain, integrator, and to workspace block simply to test out parsim().
%% Example of how to run simulink models in parallel
clear; close all; clc;
mdl = "test_model";
amp= 1:10;
freq = 1:10;
nSims = length(amp);
simIn(1:nSims) = Simulink.SimulationInput(mdl);
% Setup model workspace
k = 5;
for i =1:nSims
nm = [num2str(i) ‘.mat’];
simIn(i) = setVariable(simIn(i), "amp", amp(i), ‘Workspace’, mdl);
simIn(i) = setVariable(simIn(i), "freq", freq(i), ‘Workspace’, mdl);
simIn(i) = setPostSimFcn(simIn(i), @(x)postSim(x, simIn(i), nm));
end
out = parsim (simIn, ‘UseFastRestart’,’on’, ‘TransferBaseWorkspaceVariables’,’on’);
function postSim(out, in, nm)
% postSim – Run @ completion of each simulation
% Saves simulation input, output, and name to file "nm"
m = matfile(nm, ‘Writable’, true);
m.out = out;
m.in = in;
m.nm = nm;
end
I understand that save() does not work as expected in parallel, so I’ve implemented the example from the question below as a method of creating my mat files. The above code works exactly as expected if "parsim" is replaced with "sim"; however, no files are created when using "parsim".
https://www.mathworks.com/matlabcentral/answers/135285-how-do-i-use-save-with-a-parfor-loop-using-parallel-computing-toolbox
Note: setting ‘TransferBaseWorkspaceVariables’ to ‘off’ and setting k via setVariable does not have any impact on this behavior.
Ask: How can I modify the above code to also work with the parsim command?I am trying to run my simulink model in parallel while saving the results to a matfile after each simulation has completed.
Each worker should take the following action:
Simulate model with simIn( j )
Create matfile for above simulation
Repeate with simIn ( K ) until all simulations are complete.
The below code is my starting point. "test_model" is a simulink model of only a sin wave going into a gain, integrator, and to workspace block simply to test out parsim().
%% Example of how to run simulink models in parallel
clear; close all; clc;
mdl = "test_model";
amp= 1:10;
freq = 1:10;
nSims = length(amp);
simIn(1:nSims) = Simulink.SimulationInput(mdl);
% Setup model workspace
k = 5;
for i =1:nSims
nm = [num2str(i) ‘.mat’];
simIn(i) = setVariable(simIn(i), "amp", amp(i), ‘Workspace’, mdl);
simIn(i) = setVariable(simIn(i), "freq", freq(i), ‘Workspace’, mdl);
simIn(i) = setPostSimFcn(simIn(i), @(x)postSim(x, simIn(i), nm));
end
out = parsim (simIn, ‘UseFastRestart’,’on’, ‘TransferBaseWorkspaceVariables’,’on’);
function postSim(out, in, nm)
% postSim – Run @ completion of each simulation
% Saves simulation input, output, and name to file "nm"
m = matfile(nm, ‘Writable’, true);
m.out = out;
m.in = in;
m.nm = nm;
end
I understand that save() does not work as expected in parallel, so I’ve implemented the example from the question below as a method of creating my mat files. The above code works exactly as expected if "parsim" is replaced with "sim"; however, no files are created when using "parsim".
https://www.mathworks.com/matlabcentral/answers/135285-how-do-i-use-save-with-a-parfor-loop-using-parallel-computing-toolbox
Note: setting ‘TransferBaseWorkspaceVariables’ to ‘off’ and setting k via setVariable does not have any impact on this behavior.
Ask: How can I modify the above code to also work with the parsim command? I am trying to run my simulink model in parallel while saving the results to a matfile after each simulation has completed.
Each worker should take the following action:
Simulate model with simIn( j )
Create matfile for above simulation
Repeate with simIn ( K ) until all simulations are complete.
The below code is my starting point. "test_model" is a simulink model of only a sin wave going into a gain, integrator, and to workspace block simply to test out parsim().
%% Example of how to run simulink models in parallel
clear; close all; clc;
mdl = "test_model";
amp= 1:10;
freq = 1:10;
nSims = length(amp);
simIn(1:nSims) = Simulink.SimulationInput(mdl);
% Setup model workspace
k = 5;
for i =1:nSims
nm = [num2str(i) ‘.mat’];
simIn(i) = setVariable(simIn(i), "amp", amp(i), ‘Workspace’, mdl);
simIn(i) = setVariable(simIn(i), "freq", freq(i), ‘Workspace’, mdl);
simIn(i) = setPostSimFcn(simIn(i), @(x)postSim(x, simIn(i), nm));
end
out = parsim (simIn, ‘UseFastRestart’,’on’, ‘TransferBaseWorkspaceVariables’,’on’);
function postSim(out, in, nm)
% postSim – Run @ completion of each simulation
% Saves simulation input, output, and name to file "nm"
m = matfile(nm, ‘Writable’, true);
m.out = out;
m.in = in;
m.nm = nm;
end
I understand that save() does not work as expected in parallel, so I’ve implemented the example from the question below as a method of creating my mat files. The above code works exactly as expected if "parsim" is replaced with "sim"; however, no files are created when using "parsim".
https://www.mathworks.com/matlabcentral/answers/135285-how-do-i-use-save-with-a-parfor-loop-using-parallel-computing-toolbox
Note: setting ‘TransferBaseWorkspaceVariables’ to ‘off’ and setting k via setVariable does not have any impact on this behavior.
Ask: How can I modify the above code to also work with the parsim command? simulink, parallel computing toolbox MATLAB Answers — New Questions