reproducible and independent random stream generation in parfor loop

8 ビュー (過去 30 日間)
Ebru Angun
Ebru Angun 2022 年 3 月 12 日
コメント済み: Ebru Angun 2022 年 5 月 9 日
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 Center および File ExchangeParallel 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