reproducible and independent random stream generation in parfor loop
7 次查看(过去 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.
0 个评论
采纳的回答
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
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.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Parallel for-Loops (parfor) 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!