timeout using parfor loop and ode15s

조회 수: 5 (최근 30일)
Tricia
Tricia 2012년 4월 26일
댓글: Shreenath Krishnamurthy 2025년 7월 14일
I am working on a problem where I integrate a system of ODEs from multiple initial points. I am using a parfor loop to parallelize over the initial points. However, a fraction of the initial points result in systems of ODEs that take excessive amounts of computing time to solve. I would like to be able to have these points timeout automatically and free the computing resources so the computer can move on to the next point. Can anyone tell me how to do this?

답변 (1개)

Alex Ryabov
Alex Ryabov 2014년 3월 28일
편집: Alex Ryabov 2014년 3월 28일
Hi I just came across the same problem. I run a simulation with different parameters and for some parameters it takes very long time. Sometimes it's just a numerical instability.
To stop the function by a timeout I use events . This example will stop the calculation after 5 seconds and you will get what the solver could calculate during this time.
function Results = RunCalculation(Parameters)
% 1. define a link to an an event function which will stop calculation
xoverFcn = @(T, Y) MyEventFunction(T, Y);
% 2. register this function as an event function
options = odeset('Events',xoverFcn);
% 3. start a stopwatch timer, if you already use one, define a new one:
% tic(ticID)
% then ticID should be unique for each run and global for this function.
tic;
% Run the model
[T,Y] = ode45(NPZModel, TSpan, [1, 1, 1], options);
%%Define the event function
function [VALUE, ISTERMINAL, DIRECTION] = MyEventFunction(T, Y)
%The event function stops when VALUE == 0 and
%ISTERMINAL==1
%a. Define the timeout in seconds
TimeOut = 5;
%
%b. The solver runs until this VALUE is negative (does not change the sign)
VALUE = toc-TimeOut
%c. The function should terminate the execution, so
ISTERMINAL = 1;
%d. The direction does not matter
DIRECTION = 0;
  댓글 수: 1
Shreenath Krishnamurthy
Shreenath Krishnamurthy 2025년 7월 14일
Thank you for this. It didnt work for me with toc, as the code skips that statement and says that max generations exceeded within a second. So i replaced toc with tic and it works.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Programming에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by