86 views (last 30 days)

Show older comments

I am running monte carlo simulations and use multiple chains. To run the chains in parallel, I open a worker for each chain and use a parfor loop. The probelm is each time I run the code, the randomized initial values are the same. I have tried using the rng function but this does not seem to work when using the parallel computing toolbox. Is there a way to randomize the starting points for each matlabpool worker?

Thank you, Stephen

John Fox
on 20 Jul 2017

I had the exact same problem. My for loops gave a different answer than my parfor loops. The reason is

As described in Control Random Number Streams, each worker in a cluster has an independent random number generator stream. By default, therefore, each worker in a pool, and each iteration in a parfor-loop has a unique, independent set of random numbers. Subsequent runs of the parfor-loop generate different numbers.

I fixed this with rng(123,'twister'). At least this worked for me.

Jill Reese
on 8 Nov 2012

The R2012b documentation provides a section on controlling the random number streams on the client and on the workers. If it does not address your use case, that would be helpful to know so that we can improve it in future.

Best,

Jill

Peter Perkins
on 8 Nov 2012

Just to be clear, MATLAB initializes the random number generators on each worker so that they are definitely not the same, and suitable for parallel computation. In many cases, (needing reproduceablility being one common exception), it should normally not be necessary to worry about initializing them.

It may be that something in your code is doing something to spoil that. The link Jill pointed to should help.

Peter Perkins
on 18 Sep 2014

If I'm understanding correctly, the problem is that, just as with ordinary non-parallel MATLAB, the random numbers on each worker are the same each time you start up (the random number generators are set up using each worker's labindex). If you are doing one calculation in one session, that's fine. But if you want to combine results of MC simulations from multiple sessions, and be able to treat them as statistically independent, then obviously that is a problem.

If that's right, then the solution is to (re)initialize the generator differently on each worker each time you start it up, using pctrunonall. "Differently on each worker each time you start it up" can be achieved using something involving 'shuffle', but it's theoretically possible to get the same initialization in two places by random chance. So a better idea is a combination of labindex and some sort of unique session number.

Just as in the serial case, you could use rng(i), where i is based on the lab index and the session number. But there are parallel generators that are designed specifically for this kind of large-scale MC simulation context: mrg32k3a and mlfg6331_64. If you know how many workers and sessions, then do something like this:

stream = RandStream.create('mrg32k3a','NumStreams',workers*sessions, ...

'StreamIndices',workers*session+worker)

That gives you statistical independence across workers, across sessions. That will work for those two generators. With a non-parallel generator like mt19937ar, your only course would be to use different seeds, but again you could base the seeds on labindex and the session number.

Hope this helps.

Daniel Golden
on 25 Feb 2015

Try something like this to shuffle the random number generator on the local worker and on all the parallel workers:

pool = gcp;

rng('shuffle'); % Shuffles on local worker only

% Shuffle on each parallel worker

seed_offset = randi(floor(intmax/10));

parfor kk = 1:pool.NumWorkers

rng(kk + seed_offset);

end

Tested on R2014b

Matteo
on 9 Mar 2016

Chuck
on 5 May 2016

It does work with Parallel Computing Toolbox. Just add rng("shuffle") after the parfor line.

It might be because your version, since this post is from 2012.

Chibuzo Nnonyelu
on 10 Mar 2018

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

Start Hunting!