Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Question on RNGs for use in parallel simulations #37

Open
GregPlowman opened this issue Apr 5, 2018 · 4 comments
Open

Question on RNGs for use in parallel simulations #37

GregPlowman opened this issue Apr 5, 2018 · 4 comments

Comments

@GregPlowman
Copy link
Contributor

GregPlowman commented Apr 5, 2018

This is a question rather than an issue. I hope that's OK to ask here.

I would like to use a RNG for parallel simulations, and so want statistically independent RNGs for each worker process.

For Base.Random.MersenneTwister, I use something like this:

rng = Base.Random.MersenneTwister(0)
rngseed = Base.Random.make_seed()
srand(rng, rngseed)

rngs = randjump(rng, numtrials)

f(trial, rng) = ...
pmap(f, 1:numtrials, rngs)

I would like to use a RNG from the Random123 family.
Is this a good choice for parallel sims?

My code would be something like:

rng = RandomNumbers.Random123.Threefry4x()
rngseed = RandomNumbers.gen_seed(rng)
srand(rng, rngseed)

rngs = Vector{typeof(rng)}(numtrials)
for trial = 1:numtrials
    rngs[trial] = copy(rng)
    RandomNumbers.Random123.set_counter!(rngs[trial], trial)
end

f(trial, rng) = ...
pmap(f, 1:numtrials, rngs)

Is using set_counter! this way the right approach to give independent RNGs?
Will it work for all RNGs in the Random123 family?
Why would I choose 2x or 4x version?
Which particular RNG would you recommend?

@sunoru
Copy link
Member

sunoru commented Apr 6, 2018

Random123 is suitable for running in parallel, and set_counter! is one of the right approaches to give independent RNGs. But as I replied in another issue, you don't need to do gen_seed and srand manually. You can directly do set_counter! on your RNG objects.
If you don't need to store the seeds for reproduction, actually you can just use Threefry4x() for numtrials times to generate the RNGs. If you need to store the seeds, you should store seed = gen_seed(UInt64, 4), and Threefry4x(UInt64, seed) to generate RNG object with the seed.

All the RNGs in the Random123 family is suitable for this approach.

2x and 4x indicates the number of random numbers generated at a time. 4x with UInt64 only works on the 64-bit machines and can take advantages of the best performance so I recommended just use this (it is also the default version).

@GregPlowman
Copy link
Contributor Author

If you don't need to store the seeds for reproduction, actually you can just use Threefry4x() for numtrials times to generate the RNGs

But does this guarantee statistically independent RNG instances?

I thought for parallel sims, the approach with Random123 was to generate a single seed for seeding all RNG instances, but use set_counter on each RNG instance to "jump ahead" to a different non-overlapping part of the sequence. This way all numbers generated by the parallel RNG instances would be coming from the same sequence, but just from different sub-sequences within that sequence.

If you need to store the seeds, you should store seed = gen_seed(UInt64, 4), and Threefry4x(UInt64, seed) to generate RNG object with the seed.

Yes I do need to store the seed , which is why I would rather use gen_seed(rng) or gen_seed(typeof(rng)). (See #36). That way I don't have to manually determine the seed type if I want to change RNG type.

@sunoru
Copy link
Member

sunoru commented Apr 6, 2018

They should be statistically independent but using set_counter is the best way to guarantee that, because by default the counters for all Random123 RNGs are zeros.

@gideonsimpson
Copy link

Does anyone have some example code for using Random123 with RandomNumbers to accomplish this? I came across this project specifically because I was looking for a safe parallel RNG in Julia.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants