Search code examples
matlabparallel-processingsimulink

Run Simulink model parallel


I would like to run a complex Simulink model in a parfor loop on many cores with different data. However, I didn't have success yet so I created a simple model and tried to run it parallel with the same data.

The model looks like this, adding two signals: enter image description here With the code:

function Result  = Add (X, Y)
Result = X + Y;

The script to run the model is the following:

if matlabpool('size') == 0
    matlabpool('open',4);
end

parfor i = 1:4
    data=ones(1,20);
    X=timeseries(data);
    Y=timeseries(data);
    output = sim('model_test','StopTime','5');
    Result = output.get('Res');
end

However, the following error occurs: enter image description here

I don't understand why the variables are not existing. I know that paralell computing is always critical in terms of variable access, but I didn't have success with simulink parallel running yet. Can you please explain the error to me and how to solve it? Thank you very much!

Answer to am304: Thank you, the answer helped me in the way that I now know how to change constants with set_param in the parfor loop and I understand why it doesn't work for timeseries. However for timeseries I am still struggling. I tried several versions, also this one:

if matlabpool('size') == 0
matlabpool('open',4);
end

data=ones(1,20);
X=timeseries(data);
Ybase=timeseries(data);

parfor i = 1:4
    Y = evalin('base', 'Ybase');
    output = sim('model_test','StopTime','5');
    Result{i} = output.get('Res');
end

The variable Ybase exists in the workspace, but the following error occurs:

enter image description here

As you see, the variable Ybase exists in the base workspace. Do you know how to use evalin or assignin to access properly?

Thanks and regards!


Solution

  • I suspect it's because your data data only exists in the workspace of the main MATLAB, not in any of the instances fired up by matlabpool on the workers. Have a look at Workspace Access Issues in the documentation for more details on how to resolve this, with some examples illustrating the two approaches:

    The MATLAB workers, however, do not have access to the workspace of the MATLAB client session where the model and its associated workspace variables have been loaded. Hence, if you load a model and define its associated workspace variables outside of and before a parfor loop, then neither is the model loaded, nor are the workspace variables defined in the MATLAB worker sessions where the parfor iterations are executed. This is typically the case when you define model parameters or external inputs in the base workspace of the client session. These scenarios constitute workspace access issues.

    [...]

    Resolving Workspace Access Issues

    When a Simulink model is loaded into memory in a MATLAB client session, it is only visible and accessible in that MATLAB session; it is not accessible in the memory of the MATLAB worker sessions. Similarly, the workspace variables associated with a model that are defined in a MATLAB client session (such as parameters and external inputs) are not automatically available in the worker sessions. You must therefore ensure that the model is loaded and that the workspace variables referenced in the model are defined in the MATLAB worker session by using the following two methods.

    • In the parfor loop, use the sim command to load the model and to set parameters that change with each iteration. (Alternative: load the model and then use the g(s)et_param command(s) to set the parameters in the parfor loop)
    • In the parfor loop, use the MATLAB evalin and assignin commands to assign data values to variables. Alternatively, you can simplify the management of workspace variables by defining them in the model workspace. These variables will then be automatically loaded when the model is loaded into the worker sessions. There are, however, limitations to this method. For example, you cannot have tunable parameters in a model workspace. For a detailed discussion on the model workspace, see Model Workspaces.

    EDIT

    Here's what I would do in your particular example:

    if matlabpool('size') == 0
       matlabpool('open',4);
    end
    
    data=ones(1,20);
    X=timeseries(data);
    Y=timeseries(data);
    
    parfor i = 1:4
        assignin('base','Y',Y);
        output = sim('model_test','StopTime','5');
        Result{i} = output.get('Res');
    end
    

    Another option is to include X and Y in the model workspace so that the model is self-contained.