2

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:

Code & Workspace

From Workspace block

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?

Community
  • 1
  • 1
Elias
  • 51
  • 6
  • Hi Elias, welcome to SO! Nice post you did, but you must consider posting the code as text in your question, instead as a screenshot! – J. Chomel Jun 10 '16 at 08:29
  • Thanks for the help :) Sure posting the code as text makes much more sense, I'll keep that in mind. I'm pretty new to SO and Matlab-Simulink-Coding so forgive my unawareness about some things :) – Elias Jun 10 '16 at 08:36
  • Yes of course, no problem to me. Old ones here should help newcomers. Be aware that some are tough though. Don't take offenses personally. – J. Chomel Jun 10 '16 at 08:39

1 Answers1

0

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/

Community
  • 1
  • 1
Henrik
  • 2,180
  • 16
  • 29
  • Thanks Henrik, I'll read the question. But actually i'm not using a global variable. Initially i tried but I assumed it is a bad idea. So "GlobalData" is just a normal struct with Data-Arrays inside. The Funny thing is i can acces the GlobalData files inside the parfor-Loop. I can display the data for example with "fprintf" inside the parfor-Loop. But if the simulation wants to access the GlobalData inside the parfor-Loop the error occurs :/ – Elias Jun 10 '16 at 09:12
  • I just read the article and i think it's not excatly the same problem :/ Well i can even generate a variable with the data inside the parfor-Loop and it doesn't work. Here's a simplified code example: >parfor i=1:20 Temp = load('someData.mat') set_param(model, 'Data_cons', 'Temp') simout = sim(model) end fore some reason the model cant finde the Temp-variable... – Elias Jun 10 '16 at 09:30
  • Ok, never saw that before, and does not currently have a matlab installation with a simulink license, so .. :( – Henrik Jun 10 '16 at 11:39
  • No Problem. Here's a link to a newer question, which is simplified: http://stackoverflow.com/questions/37747380/simulink-simulation-with-parfor-parallel-computing – Elias Jun 10 '16 at 11:42