フィルターのクリア

Nested parfor effecting fprintf?

3 ビュー (過去 30 日間)
Ali Almakhmari
Ali Almakhmari 2023 年 8 月 9 日
コメント済み: Ali Almakhmari 2023 年 8 月 9 日
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 Center および File ExchangeLoops 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