Can parfor be used in this context?

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?

 Accepted Answer

Jan
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

Yes I thought this might be a problem. The parfor loop itself is too long to discuss it in detail, I will figure something out on my own. But for my understanding: How can then something like this work with parfor:
dummy=0;
parfor i=1:10^10
dummy=dummy+1;
end
This works and is actually faster than a for loop, but isn't it the same bakery problem?
The case you posted there is a special case called a reduction operation, where a mathematical transformation can be applied. In particular, that takes advantage of the (approximate) associativity of + to allow partial reductions to be done on each worker, and the the final result assembled at the client.
If your problem can be re-cast as a reduction of some sort, then this can work. If not, then the operations cannot be performed in parallel.
@Edric Ellis: Thanks for this information. I'm astonished, that PARFOR can handle this.
@Malte Römer-Stumm: A problem, which cannot be reduced:
dummy = 0;
parfor i = 1:10^10
dummy = sin(dummy) + 0.1;
end
Error: The temporary variable 'dummy' must be set before it is used. For more information, see Parallel for Loops in MATLAB, "Uninitialized Temporaries".
The error message is not really clear to me, but the problem is.
@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.

Sign in to comment.

More Answers (0)

Categories

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

Products

Release

R2019b

Tags

Community Treasure Hunt

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

Start Hunting!