getDatasetRef
Create Simulink.sdi.DatasetRef
object for worker
run
Description
Examples
Access Data from Parallel Simulation
Execute parallel simulations of the model slexAircraftExample
with different input filter time constants and access the data in different ways using the Simulation Data Inspector programmatic interface.
Setup
Check that the Simulation Data Inspector is empty and that 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.
Simulink.sdi.clear
Simulink.sdi.enablePCTSupport("local")
Ts_vals = [0.01, 0.02, 0.05, 0.1, 0.2, 0.5, 1];
Initialize Parallel Workers
Use the gcp
function to create a pool of local workers to run parallel simulations if you don't already have one. In an spmd
code block, load the slexAircraftExample
model and select signals to log. To avoid data concurrency issues using sim
in parfor
, create a temporary directory for each worker to use during simulations.
p = gcp;
Starting parallel pool (parpool) using the 'Processes' profile ... 02-Nov-2023 16:05:17: Job Queued. Waiting for parallel pool job with ID 1 to start ... 02-Nov-2023 16:06:18: Job Queued. Waiting for parallel pool job with ID 1 to start ... Connected to parallel pool with 6 workers.
spmd % Load system and select signals to log load_system("slexAircraftExample") Simulink.sdi.markSignalForStreaming("slexAircraftExample/Pilot", 1, "on") Simulink.sdi.markSignalForStreaming("slexAircraftExample/Aircraft Dynamics Model", 4, "on") % Create temporary directory on each worker workDir = pwd; addpath(workDir) tempDir = tempname; mkdir(tempDir) cd(tempDir) end
Run Parallel Simulations
Use parfor
to run the seven simulations in parallel. Select the value for Ts
for each simulation, and modify the value of Ts
in the model workspace. Then, run the simulation and build an array of Simulink.sdi.WorkerRun
objects to access the data with the Simulation Data Inspector. After the parfor
loop, use another spmd
segment to remove the temporary directories from the workers.
parfor index = 1:7 % Select value for Ts Ts_val = Ts_vals(index); % Change the filter time constant and simulate modelWorkspace = get_param("slexAircraftExample","modelworkspace"); assignin(modelWorkspace,"Ts",Ts_val) sim("slexAircraftExample"); % Create a worker run for each simulation workerRun(index) = Simulink.sdi.WorkerRun.getLatest end spmd % Remove temporary directories cd(workDir) rmdir(tempDir, "s") rmpath(workDir) end
Get Dataset Objects from Parallel Simulation Output
The getDataset
function puts the data from a WorkerRun
object into a Dataset
object so you can easily post-process.
ds(7) = Simulink.SimulationData.Dataset; for a = 1:7 ds(a) = getDataset(workerRun(a)); end ds(1)
ans = Simulink.SimulationData.Dataset '' with 12 elements Name BlockPath __________ ________________________________________ 1 [1x1 State ] '' slexAircraftExample/Actuator Model 2 [1x1 Signal] alpha, rad ...rcraftExample/Aircraft Dynamics Model 3 [1x1 State ] '' ...cs Model/Pitch Channel/Integrate qdot 4 [1x1 State ] '' ...mics Model/Vertical Channel/Integrate 5 [1x1 State ] '' ...ntroller/Alpha-sensor Low-pass Filter 6 [1x1 State ] '' ...ller/Integrator/Continuous/Integrator 7 [1x1 State ] '' ...ple/Controller/Pitch Rate Lead Filter 8 [1x1 State ] '' ...aftExample/Controller/Stick Prefilter 9 [1x1 State ] '' .../Dryden Wind Gust Models/Q-gust model 10 [1x1 State ] '' .../Dryden Wind Gust Models/W-gust model 11 [1x1 Signal] Stick slexAircraftExample/Pilot 12 [1x1 Signal] alpha, rad slexAircraftExample/alpha, rad - Use braces { } to access, modify, or add elements using index.
Get DatasetRef
Objects from Parallel Simulation Output
For big data workflows, use the getDatasetRef
function to reference the data associated with the WorkerRun
.
for b = 1:7 datasetRef(b) = getDatasetRef(workerRun(b)); end datasetRef(1)
ans = DatasetRef with properties: Name: 'Run <run_index>: <model_name>' Run: [1×1 Simulink.sdi.Run] numElements: 12
Process Parallel Simulation Data in the Simulation Data Inspector
You can also create local Simulink.sdi.Run
objects to analyze and visualize your data using the Simulation Data Inspector programmatic interface. This example shows a tag indicating the filter time constant value for each run.
for c = 1:7 Runs(c) = getLocalRun(workerRun(c)); 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
Input Arguments
workerRun
— Worker run
Simulink.sdi.WorkerRun
object
Worker run, specified as a Simulink.sdi.WorkerRun
object.
domain
— Scope
"signals"
| "outports"
| "dsm"
| "state"
| "param"
| ...
Scope of data in the Simulink.sdi.WorkerRun
object to
return, specified as one of these options:
"signals"
— Signals produced by signal logging"outports"
— Model output signals, represented by top-level Outport blocks"dsm"
— Data Store Memory blocks"state"
— Simulink® states"param"
— Parameter data for block parameters and variables tuned using dashboard blocks"sf_data"
— Stateflow® local data"sf_state"
— Stateflow states"sf_state_child"
— Stateflow child activity"sf_state_leaf"
— Stateflow leaf activity"slt_verify"
— Assertion blocks and verify assessments in Simulink Test™
Output Arguments
dsr
— Reference to run data
Simulink.sdi.DatasetRef
object
Reference to run data, specified as a Simulink.sdi.DatasetRef
object referencing the data in the
Parallel Computing Toolbox™ worker run.
Version History
Introduced in R2017b
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)