SimEvents: Parallel Simulations produce same results for each MatLab worker

9 visualizaciones (últimos 30 días)
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

Respuesta aceptada

Edric Ellis
Edric Ellis el 28 de Sept. de 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

Más respuestas (1)

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

Categorías

Más información sobre Discrete-Event Simulation en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by