question regarding what is allowed in a parfor loop
이전 댓글 표시
I am confused about a section of the description of how to use parfor:
" Next assume that A, B, and C are variables and that f, g, and h are functions:
parfor i = 1:n
t = f(A(i));
u = g(B(i));
C(i) = h(t, u);
end
If the time to compute f, g, and h is large, parfor will be significantly faster than the corresponding for statement, even if n is relatively small. Although the form of this statement is similar to a for statement, the behavior can be significantly different. Notably, the assignments to the variables i, t, and u do not affect variables with the same name in the context of the parfor statement. The rationale is that the body of the parfor is executed in parallel for all values of i, and there is no deterministic way to say what the "final" values of these variables are. Thus, parfor is defined to leave these variables unaffected in the context of the parfor statement. By contrast, the variable C has a different element set for each value of i, and these assignments do affect the variable C in the context of the parfor statement. "
This is a bit of a long paragraph, but I'm mostly confused about the last line about it affecting C. How is this any different than saying:
parfor i = 1:n
C(i) = h(f(A(i)), g(B(i)));
end
I think in this second case, that there should be no difference between using a parfor loop and just a for loop. I don't really understand why this is not the case in the first example
채택된 답변
추가 답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!