Monday, November 28, 2022
HomeMatlabParallel Simulations: Your choices to configure knowledge when utilizing parsim and batchsim...

# Parallel Simulations: Your choices to configure knowledge when utilizing parsim and batchsim » Man on Simulink

As we speak I’m completely satisfied to welcome visitor blogger Reid Spence. Reid helps Simulink customers operating simulations in parallel utilizing features like parsim or batchsim. On this submit, he shares finest practices to handle the info wanted by Simulink fashions within the context of parallel simulations.
You possibly can obtain the examples used on this weblog submit right here.

## The Drawback

Did you ever run right into a scenario the place you might have a mannequin that simulates efficiently when utilizing the sim command or the play button, however errors out when simulated utilizing parsim and the Parallel Computing Toolbox? This could occur for varied causes, let’s start with the one I see essentially the most typically.

Let’s begin with this easy mannequin, which makes use of 3 variables outlined within the MATLAB base workspace: okay, c, and m:

I can efficiently simulate this mannequin:

mdl = ‘simpleSuspension’;

in(1) = in(1).setVariable(‘okay’,10);

in(2) = in(2).setVariable(‘okay’,20);

out = sim(in,‘ShowProgress’,‘off’);

plot(out(1).logsout.get(‘x’).Values); maintain on

plot(out(2).logsout.get(‘x’).Values);

Now, let’s attempt to use parsim to simulate the mannequin in parallel with completely different values of okay:

mdl = ‘simpleSuspension’;

in(1) = in(1).setVariable(‘okay’,10);

in(2) = in(2).setVariable(‘okay’,20);

out = parsim(in,‘ShowProgress’,‘on’,‘ShowSimulationManager’,‘on’);

As you possibly can see, the simulations errored out. We are able to have a look at the Simulation Supervisor to be taught extra on the error.

## What Occurred?

Let’s look in particulars at what occurred. For that, we are able to have a look at the progress displayed by parsim. The very first thing we see is that parsim begins a parallel pool.

[08-Nov-2022 14:11:17] Checking for availability of parallel pool…

Beginning parallel pool (parpool) utilizing the ‘Processes’ profile …

Related to the parallel pool (variety of employees: 2).

Beginning a parallel pool means the MATLAB session you might be interacting with (the Shopper within the subsequent picture) is launching new MATLAB classes within the background, with out the person interface (the Staff within the subsequent picture)

As soon as the parallel employees are up and operating, we begin Simulink on every employee:

[08-Nov-2022 14:11:39] Beginning Simulink on parallel employees…

To keep away from conflicts between employees, we configure every of them to make use of a special cache folder:

[08-Nov-2022 14:11:46] Configuring simulation cache folder on parallel employees…

The mannequin is loaded on every employee:

Lastly, the mannequin is being simulated:

[08-Nov-2022 14:11:52] Operating simulations…

Hopefully, you see the place I’m going with that… the parallel employees must be configured! Within the above instance, “be configured” means loading variables within the base workspace. In case you are coping with giant complicated fashions, you in all probability perceive that “be configured” can imply lots of issues.

What we’ll focus on on this submit are the completely different choices provided by parsim and batchsim to automate the configuration of the parallel employees

## A number of Choices to Configure Parallel Staff

Articles have been printed on this weblog on the subject of parallel simulations:

Should you undergo these posts, you will note that the method of configuring parallel employees have been considerably simplified all through the years. As of MATLAB R2022b, parsim presents many choices to mechanically configure the parallel employees.

Here’s a image giving an outline of what parsim configures mechanically versus what you possibly can optionally opt-in.

Within the subsequent sections, we’ll cowl the next choices to configure parallel employees:

1. Information Dictionary and Mannequin Workspace
2. Transferring the Shopper Base Workspace
3. Challenge Startup Duties
4. Mannequin Callbacks
5. SetupFcn Callback
6. preSimFcn Callback

## Information Dictionary and Mannequin Workspace

If all of your mannequin is lacking is knowledge, one possibility is to retailer the info in a Information Dictionary or within the Mannequin Workspace. That means parsim will mechanically make the related information accessible to the parallel employees.
With a knowledge dictionary, all you could do is use setVariable to specify the variables to be completely different in every simulation:

mdl = ‘simpleSuspensionWithSldd’;

in(1) = in(1).setVariable(‘okay’,10);

in(2) = in(2).setVariable(‘okay’,20);

out = parsim(in,‘ShowProgress’,‘off’);

## Transferring the Shopper Base Workspace

If, for some purpose, you can’t or do not need to use a knowledge dictionary, it’s attainable is to make use of the TransferBaseWorkspaceVariables possibility of parsim and batchsim to ship a duplicate of the shopper’s base workspace to the employees.

mdl = ‘simpleSuspension’;

in(1) = in(1).setVariable(‘okay’,10);

in(2) = in(2).setVariable(‘okay’,20);

out = parsim(in,‘TransferBaseWorkspaceVariables’,‘on’,‘ShowProgress’,‘on’);

What are the professionals and cons of this method?

• Professional: Simplicity, one extra argument and we mechanically copy all the shopper base workspace to the employees.
• Con: Presumably inefficient if the shopper session has many giant variables in its base workspace that aren’t wanted by the simulation.

## Challenge Startup Duties

In case your mannequin is a part of a MATLAB Challenge and the MATLAB Challenge is open on the shopper, parsim will switch and open the venture on the the employees, which can execute the Challenge Startup Duties.

openProject(‘ParsimDataManagment.prj’);

mdl = ‘simpleSuspension’;

in(1) = in(1).setVariable(‘okay’,10);

in(2) = in(2).setVariable(‘okay’,20);

out = parsim(in,‘ShowProgress’,‘off’);

## Mannequin Callbacks

In case you are not utilizing a MATLAB Challenge, another choice is to make use of callback features such because the mannequin preLoadFcn. For my instance mannequin, I can outline okay, c and m that means:

When the mannequin can be loaded on the employees, the preLoadFcn callback will execute and outline the variables wanted by the mannequin.

mdl = ‘simpleSuspensionWithCallback’;

in(1) = in(1).setVariable(‘okay’,10);

in(2) = in(2).setVariable(‘okay’,20);

out = parsim(in,‘ShowProgress’,‘off’);

## SetupFcn Callback

If not one of the above works for you, parsim presents a SetupFcn callback that can be utilized to run any extra customized setup on the employees. The setupFcn callback can be run as soon as per employee. As a easy instance, the SetupFcn can consider code within the employee’s Base Workspace.

mdl = ‘simpleSuspension’;

in(1) = in(1).setVariable(‘okay’,10);

in(2) = in(2).setVariable(‘okay’,20);

out = parsim(in, ‘SetupFcn’,@()mySetupFunction());

With the setup operate being:

operate mySetupFunction()

evalin(‘base’,‘c=3;okay=5;m=2;’);

finish

## preSimFcn Callback

Along with the SetupFcn callback that runs as soon as per employee, the SimulationInput object presents the preSimFcn callback to run any MATLAB code on the employees earlier than every simulation. The SimulationInput object is handed as an argument so we are able to make modifications to the thing on the employee. We are able to use preSimFcn‘s to parallelize time intensive setups. The pliability of the preSimFcn may also make it perfect for integrating legacy MATLAB code you might have for establishing simulation runs.

mdl = ‘simpleSuspension’;

in(1) = in(1).setPreSimFcn(@(in) presim(in,10));

in(2) = in(2).setPreSimFcn(@(in) presim(in,20));

out = parsim(in);

With the presim operate:

operate in = presim(in, okay)

in = in.setVariable(‘okay’,okay*rand);

in = in.setVariable(‘c’,3);

in = in.setVariable(‘m’,2);

finish

Tell us within the feedback under which combos of the strategies listed on this submit you employ to arrange your parallel simulations.

RELATED ARTICLES