Are functions called in parfor independent from each other

1 次查看(过去 30 天)
Hello everyone!
I have a question about using parfor loop related to my statistics test. I am trying to split my test from one that has 1000 iterations to four parallel tests that have 250 iterations to save time. Am I right saying that there is no influence between workers involved in this and the test result will correspond regular 1000 iteration test? The program has no errors
Script text:
S = 250;
D = zeros(4,1);
F = zeros(4,1);
d = 4;
parfor e = 1:d
[D(e,1),F(e,1)] = MonteCarlo(S);
end
Unrecognized function or variable 'MonteCarlo'.
disp('D = ');
disp((D(1,1)+D(2,1)+D(3,1)+D(4,1))/(d*S));
disp('F=');
disp((F(1,1)+F(2,1)+F(3,1)+F(4,1))/(d*S));
  7 个评论
Matt J
Matt J 2024-6-10
编辑:Matt J 2024-6-10
These two applications would be equivalent:
They would be equivalent statistically, but note that they would not produce the same stream of random numbers, even though the generator is reset:
rng("default")
x1 = rand(1000,1);
rng("default")
parfor i = 1:4
x2(:,i) = rand(250,1);
end
x2 = x2(:);
[low,high]=bounds(x1-x2)
low = -0.9986
high = 0.9986
Torsten
Torsten 2024-6-10
These two applications would be equivalent:
I meant statistically equivalent to estimate the mean of a uniformly distributed random variable by the use of 1000 realizations. Sorry for the confusion.

请先登录,再进行评论。

回答(1 个)

Walter Roberson
Walter Roberson 2024-6-10
No, there will be a difference.
Linear for loop uses a single random number stream (unless you deliberately change this.)
parfor iterations operate on separate random streams for each iteration.
So for example linear for loop of 1000 iterations might start with seed 3128931 and iterate over that stream. But parfor 1:4 might run the first iteration starting with seed 7488, and the second iteration with seed 574752455, and the third iteration with seed 219, and the fourth iteration with seed 875621642315.
It follows that although the results are statistically the same as each other, that they will not be actually the same as each other.

产品


版本

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by