parfor - can't get my head around it
Mostra commenti meno recenti
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 Commento
Jan
il 9 Mag 2012
As always it would be helpful not only to post that there is an error, but the error message also.
Risposte (3)
Jan
il 9 Mag 2012
3 voti
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 Commento
Daniel Shub
il 9 Mag 2012
Yeah, what you said ...
Daniel Shub
il 9 Mag 2012
0 voti
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
il 9 Mag 2012
2 Commenti
Daniel Shub
il 9 Mag 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
il 9 Mag 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.
Categorie
Scopri di più su Parallel for-Loops (parfor) in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!