Question about repeatability in parfor-loop

22 visualizzazioni (ultimi 30 giorni)
Kevin
Kevin il 28 Gen 2026 alle 0:22
Commentato: Kevin il 28 Gen 2026 alle 14:59
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

Risposta accettata

Walter Roberson
Walter Roberson il 28 Gen 2026 alle 2:24
Modificato: Walter Roberson il 28 Gen 2026 alle 2:25
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 Commento
Kevin
Kevin il 28 Gen 2026 alle 14:59
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

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Loops and Conditional Statements in Help Center e File Exchange

Prodotti


Release

R2025b

Community Treasure Hunt

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

Start Hunting!

Translated by