Main Content

batchsim

Offload simulations to run on a compute cluster

Description

example

simJob= batchsim(in)runs a batch job on a single worker to simulate a model using the inputs specified in theSimulationInputobject,in.

simJob= batchsim(myCluster,in)runs a batch job on the cluster identified by the cluster objectmyCluster. If a cluster profile is not specified,batchsimuses a default cluster profile as set up in the parallel preferences. For more information, seeDiscover Clusters and Use Cluster Profiles(Parallel Computing Toolbox).

simJob= batchsim(...,Name,Value)runs a batch job that simulates a model using the inputs specified in theSimulationInputobject and the options specified asName,Valuepair.

batchsimoffloads simulations to a compute cluster, enabling you to carry out other tasks while the batch job is processing, or close the client MATLAB®and access the batch job later. Use the'Pool'argument to run simulations in parallel.

Thebatchsimcommand uses the Parallel Computing Toolbox™ andMATLAB Parallel Server™licenses to run the simulations on compute cluster.batchsimruns the simulations in serial if a parallel pool cannot be created. If Parallel Computing Toolbox license is not available,batchsimerrors out.

Examples

collapse all

This example shows how to run parallel simulations in batch.batchsimoffloads simulations to a compute cluster, enabling you to carry out other tasks while the batch job is processing, or close the client MATLAB and access the batch job later.

This example uses theex_sldemo_househeatmodel and runs simulations in batch to observe the model behavior for different temperature set points.

Open the model.

openExample('simulink/OpenTheModelExample'); open_system('ex_sldemo_househeat'); load_system('ex_sldemo_househeat')

Define a set of values for different temperatures. Here, thesetPointValuesare a standard range of temperatures. The length of the vector helps create an array ofSimulink.SimulationInputobjects in the following step.

setPointValues = 65:2:85; spv_Length = length(setPointValues);

使用setPointValues, initialize an array ofSimulink.SimulationInputobjects.

in(1:spv_Length) = Simulink.SimulationInput('ex_sldemo_househeat');fori = 1:1:spv_Length in(i) = in(i).setBlockParameter('ex_sldemo_househeat/Set Point',...'Value',num2str(setPointValues(i)));end

指定the pool size of the number of workers to use. In addition to the number of workers used to run simulations in parallel, a head worker is required. In this case, let's assume that three workers are available to run a batch job for the parallel simulations. The job object returns useful metadata as shown. You can use the jobIDto access the job object later from any machine.NumWorkerstells you how many workers are running the simulations.NumWorkersis always the number of workers specified in the'Pool'argument and an additional head worker.'Pool'is set to3, you can change the value of'Pool'based on the availability of the workers.

simJob = batchsim(in,'Pool',3)
ID: 1 Type: pool NumWorkers: 4 Username: ##### State: running SubmitDateTime: ##-###-#### ##:##:## StartDateTime: Running Duration: 0 days 0h 0m 0s

Access the results of the batch job using thefetchOutputsmethod.fetchOutputsmethod retrieves the results from the workers and eturns an array ofSimulink.SimulationOuputobjects.

out = fetchOutputs(simJob)
1x11 Simulink.SimulationOutput array

Input Arguments

collapse all

Specified as aSimulink.SimulationInputobject or an array ofSimulink.SimulationInputobjects that is used to specify changes to the model for simulation.

Example:in = Simulink.SimulationInput('vdp')

集群object that is used to specify the cluster in which the batch job runs.

Name-Value Arguments

Example:'Pool',5

指定optional comma-separated pairs ofName,Valuearguments.Nameis the argument name andValueis the corresponding value. You can specify several name and value pair arguments in any order asName1,Value1,...,NameN,ValueN.

Specified as a character vector, a cell array or an array of character vector to define paths to be added to the MATLAB search path of the workers before the simulations execute. The default search path might not be the same on the workers as it is on the client; the path difference could be the result of different current working folders (pwd), platforms, or network file system access. The 'AdditionalPaths' property can assure that workers are looking in the correct locations for necessary code files, data files, model files, etc.

Specified as a cell array of additional files to attach to the parallel pool.

Specified as true or false to control whether user-added entries on the client path are added to each worker path.

Specified as true or false to control whether code files are automatically attached to the job.

Specified as true or false to indicate collection of thediary.

指定afunction handleto'CleanupFcn'to run once per worker after the simulations are completed.

Specifies the names of environment variables copied from the client session to the workers. The names specified here are appended to the 'EnvironmentVariables' property specified in the applicable parallel profile to form the complete list of environment variables. Any variables listed which are not set are not copied to the workers. These environment variables will be set on the workers for the duration of the batch job.

WhenManageDependenciesis set to'on', model dependencies are automatically sent to the parallel workers if necessary. IfManageDependenciesis set to'off', explicitly attach model dependencies to the parallel pool.

An integer specifying the number of workers to make into a parallel pool for the jobin additionto the worker running the batch job itself. The simulations use this pool for execution. Because the pool requires N workers in addition to the worker running the batch, there must be at least N+1 workers available on the cluster.

The name of a cluster profile used to identify the cluster. If this option is omitted, the default profile is used to identify the cluster and is applied to the job and task properties.

指定afunction handleto'SetupFcn'to run once per worker before the start of the simulations.

Note

To avoid a compilation error, either set 'LoadExternalInput' to 'off' or ensure that the specified external input is available when usingbuildRapidAcceleratorTarget

Set to'on', to copy the progress of the simulations in the command window todiaryofSimulink.Simulation.Jobobject. The progress is hidden when set to'off'.

Setting'StopOnError'to'on'stops the execution of simulations if an error is encountered.

WhenTransferBaseWorkspaceVariablesis set to真正的, variables used in the model and defined in the base workspace are transferred to the parallel workers.

WhenUseFastRestartis set to真正的, simulations run on the workers using fast restart.

Note

When usingbatchsim, use theUseFastRestartoption and not theFastRestartoption. SeeGet Started with Fast Restartfor more information.

Output Arguments

collapse all

An object containing metadata of submitted batch job. Poll job object using it's ID to check the status of simulations or to access outputs on completion of the job.

Extended Capabilities

Version History

Introduced in R2018b

See Also

Functions

Classes