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!