필터 지우기
필터 지우기

Are functions called in parfor independent from each other

조회 수: 1 (최근 30일)
Arkadiy
Arkadiy 2024년 6월 10일
댓글: Torsten 2024년 6월 10일
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