Parfor performance are too good to be true

1 回表示 (過去 30 日間)
Noam
Noam 2015 年 11 月 4 日
編集済み: Thorsten 2015 年 11 月 4 日
Code:
function X
z=ones(1000);
tic
parfor j = 1:200
for k = 1:200
z = z+1;
end
end;
toc
zz=ones(1000);
tic
for j = 1:200
for k = 1:200
zz = zz+1;
end
end;
toc
isequal(z,zz)
Output:
Elapsed time is 0.091983 seconds.
Elapsed time is 12.810199 seconds.
ans =
1
I have two questions:
1. I have only 4 cores, how does parfor run more than 100 times faster?
2. Is the code legal? z = z+1 from 4 cores simultaneously can cause unwanted result (two +1 operations in the same time can result only one increment).
Thanks

採用された回答

Thorsten
Thorsten 2015 年 11 月 4 日
編集済み: Thorsten 2015 年 11 月 4 日
1. I guess the code is optimized, such that, e.g., the for loop is replaced by z = z + 200;
2. That's valid, because the computation of z can be done in any order. It's a simplified version of the last example in http://de.mathworks.com/help/distcomp/parfor.html
  2 件のコメント
Noam
Noam 2015 年 11 月 4 日
1. I guess you are right, but it's strange that in the other case it's not optimised.
2. So I understand that we do not need semaphors etc. as in C.
Thorsten
Thorsten 2015 年 11 月 4 日
編集済み: Thorsten 2015 年 11 月 4 日
1. In a parfor Matlab has to ensure that the results of each iteration are independent. When this is analyzed, the optimization is probably done as a by-product.
2.Right.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by