Parfor variable slicing and assignment

조회 수: 5 (최근 30일)
Isaac
Isaac 2014년 11월 28일
댓글: Isaac 2014년 12월 3일
Hello,
I am wondering why the following behavior is not allowed in Matlab. This is some code that just fills a random matrix in parallel and calls size().
% test loop
m=5; n=10;
A = zeros(m,n);
parfor i=1:m,
A(i,:) = rand(1,n);
sizenow = size(A);
end
The error is "The parfor cannot run due to the way A is used." The reason it should work is that sliced variables in a parfor loop cannot change size; if they could, then the loop iterations would be dependent on each other (each would get a different size of A to work with). So shouldn't Matlab know that the size of A is fixed? If so, why can't the size of A be distributed to each processor?
In fact, if the code reads
parfor i=1:m,
ncol = randi(n,1);
A(i,:) = rand(1,ncol);
end
so that now the size of A changes at each loop iteration, Matlab throws a "subscripted assignment dimension mismatch" error. Matlab (or some part of it) knows what the size of A needs to be in the loop.
Isaac

답변 (3개)

Edric Ellis
Edric Ellis 2014년 12월 1일
MATLAB uses a language analysis to work out what's going on the body of your PARFOR loop. In this case, you would like A to be a "sliced output" - that is, each worker is writing to independent slices of A, and the whole value of A is not available. The language analysis sees the call size(A) and concludes that each worker needs to access the whole value of A inside the loop to make that call - and that is not allowed for a sliced output. In this case, we know that size(A) is constant and doesn't need the values inside the array - but the language analysis cannot prove that (for example, size might be an overloaded method).
In this case the workaround is simple - make the call to size outside the loop and pass the constant size into the loop. In your case you can simply use m and n inside the loop.
  댓글 수: 3
Matt J
Matt J 2014년 12월 2일
편집: Matt J 2014년 12월 2일
However, I think the Matlab language analysis should understand certain functions that are invariant in a parfor loop
As Edric said, though, the size() function is overloadable. If the variable you are slicing is not a native MATLAB object, but rather an object of a user-defined class with its own size() method, parfor has no way of verifying that that size() method has the same invariant behavior as the native one.
Possibly, parfor could be engineered to detect when A is a native MATLAB object, and treat that as a special case. But again, why would the software developers go to such lengths when it only serves to support inefficient coding? If size() is returning the same result every time, it shouldn't be in the loop.
Isaac
Isaac 2014년 12월 3일
Again, I agree that the right way to do this is just to call size() before the loop.
I'd like to point out, though, that Matlab gives special meaning to the operators +,min(),max(),union(),intersect(), and some others in the parfor context; they can operate as reductions. Of course, +,min(), etc could be overloaded with non-associative operators, and then the reduction with parfor would not work. In the same way that calling
A = min(A,val);
triggers a parallel-reduction, it makes sense to me that calling
sz = size(A);
could trigger a broadcast.

댓글을 달려면 로그인하십시오.


Thorsten
Thorsten 2014년 11월 28일
Probably Matlab simply is not smart enough to figure out that in your particular example the size of A is fixed. Without having preallocated A outside the loop the size cannot determined within the parfor. So it would boil down to check that 1. A is defined before parfor 2. All changes of A in loop do not affect it's size
2. is easy in your example, but could be much harder in general. May be that's why it is not implemented at all.

Matt J
Matt J 2014년 11월 28일
편집: Matt J 2014년 11월 28일
If so, why can't the size of A be distributed to each processor?
My guess is they could, but don't expect you to require it. After all, why would you repeatedly compute the size of a matrix in a loop when its size never changes? The more efficient thing would always be to compute it once prior to the loop and broadcast that result to the workers.

카테고리

Help CenterFile Exchange에서 Parallel for-Loops (parfor)에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by