Graceful exit from a parfor loop

If I detect an error while processing data within a parfor loop, I would like to gracefully exit the program. Can I delete the parallel pool object using "delete" to shut down all of the workers before exiting?

Respuestas (1)

Matt J
Matt J hace alrededor de 12 horas

1 voto

It sounds like a job for onCleanup.

2 comentarios

Clay Fulcher
Clay Fulcher hace 12 minutos
Editada: Clay Fulcher hace 11 minutos
Thanks Matt. I'll give this a try. I'm not sure it will destroy all the independent workers in a parallel loop, but I'll give it a try. Do you know if simply deleting the parallel pool object will accomplish the destruction of the tasks being performed by all the independent workers in the parfor loop?
Do you know if simply deleting the parallel pool object will accomplish the destruction of the tasks being performed by all the independent workers in the parfor loop?
A simple error will do that. If even one of the workers hits an error, the whole loop aborts and all of the workers stop what they are doing. The only question at that point is whether you want the pool left open. So your code could look like this:
function output=runSomething(poolSize)
objPool = parpool(poolSize);
objCleanup=onCleanup( @() delete(objPool));
try
parfor i=1:N
end
output=...
catch
disp 'The loop failed. Aborting'
output=[];
return
end
end
This will completely close the pool whether the function runs to successful completion or not.

Iniciar sesión para comentar.

Categorías

Productos

Versión

R2025b

Preguntada:

hace alrededor de 15 horas

Comentada:

hace 6 minutos

Community Treasure Hunt

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

Start Hunting!

Translated by