MATLAB Answers

Translated by

このページのコンテンツは英語から自動翻訳されています。自動翻訳をオフにする場合は「<a class="turn_off_mt" href="#">ここ</a>」をクリックしてください。

H L
0

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

H L
さんによって質問されました 2018 年 9 月 4 日
最新アクティビティ H L
さんによって 回答されました 2018 年 9 月 6 日
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 件のコメント

サインイン to comment.

タグ

4 件の回答

回答者: Titus Edelhofer
2018 年 9 月 5 日
 採用された回答

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 件のコメント

サインイン to comment.


回答者: Titus Edelhofer
2018 年 9 月 4 日

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 件のコメント

サインイン to comment.


回答者: H L
2018 年 9 月 5 日

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 件のコメント

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

サインイン to comment.


回答者: H L
2018 年 9 月 6 日

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

  0 件のコメント

サインイン to comment.



Translated by