I'm working on a project in which parallel computing would be a huge advantage. The project simulates multiple Simulink
models. I did the simulation with a normal for-Loop, but since it takes days to simulate I decided to try the "parfor"-Loop
.
But that's where the problem begins. First I'll give you pictures of my code, the workspace and the Simulink-part
which is causing me problems:
Here's my code:
apool = gcp('nocreate');
if isempty(apool)
apool = parpool('local');
end
wpath = pwd;
parfor k = 1:number_of_models
load_system(strcat(wpath,'\Models_Folder\',House(k).model_name));
set_param(House(k).model_name, 'Stoptime', num2str(foreruntime));
set_param(House(k).mask_name, 'Data_contr', num2str(controlvector(k)));
set_param(House(k).mask_name, 'Data_cons', strcat('GlobalData(',num2str(k),').consume.',MaskParam(k).consume_input))
SimOut(k) = sim(House(k).model_name);
end
delete(apool);
The confusing thing is if i delete the column:
SimOut(k) = sim(House(k).model_name);
the code just works fine -> the modelparameters
are set in a parfor loop
but if I don't delete the column the following error appears:
Error using Forerunsimple (line 9)
Error evaluating parameter 'Data_cons' in 'model_house_14/House'
Caused by:
Error using parallel_function>make_general_channel/channel_general (line 907)
Error evaluating parameter 'Data_cons' in 'model_house_14/House'
Error using parallel_function>make_general_channel/channel_general (line 907)
Undefined variable "GlobalData" or class "GlobalData".
As you can see in the picture the variable "GlobalData"
is defined in the workspace. So in my opinion it should work. Obviously it doesn't. Do you have any idea what could be the problem?
you may want to see this question, IMHO, it is related, and could in fact be the same problem:
MATLAB: What happens for a global variable when running in the parallel mode?
There a workspace global variable appears to be empty, even if it was defined.
user Edric provides a link, and a short explanation, that global variables are not passed to workers (for instance simulink running as parallel).
The link is to this blog entry: "Getting parfor loops up and running": http://blogs.mathworks.com/loren/2009/10/02/using-parfor-loops-getting-up-and-running/