Are functions called in parfor independent from each other
4 次查看(过去 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
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
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)
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
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.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Statistics and Machine Learning Toolbox 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!