How to control the random number generation when using a parfor loop
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
Mohamed Abdalmoaty
am 29 Okt. 2016
Beantwortet: Edric Ellis
am 1 Nov. 2016
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 Kommentare
Akzeptierte Antwort
Edric Ellis
am 1 Nov. 2016
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 Kommentare
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Parallel for-Loops (parfor) finden Sie in Help Center und File Exchange
Produkte
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!