Parfor possible, though iterations depend

조회 수: 6 (최근 30일)
thatbux
thatbux 2016년 7월 22일
댓글: thatbux 2016년 7월 25일
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

채택된 답변

Edric Ellis
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
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.

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by