Question about repeatability in parfor-loop
22 ビュー (過去 30 日間)
古いコメントを表示
Hi everyone,
I thought that simuation results from a for-loop would be identical to simuation results from a parfor-loop. I am wrong. Granted, the difference is very small (e.g. 1.6098e-15).
Does anyone know the reason for the difference? I am running both for-loop and parfor-loop on the same PC.
Here is my little example to show the difference.
Matrices X1 and X2 are identical since they are both generated by for-loops.
Matrices X1 and X3 are different since X1 comes from for-loop and X3 comes from parfor-loop.
Matrices X3 and X4 are identical since they are both generated by parfor-loops. If this is not true, then I am very confused.
kevin1336()
function kevin1336
%% Run for-loop the first time
N = 100;
X1 = zeros(N, 5);
tic
for iter = 1:N
rng(iter, 'twister')
x = core;
X1(iter, :) = x.';
end
toc
%% Run the same for-loop the second time
X2 = zeros(N, 5);
tic
for iter = 1:N
rng(iter, 'twister')
x = core;
X2(iter, :) = x.';
end
toc
max(abs(X1(:) - X2(:)))
%% Run parfor-loop the first time
X3 = zeros(N, 5);
tic
parfor iter = 1:N
rng(iter, 'twister')
x = core;
X3(iter, :) = x.';
end
toc
max(abs(X1(:) - X3(:)))
%% Run parfor-loop the second time
X4 = zeros(N, 5);
tic
parfor iter = 1:N
rng(iter, 'twister')
x = core;
X4(iter, :) = x.';
end
toc
max(abs(X4(:) - X3(:)))
end
function x = core
M = 1e6;
A = rand(M,5);
b = rand(M,1);
x = A \ b;
end
0 件のコメント
採用された回答
Walter Roberson
約14時間 前
編集済み: Walter Roberson
約14時間 前
Inside the parfor, by default each worker only gets a single core, so the \ operation is calculated using a single core.
Inside normal for loops, each worker gets the full complement of cores to automatically parallelize the \ operation over.
The difference in the number of cores results in slight differences in rounding.
その他の回答 (0 件)
参考
カテゴリ
Help Center および 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!