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
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 ?
采纳的回答
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 个评论
更多回答(0 个)
另请参阅
类别
在 Help Center 和 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!