Nested parfor effecting fprintf?

2 views (last 30 days)
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 Comments
Walter Roberson
Walter Roberson on 9 Aug 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 on 9 Aug 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.

Sign in to comment.

Accepted Answer

Bruno Luong
Bruno Luong on 9 Aug 2023
Edited: Bruno Luong on 9 Aug 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.

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Products


Release

R2022b

Community Treasure Hunt

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

Start Hunting!