Question about repeatability in parfor-loop

조회 수: 17 (최근 30일)
Kevin
Kevin 대략 18시간 전
댓글: Kevin 대략 3시간 전
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()
Elapsed time is 6.684938 seconds. Elapsed time is 7.309150 seconds.
ans = 0
Elapsed time is 8.928830 seconds.
ans = 0
Elapsed time is 6.893095 seconds.
ans = 0
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

채택된 답변

Walter Roberson
Walter Roberson 대략 13시간 전
편집: Walter Roberson 대략 13시간 전
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.
  댓글 수: 1
Kevin
Kevin 18분 전
Hi Walter,
Thank you so much for your answer. Now I understand the implications of MATLAB funcitons automatically using multiple cores behind the scene.
To confirm, I have changed my toy example to use FFT and it works. No difference between for-loop and parfor-loop.
function x = core
M = 1e6;
x = fft(rand(M,1), 2^22);
x = x(1:5);
end
Kevin

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

제품


릴리스

R2025b

Community Treasure Hunt

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

Start Hunting!

Translated by