reproducible and independent random stream generation in parfor loop

조회 수: 8 (최근 30일)
We have a single program that has to be run on 60 independent (non-overlapping) random streams. We have 12 workers, and because each problem is to be solved independently of the others (no communication between workers), we have decided to use a parfor loop. the random streams have to be reproducible.
1- If we write the code as
stream = RandStream('mrg32k3a','Seed',seed);
parfor ii = 1:60
set(stream,'Substream',ii);
par(ii) = rand(stream);
end
will this create 60 reproducible non-overlapping random streams, where each seed is assigned to a single worker?
2- Within the code, we use normrnd and mvnrnd, which need rng to set the seed. How can we change the code above to be able to use normrnd and mvnrand? Will the use of rng(ii) above instead of substream solve the problem?
Thanks in advance.

채택된 답변

Edric Ellis
Edric Ellis 2022년 3월 14일
This topic is covered here in the documentation. You should not mix setting 'Seed' with setting 'Substream'. (The 'Seed' value sets up the state of the random number generator in a different way, and does not give you the control you need in this situation). So, you should modify your code slightly to do this:
% Use parallel.pool.Constant to hold a RandStream on each worker
sc = parallel.pool.Constant(RandStream('mrg32k3a'));
parfor ii = 1:60
% Get the stream value
stream = sc.Value;
% Set the Substream
set(stream,'Substream',ii);
% Make this stream the default (for normrnd etc.), and store
% the old value for later.
oldGlobalStream = RandStream.setGlobalStream(stream);
par(ii) = rand(stream); % Or you could just call rand()
% At the end, you could restore the old global stream
RandStream.setGlobalStream(oldGlobalStream);
end
Here I've used RandStream.setGlobalStream to set up the stream for normrnd, and reverted at the end of the loop iteration.
  댓글 수: 6
Edric Ellis
Edric Ellis 2022년 5월 9일
Deleting jobs in that way will shut down any active parpool - because a parpool needs a Job to manage the worker processes.
By default, when you encounter a parfor loop, a parpool will start if one is not available. But this doesn't happen if you set the "Auto create parallel pool" preference to false.
Ebru Angun
Ebru Angun 2022년 5월 9일
Thanks so much for all your help. I have realized that I have done a stupid mistake: I forgot to change the for loop to the parfor loop. That was the reason of the error message. After dealing with the code, ı have become blind and could not see such a mistake. Thanks.
Best regards
Ebru

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Parallel for-Loops (parfor)에 대해 자세히 알아보기

제품


릴리스

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by