Parfor indices loop problem

10 views (last 30 days)
shekhar narayanan
shekhar narayanan on 2 Apr 2021
I am new to using parfor. I have a script that takes about an hour to run for each iteration and thought of using parfor to speed it up. My problem is that the parfor loop doesn't go through all indices of the vector I'm iterating through.
parfor root_index=1:length(root)% root is a char array that has the directories I need for the next loop
cd(root(root_index))
%Get array with folder names=folders
for folder_index=1:length(folders)
%do something
end
end
Folders=[F8.F9, F10, F11, F12, F13, F14, F15]; Loop works fine till F10 and then starts repeating itself
  2 Comments
Raymond Norris
Raymond Norris on 2 Apr 2021
root is a set of directories and folders is a set of (sub)directories, correct?
When you say "the loop works fine", which loop are you referring to? folders is the used directly by the for-loop, not the parfor. At the top you mention that the parfor loop doesn't go all the way through, but in the end you say that folders doesn't complete.
I've attached an example that works for for-loop and parfor. See if this works for you. To run it, run the following:
% Run as a for-loop
sn(0)
% Run parfor (let's assume 4 workers)
sn(4)
One thing to check is that you're changing your directory before the next parfor iteration.
If this works for you, share more about what you're doing to see what the difference is.
shekhar narayanan
shekhar narayanan on 5 Apr 2021
Hi, thank you for your comment. You're right, I should have made the distinction between the loop that works and the one that causes problems. I meant: before I used parfor in the outer loop, the inner loop traversed all indices but now, the inner loop for some reason doesn't do that.
Also, yes, the directory does change before the next parfor iteration, is it not good practice to do so? I will check your script and let you know what worked as soon as I find out!

Sign in to comment.

Answers (0)

Categories

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

Community Treasure Hunt

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

Start Hunting!