SimEvents: Parallel Simulations produce same results for each MatLab worker

1 view (last 30 days)
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

Accepted Answer

Edric Ellis
Edric Ellis on 28 Sep 2012
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

More Answers (1)

Daniel Huber
Daniel Huber on 12 Oct 2012
Thanks, that was it.
I totally forgot about my question and somehow I expected to get an email, when someone answers...

Categories

Find more on Discrete-Event Simulation in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!