parfor - can't get my head around it
이전 댓글 표시
why does the following code deliver an error..
clear all
n=10;
count=zeros(1,n);
b=round(rand(1,n)*10); %basically some arbitrary array
parfor i=1:length(b)
count(b(i))=count(b(i))+1;
end
count
whereas
count(i)=count(i)+1;
works fine. But I need to address different count-elements, any suggestions?
thanks for every hint in advance.
댓글 수: 1
Jan
2012년 5월 9일
As always it would be helpful not only to post that there is an error, but the error message also.
답변 (3개)
Jan
2012년 5월 9일
3 개 추천
In your code Matlab cannot predict if a specific element of count is read and written at the same time. If this happens, the result is not predictable. In consequence the parallelization fails.
Daniel Shub
2012년 5월 9일
0 개 추천
The MATLAB parser (???) cannot tell that count can be sliced. The parser thinks that potentially b(i) might equal b(j) for some i not equal to j.
Florian
2012년 5월 9일
댓글 수: 2
Daniel Shub
2012년 5월 9일
If your original question has been answers please accept an answer and vote for helpful answers. If not, comment on the answers, edit your question, or continue waiting. You should post this "answer" as a new question. I think this one has work around, but I don't remember it.
Jan
2012년 5월 9일
What is B_field_loop? Imagine it is a function, which uses ASSIGNIN to overwrite the value of "k" by e.g. 14. Then again different PARFOR threads can access an element of B at the same time for reading and writing, such that the results are random.
Isn't there an exhaustive documentation for PARFOR which explains such tricky details?! Parallelizing functions is a very complicated job and avoiding a false sharing of variables is not trivial.
카테고리
도움말 센터 및 File Exchange에서 Parallel for-Loops (parfor)에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!