Simulink.sdi.sendWorkerRunToClient
Send run created on parallel workers to the Simulation Data Inspector
Description
Simulink.sdi.sendWorkerRunToClient
sends the run most
recently generated by the worker to the client MATLAB® and imports the run to the Simulation Data Inspector.
Simulink.sdi.sendWorkerRunToClient(
sends the run corresponding to run
)run
to the client MATLAB and imports the run to the Simulation Data Inspector.
Examples
Manually Send Runs from Parallel Workers to the Simulation Data Inspector
This example shows how to use Simulink.sdi.sendWorkerRunToClient
to send runs created using parallel workers manually to the Simulation Data Inspector.
Setup
This example runs several simulations of the vdp
model, varying the value of the gain, Mu
. To set up for the parallel simulation, define a vector of Mu
values and configure the Simulation Data Inspector for manual Parallel Computing Toolbox support.
% Enable manual Parallel Computing Toolbox support Simulink.sdi.enablePCTSupport('manual'); % Choose several Mu values MuVals = [1 2 3 4];
Initialize Parallel Workers
Use parpool
(Parallel Computing Toolbox) to start a pool of four parallel workers. This example calls parpool
inside an if statement so you only create a parallel pool if you don't already have one. You can use spmd
(Parallel Computing Toolbox) to run initialization code common to all workers. For example, load the vdp
model and select signals to log to runs that we can send to the Simulation Data Inspector on the client MATLAB. To avoid data concurrency issues when simulating with sim
in parfor
, create a temporary directory on each worker. After the simulations complete, another spmd block deletes the temporary directories.
p = gcp('nocreate'); if isempty(p) parpool(4); end
Starting parallel pool (parpool) using the 'local' profile ... connected to 4 workers.
spmd % Load system and select signals to log load_system('vdp') Simulink.sdi.markSignalForStreaming('vdp/x1',1,'on') Simulink.sdi.markSignalForStreaming('vdp/x2',1,'on') % Create temporary directory for simulation on worker workDir = pwd; addpath(workDir) tempDir = tempname; mkdir(tempDir) cd(tempDir) end
Run Parallel Simulations with parfor
To stream data from parallel workers to the Simulation Data Inspector, you have to run parallel simulations using parfor
(Parallel Computing Toolbox). Each worker runs a vdp
simulation with a different value of Mu
. Simulink cannot access the contents of the parfor
loop, so the variable MuVal
is defined in the worker's workspace, where the vdp
model can see it, using assignin
.
parfor (index = 1:4) % Set value of Mu in the worker's base workspace assignin('base','MuVal',MuVals(index)); % Modify the value of Mu in the model and simulate set_param('vdp/Mu','Gain','MuVal') sim('vdp')
Access Data and Send Run to Client MATLAB
You can use the Simulation Data Inspector programmatic interface on the worker the same way you would in the client MATLAB. This example creates a Simulink.sdi.Run
object and attaches the value of Mu
used in the simulation with the Tag
property.
% Attach metadata to the run IDs = Simulink.sdi.getAllRunIDs; lastIndex = length(IDs); runID = Simulink.sdi.getRunIDByIndex(lastIndex); parRun = Simulink.sdi.getRun(runID); parRun.Tag = strcat('Mu = ',num2str(MuVals(index))); % Send the run to the Simulation Data Inspector on the client MATLAB Simulink.sdi.sendWorkerRunToClient end
Close Temporary Directories and View Runs in the Simulation Data Inspector
Use another spmd
section to delete the temporary directories created on the workers once the simulations complete. In each simulation, Simulink.sdi.sendWorkerRunToClient
imported runs from all the workers into the Simulation Data Inspector. You can view the data and check the run properties to see the value of Mu
used during simulation.
spmd % Remove temporary directories cd(workDir) rmdir(tempDir, 's') rmpath(workDir) end Simulink.sdi.view
Input Arguments
run
— Run ID or Simulink.sdi.Run
object
runID | Simulink.sdi.Run
object
Run ID or Simulink.sdi.Run
object corresponding to the
run you want to import into the Simulation Data Inspector.
Version History
Introduced in R2018a
Open Example
You have a modified version of this example. Do you want to open this example with your edits?
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)