Parfor possible, though iterations depend
4 次查看(过去 30 天)
显示 更早的评论
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 个评论
采纳的回答
Edric Ellis
2016-7-25
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.
更多回答(1 个)
Adam
2016-7-22
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 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!