Main Content

Comparison Between Multiple Simulation Workflows

When running a set of multiple simulations, you can run them in parallel on multiple MATLAB® workers in a parallel pool. To run multiple simulations, you can use parsim, parsim with 'RunInBackground' option turned on, or batchsim.

The flow chart shows how running multiple simulations with parsim, parsim with RunInBackground and batchsim differ.

The parsim and batchsim commands use the Parallel Computing Toolbox™ license to run the simulations in parallel. parsim runs the simulations in serial if a parallel pool cannot be created or if the Parallel Computing Toolbox license is not available. batchsim commands cannot run without Parallel Computing Toolbox license.

parsim Workflow

Using parsim command with Parallel Computing Toolbox to run multiple simulations sets up a parallel pool automatically and runs simulations in parallel. The client is always tied to the MATLAB workers.

Basic parsim Workflow

  1. Create an array of Simulink.SimulationInput objects, in, to specify changes to the model.

  2. Specify one-time setup required for running multiple simulations. You can use SetupFcn and TransferBaseWorkspaceVariables to perform setup on the parallel workers.

  3. Run parsim(in) to execute these multiple simulations in parallel. If a parallel pool does not exist, parsim creates it. parsim uses default settings.

  4. You can open Simulation Manager by setting the 'SimulationManager' argument to 'on' with parsim, parsim(in,'SimulationManager','on'). Simulation Manager enables you to monitor the simulations as they are running and gives you access to outputs of the simulations when they are ready. Simulation Manager provides you with information about the simulations running on the workers. For more information, see Simulation Manager.

  5. Once all the simulations are complete, you get an array of Simulink.SimulationOutput objects.

Limitations

  • Closing the MATLAB session terminates simulations on the workers, disabling retrieval of partial results.

parsim with RunInBackground Workflow

  1. Create an array of Simulink.SimulationInput objects, in, to specify changes to the model.

  2. Specify one-time setup required for running multiple simulations. You can use SetupFcn and TransferBaseWorkspaceVariables to perform setup on the parallel workers.

  3. Run parsim with RunInBackground option set to 'on': parsim(in,'RunInBackground','on'). Setting the 'RunInBackground' option to 'on' runs the simulations asynchronously. This keeps the MATLAB command prompt available enabling you to work on other tasks.

  4. With 'RunInBackground' option set to 'on', parsim returns a Simulink.Simulation.Future object. You can poll this object to check the status of simulations, fetch the outputs of simulations when they are completed, or cancel simulations. For more information, see Simulink.Simulation.Future.

Limitations

  • Closing the MATLAB session terminates simulations on the workers, disabling retrieval of partial results. If the future object is cleaned, you are subsequently unable to access the results of the simulations.

  • Using a fetchNext method loop on Future objects, along with Simulation Manager causes them to compete for retrieval of Future objects. Use either a fetchNext method loop or Simulation Manager to get the outputs of completed simulations.

batchsim Workflow

A batch workflow typically means submitting jobs to run simulations on MATLAB workers and subsequently accessing the results of those simulations. When you run simulations in batches, you offload the execution of simulations onto a compute cluster. To learn more about batch processing, see Simple Batch Processing (Parallel Computing Toolbox).

  1. Create an array of Simulink.SimulationInput objects, in, to specify changes to the model.

  2. Specify one-time setup required for running multiple simulations. You can use SetupFcn and TransferBaseWorkspaceVariables to perform setup on the parallel workers.

  3. To run on a cluster or desktop background, call batchsim to offload the execution of simulations. With batchsim, you can use most of the arguments that are compatible with parsim and batch commands. For more information, see batchsim.

    Using batchsim, you offload simulations to a MATLAB worker. To run the batch job on multiple workers, specify a pool size, N, that is an integer specifying the number of workers to make into a parallel pool for the job: batchsim(in, 'Pool', N). There must be at least N+1 workers available on the cluster. If the pool size is not specified, batchsim(in) runs the simulations on a single worker in the cluster specified by the default cluster profile.

    Note

    batchsim errors out when used with a pool size if Parallel Computing Toolbox license is not available.

  4. batchsim offloads simulations to the 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.

  5. On submitting a batch job, batchsim returns a job object containing a job ID and other information that you can use to access the batch job at a later time. Access this job again to check the progress of simulations.

Limitations

  • Because the client session is not tied to the workers, you cannot access the outputs unless the job is finished.

  • batchsim does not give you the ability to monitor simulations with Simulation Manager. For batch jobs, you can use a batch job monitor that tells you if the job is queued, in progress, or completed. For more information, see Job Monitor (Parallel Computing Toolbox)

See Also

Functions

Classes

Tools

Related Topics