mvnrnd bug when run in parfor?
Ältere Kommentare anzeigen
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
Antworten (4)
Shashank Prasanna
am 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
Kirk
am 19 Jun. 2013
Edric Ellis
am 20 Jun. 2013
Bearbeitet: Edric Ellis
am 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 Kommentare
Kirk
am 20 Jun. 2013
Bearbeitet: Edric Ellis
am 21 Jun. 2013
Edric Ellis
am 21 Jun. 2013
Sorry, I'm not that experienced with MVNRND - could you post a completely standalone script that reproduces the problem. Note that in the example above, your PARFOR loops are not actually going to modify 'A' - but perhaps you just missed something. You need something like
parfor ...
A(i, :) = mvrnd(...);
end
otherwise PARFOR treats 'A' as a loop temporary.
Edric Ellis
am 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
am 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 Kommentare
Kirk
am 24 Jun. 2013
Peter Perkins
am 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".
Kategorien
Mehr zu Parallel for-Loops (parfor) finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!