SimEvents: Parallel Simulations produce same results for each MatLab worker
1 次查看(过去 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!