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

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 2021년 4월 5일
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!

댓글을 달려면 로그인하십시오.

답변 (0개)

카테고리

도움말 센터File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

질문:

2021년 4월 2일

댓글:

2021년 4월 5일

Community Treasure Hunt

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

Start Hunting!

Translated by