Clear Filters
Clear Filters

help with parallel simulation and setting up parallel worker workspaces, getting error with

19 views (last 30 days)
A brief description (in case I'm turning this into a xy problem):
I'm working on a project where we are conducting run to failure experiments for a UAV and have two identical simulink models, except one of them has some extra monitoring and log output. We run the UAV mission, then degrade some of the parameters, and repeat this over and over until the UAV crashes or violates a safety threshold. This is good, but we want to know before the UAV actually crashes, which is where the parallel part comes in. We simulate the "digital twin" in parallel to collect many samples after each real mission, and then generate a distribution from those samples that can tell us if the UAV is going to crash on the next mission. With a 1D polyfit and sliding window, we can forecast a few missions out. In order for this to work, the degradation parameters from the main workspace with the real UAV need passed to each parallel workspace. The problem is that I am getting an error "Array indices must be positive integers or logical values" for each of the parallel workers.
I have a function called parallel_initializer_handler() that is executed on line 173 of next_paper.mlx which creates the workspace for each parallel worker. When I execute this function in the main workspace, I do not get any error, and the struct which I am updating is successfully updated.
This is how I am setting this up:
in(n) = Simulink.SimulationInput("digitaltwin1c");
out = parsim(in, 'ShowSimulationManager',"off", 'ShowProgress',"off", "UseFastRestart", "on", "SetupFcn", @()parallel_initializer_handler(i, lookback, horizon, r_var, q_var, m_var, twin_sample_rate));
Should I approach this differently? How can I peer into each of the parallel workspaces to see what is happening? How can I fix the error, and know that I am passing variables correctly? How can I get some output that says, "this is parallel worker X, the value of my variable is Y" ?
Tim Darrah
Tim Darrah on 29 Mar 2021
Thanks for your comment. I will have to read more into evalin. The reason why I am using it vs assignin is because I need each parallel worker to have their own values for a couple of random variables under study, as opposed to just assigning a variable to some value. This is also why I havn't considered the TransferBaseWorkspaceVariables option, without having tested this I believe it would solve the problem of loading the structs that are needed for the simulation, but how would I make sure that each worker gets its own random value?
For example, I need this executed on each worker so that each worker has a different value and the distribution among the workers doesnt collapse to a single value, where mu and sigma are passed to the setup function (or equivalent if a different implementation) of the parallel worker.
random_variable = normrnd(mu, sigma);
Once this is done, how can I then access this random_variable for each parallel worker from the main workspace?
Mario Malic
Mario Malic on 29 Mar 2021
assignin is under the same roof as evalin, maybe there isn't other way to transfer variables into Simulink workspace.
r = normrnd(mu, sigma, numberofsims, 1)
% r is now a vector size(r) = [numberofsims, 1]
From the second example in parsim documentation:
for i = length(r)
in(i) = Simulink.SimulationInput('CSTR');
in(i) = in(i).setVariable('FeedTemp0',r(i));
Does your simulink model take any other parameter values from workspace? They might be missing, that might be why you got an error in the first place.
If yes, assuming that you use TransferBaseWorkspaceVariables, you should still use the SetupFcn to replace the values for each worker.
Also, can you paste the whole error so It can be seen for which variable does this error occur?
Array indices must be positive integers or logical values.

Sign in to comment.

Answers (1)

Alvaro on 22 Nov 2022
Edited: Alvaro on 22 Nov 2022
You can set a diary for each worker and print the iteration that a particular worker is working on as well as any parameters that were passed to that worker. Afterwards, you can peek into the diaries to see what each worker was doing during the simulation.
A simple example is attached where an array of random numbers is passed to a pool of workers and each worker writes in their respective diary the iteration and random number assigned to it.
The following thread has more details on how this process works.


Find more on Model Predictive Control Toolbox 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!