SimEvents: Parallel Simulations produce same results for each MatLab worker
조회 수: 6 (최근 30일)
이전 댓글 표시
Hello,
I am trying to run the following:
function means = basic_parallel_test()
modelname = 'G_G_1_5';
reps = 10;
means = ones(10,1);
parfor j = 1 : reps
load_system(strcat(modelname,'.slx'));
se_randomizeseeds(modelname);
simout = sim(modelname);
y = simout.get('ct')
means(j,1) = mean(y.signals.values(:,1))
bdclose(modelname);
end;
end
No errors are reported. So far so good...
But the result is:
ans =
28.3941
30.0470
29.8563
27.9616
30.0470
29.8563
27.9616
27.9957
28.3941
27.9957
Every value is there twice. So it seems since I had 2 matlabworkers, both accessed the same instance of simout. Sice I defined that variable in the parfor-loop I thought it would only be visible inside the loop.
How could I do this right?
Thanks Daniel
댓글 수: 0
채택된 답변
Edric Ellis
2012년 9월 28일
The reason for the duplicate results is that your call to 'se_randomizeseeds' is using the system clock to set up the random number generators. So, because the workers are in sync, they're both getting the same value. You can get around this by explicitly passing a 'GlobalSeed' option to 'se_randomizeseeds'. Here's one way to do that, where we're using a combination of the current time:
parfor j = 1 : reps
seed = mod(floor((j/reps) * now * 8640000),2^31-1);
se_randomizeseeds(modelname, 'GlobalSeed', seed);
...
end
댓글 수: 0
추가 답변 (1개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Discrete-Event Simulation에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!