elapsed time in parfor
이전 댓글 표시
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?
답변 (1개)
Edric Ellis
2014년 8월 5일
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), ...);
카테고리
도움말 센터 및 File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!