MATLAB Answers

H L
0

why the output of randn is different between serial and parallel loop if using function of RNG

Asked by H L
on 4 Sep 2018
Latest activity Answered by H L
on 6 Sep 2018
The outputs of randn are different between serial and parallel loop if using function of RNG with same seed, although they are same in respecive loops. But the outputs of randn are same between serial and parallel loop (this is what I want ), if using randn('state',XXX), which will be not accepted in the future release.
where did I go wrong and how to correct it if I insist on RNG? Thanks in advance for your commnet
My codes as follows(by matlab R2013)
1. the serial file
clc
clear all
delete *.mat
LOOP=4;
for loop=1:LOOP
rng(123);
% randn('state',11)
out=randn(1,1);
save_result(loop,out)
end
2. the parallel file
clc
clear all
delete *.mat
LOOP=4;
cluster_info = parcluster('local');
NumWorkers=cluster_info.NumWorkers;
isOpen = matlabpool('size');
if isOpen>0
matlabpool('close')
end
matlabpool(cluster_info,min(NumWorkers,LOOP));
parfor loop=1:LOOP
rng(123);
% randn('state',11)
out=randn(1,1);
save_result(loop,out)
end
matlabpool('close')
3. the common file
function save_result(loop,out)
save(['RandValue',num2str(loop),'.mat'],'out')

  0 Comments

Sign in to comment.

Tags

4 Answers

Answer by Titus Edelhofer on 5 Sep 2018
 Accepted Answer

Hi,
interesting. There was one thing missing, namely, that the workers might use a different algorithm (designed to be for the "other" use case). This now works as expected (both serial and all parallel runs give the same results)
function testrng
% init for "serial":
rng('default');
rng(11);
x = randn(10, 1);
% now parallel:
y = zeros(10, 4);
parfor i=1:4
rng('default');
rng(11);
y(:,i) = randn(10, 1);
end
% test
[x y]
diff([x y], 1, 2)
Titus

  0 Comments

Sign in to comment.


Answer by Titus Edelhofer on 4 Sep 2018

Hi,
the idea why the parallel rng gives different answers is, that a common use case is for Monte Carlo simulations with e.g. 100k simulations distributed to ten workers is only wise, if the ten workers each run 10k different simulations. Running ten times the same 10k simulations doesn't make sense. If for your use case it makes sense, take a look at the documentation how to replace the randn call with seed/state. Should be simply
rng(11)
instead of
randn('state', 11);
Titus

  0 Comments

Sign in to comment.


Answer by H L
on 5 Sep 2018

Dear Titus:
Thanks very much for your reply. Maybe I did not say clearly, since I given a simple example for my problem. In fact, I was emulating a communication system case to get how the performances are at different SNR(the ratio of signal power to noise). Every SNR is a for loop, where the generation of value of white noise must be same in every loop to eliminate the effect of noise change , and only the ratio of SNR(dB) is changed. So I need initializ same seed when per loop begins.
The parallel simulation is only for speeding to me, if the parallel output is not as expected, I need revist the code to check in serial state. My concern is that the generation of white noise is different between serial state and parallel state in same loop, even if I set the same seed for RNG. I can not repeat situation of simulation in serial state as same as in parallel state. If I use randn('state',seed) in both serial and parallel code, I can repeat and fix bug easily. why RNG is invalid in this case?
Thank you! Best regards,

  1 Comment

Hi,
that makes perfectly sense. As said, often it's desirable to have the different worker different seeds to combine the output. In your case it doesn't, completely agreed.
Titus

Sign in to comment.


Answer by H L
on 6 Sep 2018

Hi Titus:
amazing rng('default')
trouble shooting.
Thank you so much for your assistance

  0 Comments

Sign in to comment.