Nested parfor effecting fprintf?

4 次查看(过去 30 天)
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 个评论
Walter Roberson
Walter Roberson 2023-8-9
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 2023-8-9
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.

请先登录,再进行评论。

采纳的回答

Bruno Luong
Bruno Luong 2023-8-9
编辑:Bruno Luong 2023-8-9
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.

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

产品


版本

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by