Main Content

Simulink.sdi.WorkerRun class

Package:Simulink.sdi

Access simulation data from parallel workers

Description

TheSimulink.sdi.WorkerRunclass provides access to run data generated on Parallel Computing Toolbox™ parallel workers. Create aSimulink.sdi.WorkerRunobject on the worker, and then use the object to access data in your local MATLAB®session.

Construction

workerRun= Simulink.sdi.WorkerRun(runID)creates aSimulink.sdi.WorkerRunobject with the run identifier specified byrunID.

workerRun= Simulink.sdi.WorkerRun.getLatestcreates aSimulink.sdi.WorkerRunobject of the most recent run.

Input Arguments

expand all

Run identifier

Methods

getDataset Create Dataset of worker run data
getDatasetRef Create DatasetRef for worker run
getLatest Create worker run for latest run
getLocalRun Create local run from worker run

Copy Semantics

Handle. To learn how handle classes affect copy operations, seeCopying Objects.

Examples

collapse all

This example executes parallel simulations of the modelslexAircraftExamplewith different input filter time constants and shows several ways to access the data using the Simulation Data Inspector programmatic interface.

Setup

Start by ensuring the Simulation Data Inspector is empty and Parallel Computing Toolbox support is configured to import runs created on local workers automatically. Then, create a vector of filter parameter values to use in each simulation.

% Make sure the Simulation Data Inspector is empty, and PCT support is% enabled.Simulink.sdi.clear Simulink.sdi.enablePCTSupport('local')% Define Ts valuesTs_vals = (0.01, 0.02, 0.05, 0.1, 0.2, 0.5, 1];

Initialize Parallel Workers

Use gcp to create a pool of local workers to run parallel simulations if you don't already have one. In anspmdcode block, load theslexAircraftExamplemodel and select signals to log. To avoid data concurrency issues usingsiminparfor, create a temporary directory for each worker to use during simulations.

p = gcp;
Starting parallel pool (parpool) using the 'local' profile ... connected to 4 workers.
spmd% Load system and select signals to logload_system('slexAircraftExample') Simulink.sdi.markSignalForStreaming('slexAircraftExample/Pilot', 1,'on') Simulink.sdi.markSignalForStreaming('slexAircraftExample/Aircraft Dynamics Model', 4,'on')% Create temporary directory on each workerworkDir = pwd; addpath(workDir) tempDir = tempname; mkdir(tempDir) cd(tempDir)end

Run Parallel Simulations

Useparforto run the seven simulations in parallel. Select the value forTsfor each simulation, and modify the value ofTsin the model workspace. Then, run the simulation and build an array ofSimulink.sdi.WorkerRunobjects to access the data with the Simulation Data Inspector. After theparforloop, use anotherspmdsegment to remove the temporary directories from the workers.

parforindex = 1:7% Select value for TsTs_val = Ts_vals(index);% Change the filter time constant and simulatemodelWorkspace = get_param('slexAircraftExample','modelworkspace'); modelWorkspace.assignin('Ts',Ts_val) sim('slexAircraftExample')% Create a worker run for each simulationworkerRun(index) = Simulink.sdi.WorkerRun.getLatestendspmd% Remove temporary directoriescd(workDir) rmdir(tempDir,'s') rmpath(workDir)end

Get Dataset Objects from Parallel Simulation Output

ThegetDatasetmethod puts the data from aWorkerRuninto aDatasetobject so you can easily post-process.

ds(7) = Simulink.SimulationData.Dataset;fora = 1:7 ds(a) = workerRun(a).getDataset;endds(1)
ans = Simulink.SimulationData.Dataset '' with 2 elements Name BlockPath __________ ________________________________________ 1 [1x1 Signal] alpha, rad ...rcraftExample/Aircraft Dynamics Model 2 [1x1 Signal] Stick slexAircraftExample/Pilot - Use braces { } to access, modify, or add elements using index.

Get DatasetRef Objects from Parallel Simulation Output

For big data workflows, use thegetDatasetRefmethod to reference the data associated with theWorkerRun.

forb = 1:7 datasetRef(b) = workerRun(b).getDatasetRef;enddatasetRef(1)
ans = DatasetRef with properties: Name: 'Run 3: slexAircraftExample' Run: [1×1 Simulink.sdi.Run] numElements: 2

Process Parallel Simulation Data in the Simulation Data Inspector

You can also create localRunobjects to analyze and visualize your data using the Simulation Data Inspector API. This example adds a tag indicating the filter time constant value for each run.

forc = 1:7 Runs(c) = workerRun(c).getLocalRun; Ts_val_str = num2str(Ts_vals(c)); desc = strcat('Ts = ', Ts_val_str); Runs(c).Description = desc; Runs(c).Name = strcat('slexAircraftExample run Ts=', Ts_val_str);end

Clean Up Worker Repositories

Clean up the files used by the workers to free up disk space for other simulations you want to run on your worker pool.

Simulink.sdi.cleanupWorkerResources

Alternatives

You can also access, view, and analyze simulation data from Parallel Computing Toolbox workers using the Simulation Data Inspector UI.

Introduced in R2017b