parfor - can't get my head around it
Afficher commentaires plus anciens
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 commentaire
Jan
le 9 Mai 2012
As always it would be helpful not only to post that there is an error, but the error message also.
Réponses (3)
Jan
le 9 Mai 2012
3 votes
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.
1 commentaire
Daniel Shub
le 9 Mai 2012
Yeah, what you said ...
Daniel Shub
le 9 Mai 2012
0 votes
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
le 9 Mai 2012
2 commentaires
Daniel Shub
le 9 Mai 2012
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
le 9 Mai 2012
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.
Catégories
En savoir plus sur Parallel for-Loops (parfor) dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!