Skip for loop iteration if it takes too long to run

10 Ansichten (letzte 30 Tage)
Gauri Kant
Gauri Kant am 17 Jun. 2021
Beantwortet: Rajanya am 20 Nov. 2024 um 11:07
Hi,
I have a for loop and within that loop I have a numerical optimisation function which converges 99% of the time but in some iterations, it does not and that iteration keeps running with no termination. I want to skip an iteration which takes too long to run. For eg, move to the next iteration if the if the current iteration has already been running for 5 mins but has not yet terminated. I thought of using tic toc but that requires the numerical optimisation function to terminate in order to get the time elapsed. Any ideas as to I can solve this?

Antworten (1)

Rajanya
Rajanya am 20 Nov. 2024 um 11:07
You can use the ‘parfeval’ function from MATLAB's Parallel Computing Toolbox to accomplish this, as it allows you to asynchronously execute a specified function on parallel pool workers.
The main MATLAB session can assign a worker process to execute a specific function and monitor both the elapsed time and the function's status. If the function does not reach the 'finished' status within a set time limit, you can cancel its execution on the worker process and proceed to the next iteration.
Below is a sample code for demonstration. The maximum allowable execution time for the function is set to ‘targetTime’. If this time is exceeded, the function stops executing, and the loop proceeds to the next iteration. The custom function ‘myFunction’ pauses for 10 seconds during the second and fourth iterations; otherwise, it pauses for 4 seconds, which is less than ‘targetTime’. The future object 'f', returned by ‘parfeval’, has a ‘State’ property that is used to check the function's execution status.
targetTime = 6;
function result = myFunction(i)
if i==2 || i==4
pause(10); %to make the second and fourth iteration skip
else
pause(4);
end
result = i; % Sample result
end
for i = 1:5
% Start an asynchronous evaluation of the function assuming a pool (local) is already created
f = parfeval(@myFunction, 1, i); %number of workers set to 1;
% just to leverage the asynchronous behaviour
% Start a timer
startTime = tic;
isCompleted = false;
while toc(startTime) < targetTime %keep track of time
if strcmp(f.State, 'finished')
isCompleted = true;
result = fetchOutputs(f); % Get the result
break;
end
end
if ~isCompleted % if the function execution time overshoots the targetTime, stop it
cancel(f);
fprintf(['Iteration %d timed out and was skipped and ' ...
'it ran for %d\n'], i, toc(startTime));
end
end
On execution, this correctly skips the second and fourth iterations as shown:
For more information on ‘parfeval’ function, refer to the below MathWorks documentation: https://www.mathworks.com/help/releases/R2021a/parallel-computing/parallel.pool.parfeval.html
Hope this helps!!

Kategorien

Mehr zu Programming Utilities finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by