reproducible and independent random stream generation in parfor loop

13 visualizzazioni (ultimi 30 giorni)
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.

Risposta accettata

Edric Ellis
Edric Ellis il 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 Commenti
Edric Ellis
Edric Ellis il 9 Mag 2022
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 il 9 Mag 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

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Parallel Computing Fundamentals in Help Center e File Exchange

Prodotti


Release

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by