Possible to prematurely stop single workers in a parsim simulation

6 views (last 30 days)
I am running a parsim simulation for a simulink model in R2023B. Here, I experience single workers getting stuck in the simulation, during compilation or in a 'queued' state, some of these later aborting the simulation.
For exploratory simulation, it is not necessary for my simulation run to have every simulation successfully run, I can tolerate a few fails for overall speed gain.
So, my idea would be to set a timeout for each simulation and prematurely stop the simulation, ideally with a timeout error for later processing, if the overall worker occupation of a single run exceeds a set time. This should include queueing and compilation.
I have found this related article on stopping the whole pool using a postSimFunction, and plan to use this to evaluate the run time of each other worker after one simulation has finished, stopping these that take too long.
However, I fail to measure single worker duration and aborting a single simulation on a worker from this postSimfunction. Are there any good ways on how to do that?
If that cannot be done, is it possible to place some form or real-time clock in a simulink simulation to detect simulation duration and finish the simulation based on that criterion?
Thank you for your help.

Accepted Answer

BW
BW on 4 Feb 2025
Edited: BW on 4 Feb 2025
Inspired by your answer, I've created a MATLAB function block that measures the real-time duration and if the time surpassed the input argument in sec, sends a signal to a 'stop' block. This works quite well for me as of now and (except for the workers getting stuck before starting) solves the hangs:
function stopsim = fcn(max_time)
persistent starttime;
if isempty(starttime)
starttime=tic;
end
timediff=toc(starttime);
if timediff>max_time
stopsim=1;
else
stopsim=0;
end
Thank you again for your help on this!

More Answers (1)

Jaimin
Jaimin on 27 Jan 2025
Hi @BW
You can add a clock block to your Simulink model to measure the simulation time. Combine this with a custom block or MATLAB Function block that checks the elapsed time and terminates the simulation if it exceeds a certain threshold.
Kindly refer following code snippet for understanding.
function stopSimBasedOnTime(block)
% Custom MATLAB Function Block
maxSimTime = 300; % Maximum allowed simulation time in seconds
elapsedTime = block.CurrentTime;
if elapsedTime > maxSimTime
set_param(bdroot, 'SimulationCommand', 'stop');
end
end
Use model callbacks like “StartFcn” or “StopFcn” to initialize and check the simulation time, respectively.
For more information kindly refer following MathWorks documentation.
I hope this will be helpful.
  2 Comments
BW
BW on 27 Jan 2025
Thank you very much for your answer Jaimin, but I am rather looking for a means to measure the real-time duration of a simulation instead of the simulation time. Your anwer is however much along the lines of what I need, I wonder if this can be done with real-time measurement instead?
BW
BW on 27 Jan 2025
Furthermore, it might be that this would not help if a worker gets stuck in the 'compilation done' phase.

Sign in to comment.

Categories

Find more on Simulink Real-Time 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!