reproducible and independent random stream generation in parfor loop

10 views (last 30 days)
Ebru Angun
Ebru Angun on 12 Mar 2022
Commented: Ebru Angun on 9 May 2022
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.

Accepted Answer

Edric Ellis
Edric Ellis on 14 Mar 2022
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 Comments
Ebru Angun
Ebru Angun on 9 May 2022
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

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!