elapsed time in parfor

2 views (last 30 days)
Adam
Adam on 4 Aug 2014
Answered: Edric Ellis on 5 Aug 2014
I'm running a parameter optimisation routine, which involved running an ODE model (ode15s) with a range of parameters sampled from the parameter space. For some parameters the problem is stiff and takes very long time to integrate. So I'd like to ignore a combination of parameters if the solution takes too long.
Initially I tried to do this by declaring a global or persistent variable for the time in the function that the ODE solver is calling and break if it exceeds a max_time.
Example:
MAXTIME = 100; %Max time in seconds global elapsedtime
if isempty(elapsedtime) elapsedtime = tic; end
if toc(elapsedtime) > MAXTIME error('Stopped. Taking too long.') end
In parfor global variables cannot be used and I'm looking for another solution.
Can someone help?

Answers (1)

Edric Ellis
Edric Ellis on 5 Aug 2014
You should be able to solve this problem simply by parameterising your function, a bit like this:
function dy = simWithTimeout(t, y, timer, maxtime)
if toc(timer) > maxtime
error('Timeout!');
end
... calculate dy ...
end
And then invoking it like this:
timer = tic;
MAXTIME = 100;
[T, Y] = ode15s(@(t, y) simWithTimeout(t, y, timer, MAXTIME), ...);

Categories

Find more on Loops and Conditional Statements 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!