elapsed time in parfor

2 vues (au cours des 30 derniers jours)
Adam
Adam le 4 Août 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?

Réponses (1)

Edric Ellis
Edric Ellis le 5 Août 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), ...);

Catégories

En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by