Can parfor be used in this context?
1 view (last 30 days)
Show older comments
Hi,
i use a for loop to fill the columns of a list sequentally and the way the next column is filled depends on the content of the current column. For speed i'd like to use parfor, but the script shows a red warning saying "Valid indices for (name of list) are restricted in parfor loops", which I don't understand how to adress. This Warning also shows up if i simply use indices like (1,1,1). Can I use parfor in a context where the second iteration depends on the results of the first iteration or does the warning tells me something else?
0 Comments
Accepted Answer
Jan
on 17 Jun 2022
"Can I use parfor in a context where the second iteration depends on the results of the first iteration."
No, you can't for obvious reasons. How could a work be done in parallel, if the process depends on the former results? Imagine the situation physically, e.g. two bakerman with one knead the dough while the other one is shaping the bread. The second one has to wait until the first one is ready.
For more explicit tips, post your code here instead of explaining, what it should do.
4 Comments
Jan
on 20 Jun 2022
Edited: Jan
on 20 Jun 2022
@Edric Ellis: Thanks for this information. I'm astonished, that PARFOR can handle this.
dummy = 0;
parfor i = 1:10^10
dummy = sin(dummy) + 0.1;
end
The error message is not really clear to me, but the problem is.
Edric Ellis
on 20 Jun 2022
@Jan hm, I see what you mean. You and I can see that you're trying to perform a reduction operation, but you didn't match the pattern. The parfor machinery only sees that you're assigning (without indexing) to dummy, you're not matching the "reduction variable" pattern, and is forced to conclude: "Aha! That must be a temporary variable". And then it thinks "Oh dear, the temporary variable is being used before being assigned, that is a problem". There's more on supported parfor reductions in the doc.
More Answers (0)
See Also
Categories
Find more on Parallel for-Loops (parfor) 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!