Parfor possible, though iterations depend
2 views (last 30 days)
Show older comments
Hi,
I am actually concerned with a problem that requires something like the following code, but I am wondering why this does not result in an error message:
parfor k=1:10
List = [List; ones(round(rand()*10), 5)];
end
List is a variable which size is unknown before executing the loop, since an unknown number of rows is added in each iteration.
If I am getting it right, this is supposed to produce errors when executed, since the iterations depend on each other, i.e. the number of rows in List changes with each iteration. However, for a small amount of test data this is not the case -- it seems to work. But why?
Best regards Philipp
0 Comments
Accepted Answer
Edric Ellis
on 25 Jul 2016
There are two different types of output possible from a parfor loop. The simplest is the "sliced" output where each iteration of the loop writes to a different element of the variable. The other type is a "reduction" output. In that case, as shown in the doc page I linked to, various reduction operations are permitted - and one of them is concatenation.
In this case, MATLAB knows how to reduce together the different values of List produced by each worker, and can correctly reconstruct the result "as if" it had been concatenated in serial execution.
More Answers (1)
Adam
on 22 Jul 2016
k is not involved in your parfor loop at all so each worker will just create its own List variable and doesn't care about the fact it grows in a loop because they are all independent of each other.
If you were throwing k into the expression in some way that caused the workers to be sharing the 'List' variable then there would be a problem.
0 Comments
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!