How to stop a parsim (parallel Simulink simulation) run from a worker when a certain condition occurs in R2022b?

6 次查看(过去 30 天)

In the context of conducting multiple parallel simulations using MATLAB's parsim function in R2022b, I am looking for a method to dynamically interrupt and halt the entire batch of simulations based on specific runtime conditions. Specifically, I need to stop the parallel execution when one of the simulations meets a certain criterion, such as reaching a specific value or condition within its simulation data. How can I implement a mechanism in MATLAB R2022b that monitors the simulations' progress and appropriately terminates the parallel pool when this predefined condition occurs, ensuring an efficient and controlled simulation environment?

采纳的回答

MathWorks Support Team
In MATLAB R2022b, you can halt a parallel Simulink simulation (parsim) from a worker based on specific conditions by leveraging the post-simulation function callback (setPostSimFcn) and a value store callback (ValueStore) associated with a parallel pool. This method allows for dynamic control over the simulation process, enabling the termination of the parallel pool when predefined conditions are met. Below is a step-by-step guide on how to implement this functionality:
Step 1: Prepare the Simulations
First, initialize your simulations with the necessary parameters and the post-simulation function. This function will check the condition to decide whether to stop the simulations.
mdl = 'simpleMdl';
numSims = 100;
simIn(1:numSims) = Simulink.SimulationInput(mdl);
for idxSim = 1:numSims
simIn(idxSim) = simIn(idxSim).setVariable('A', idxSim);
simIn(idxSim) = simIn(idxSim).setPostSimFcn(@myPostSimFcn);
simIn(idxSim).UserString = string(idxSim);
end
Step 2: Initialize Parallel Pool and Value Store
Check if a parallel pool exists; if not, create one and initialize a ValueStore for it. The ValueStore is used to trigger the shutdown based on the simulation index.
if isempty(gcp("nocreate"))
pool = parpool('Processes');
store = pool.ValueStore;
store.KeyUpdatedFcn = @shutdownParpool;
end
Step 3: Run the Simulations
Execute the parallel simulations with the parsim command, ensuring that the simulations continue even if errors occur.
out = parsim(simIn, "StopOnError", "off");
Step 4: Define the Post-Simulation Function
The post-simulation function evaluates after each simulation. If the condition (e.g., simulation index greater than 20) is met, the function updates the ValueStore, triggering its callback to stop the parallel pool.
function simOut = myPostSimFcn(simOut)
idxSim = simOut.SimulationMetadata.UserString;
shutdownParpool = double(idxSim) > 20;
if shutdownParpool
store = getCurrentValueStore();
store(double(idxSim)) = idxSim;
end
end
Step 5: Define the Value Store Callback Function
This function acts upon the ValueStore update. It shuts down the parallel pool when triggered by the post-simulation function.
function shutdownParpool(store, key)
pool = gcp("nocreate");
if ~isempty(pool)
disp("Simulation number " + key + " is causing the shutdown of the parpool.")
delete(pool);
end
end

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Test Model Components 的更多信息

标签

尚未输入任何标签。

产品


版本

R2022b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by