How to control the random number generation when using a parfor loop

2 次查看(过去 30 天)
When I want to repeat my code with the same results, I fix the seed in the beginning of the code. Say: rng(100) I can then generate a random vector: say R = rand(100,1). The result is the same every time I run the code.
Now, instead of generating a random vector of dimension 100 at once, I would like to parallelize my code using parfor and 5 workers. Each worker should work with a subvector: Worker one should work with R(1:20) (it has to generate it first since I do not want to save the whole thing before starting parfor), worker two works with R(21:40).. worker 5 works with R(81:100).
How to control the generation of these subvectors inside each parfor loop, such that the resulting vector R is the same as If I generated it by the command
rng(100)
R = rand(100,1)?

采纳的回答

Edric Ellis
Edric Ellis 2016-11-1
parfor does not guarantee to run the the iterates of the loop in any particular order, and therefore you have to work hard to get reproducible results in this case. Essentially, you would need to set up the random number state based on the loop index (you could use parallel streams to do this). It might be simpler to generate the random numbers outside the loop and send them as a sliced input, i.e.
rng(100);
R = rand(1000,1);
parfor idx = 1:1000
out(idx) = myFcn(R(idx));
end
Generally speaking, the random number generation itself is not the bottleneck in most applications, so this should not be a significant overhead.

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Parallel for-Loops (parfor) 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by