Nested parfor effecting fprintf?

4 visualizaciones (últimos 30 días)
Ali Almakhmari
Ali Almakhmari el 9 de Ag. de 2023
Comentada: Ali Almakhmari el 9 de Ag. de 2023
Hey guys. I have a gigantic while loop, and inside of it, I have multiple for loops. One of those for loops is a parfor. Now my questions is this okay? Is there any harm of doing this? I stumbled upon this: https://www.mathworks.com/help/parallel-computing/nested-parfor-loops-and-for-loops.html. And it got me a bit confused.
To be honest, I am a bit worried because my setup is like the following:
counter = 0
while(1)
for ii=1:5
%blah blah blah
end
for iii = 1:1022
%blah blah blah
end
parfor(iiii = 1,6)
%blah blah blah
end
if(something)
break
end
save("something.mat","SOMETHING")
counter = counter + 1;
fprintf("\n %d is done",counter)
end
And the fprintf keeps outputing "1 is done" with each counter iteration. Its as if the counter variable is not incremented. And what is worse is that the the saving of the .mat file is not happening. I can see it not being updated in the directory. Which I am beginning to think that it might be due to something related to the parallel processer. Any thoughts?
  2 comentarios
Walter Roberson
Walter Roberson el 9 de Ag. de 2023
Could you confirm that your intention is to have the iiii loop execute only a single iteration with iiii being 1, and that your intention is that 6 workers are to be allocated to the parfor ? Or did you intend to use parfor iiii = 1:6 instead of 1,6 ?
Ali Almakhmari
Ali Almakhmari el 9 de Ag. de 2023
Thank you all for your help, the issue was not with MATLAB but with my functions that were called in the for loop...they were messing with my stuff. I still super appreciate you all replying. Thank you again.

Iniciar sesión para comentar.

Respuesta aceptada

Bruno Luong
Bruno Luong el 9 de Ag. de 2023
Editada: Bruno Luong el 9 de Ag. de 2023
I add this line before the IF test
something = rand() < 0.01
if(something)
break
end
it works for me, my command window
>> toto
Starting parallel pool (parpool) using the 'Threads' profile ...
Connected to parallel pool with 14 workers.
ans =
6
1 is done
ans =
6
2 is done
ans =
6
3 is done
ans =
6
...
your something seems to be true at the first while loop. Move the IF at the end, after fprintf.

Más respuestas (0)

Categorías

Más información sobre Loops and Conditional Statements en Help Center y File Exchange.

Productos


Versión

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by