How to control the random number generation when using a parfor loop
1 回表示 (過去 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)?
0 件のコメント
採用された回答
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 件のコメント
その他の回答 (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!