mvnrnd bug when run in parfor?
1 view (last 30 days)
Show older comments
We are having trouble running the multivariate normal random number generator (mvnrnd) inside a 'parfor' loop. For example, when we run mvrnd within a 'parfor' loop with 5 iterations (i=5) and mvrnd randomly calculate the variable A. We end up with:
A = [2, 7, -3, 12]
[2, 7, -3, 12]
[2, 7, -3, 12]
[2, 7, -3, 12]
[2, 7, -3, 12]
with each iteration of i being identical. Re-running the code will generate a new random set of A, however, like above each iteration i will be identical:
A = [1, 3, 5, -2]
[1, 3, 5, -2]
[1, 3, 5, -2]
[1, 3, 5, -2]
[1, 3, 5, -2]
However, if we run the same bit of code within a 'for' loop, A, always changes randomly as we'd expect.
A = [2, 7, -5, 12]
[3, -9, 2, 12]
[17, 1, -1, 1]
[2, -2, 8, 11]
[5, 7, -3, 19]
This strikes us as rather odd behavior. Can anyone explain why mvrnd doesn't randomly generate A within a parlor loop like it does in a for loop?
Thanks in advance
0 Comments
Answers (4)
Shashank Prasanna
on 18 Jun 2013
This is expected behavior since the random numbers have been seeded to the same initial conditions at the start of each workers. You can change the seed as follows:
parfor i = 1:4
rng(i)
A(i,:) = mvnrnd(zeros(1,4),diag(ones(4,1)));
end
RNG will seed it to a different seed based on the loop index
0 Comments
Edric Ellis
on 20 Jun 2013
Edited: Edric Ellis
on 20 Jun 2013
As Shashank points out, this is expected behaviour. Matlabpool workers have their random number generators set up carefully at the start of the session to get precisely the same results, in an exactly analogous way to what MATLAB does. For example, when I start MATLAB R2013a, the first random number generated by RAND is always the same:
>> rand
ans =
0.8147
Likewise, immediately after opening a matlabpool, the first random number is always precisely the same:
>> matlabpool('open', 'local', 1); spmd, rand, end
Starting matlabpool using the 'local' profile ... connected to 1 workers.
Lab 1:
ans =
0.3246
If you do not close the matlabpool, then the random number stream will continue to progress and different numbers will be generated. If you want to choose a different starting point, then setting the seed directly on the workers is not the best idea.
There's more detail in this answer which tells you about options for setting up the random number generation on the workers.
4 Comments
Edric Ellis
on 25 Jun 2013
Your PARFOR loop still assigns to the whole of "A", thus it is considered to be a "temporary" variable - you need to assign to slices of "A" to get your results...
Peter Perkins
on 24 Jun 2013
Your cov matrices that are pretty badly badly conditioned, but th reason why M<VNRND refuses to continue is that one of them is not even positive semi-definite:
>> eig(s(:,:,7))
ans =
-0.23708
7.0797
8.6212
11775
You can't expect MVNRND to accept a matrix that is not a valid cov matrix, and that one isn't.
2 Comments
Peter Perkins
on 27 Jun 2013
I'm not sure what you're asking. These are pretty badly conditioned matrices, and sigma(:,:,7) may be so badly conditioned that mvnrnd will still refuse to work on it even though eig believes that all the eigen values are non-negative. If you're asking how to fix that, that's a hrd question worthy of it's own thread. If you're just asking how to copy and paste and not lose precision, you probably just want to use "format long" or "format long g".
See Also
Categories
Find more on Parallel for-Loops (parfor) in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!