주요 콘텐츠

Simulink.sdi.sendWorkerRunToClient

R2026b

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.

example

Simulink.sdi.sendWorkerRunToClient(run) sends the run corresponding to run to the client MATLAB and imports the run to the Simulation Data Inspector.

Examples

collapse all

To manually send runs created using parallel workers to the Simulation Data Inspector, use Simulink.sdi.sendWorkerRunToClient .

Setup

This example runs several simulations of the ThreeSigs model, varying the value of the amplitude Sine Wave block. To set up for the parallel simulation, define a vector of amplitude values and configure the Simulation Data Inspector for manual Parallel Computing Toolbox™ support.

ampVals = [0.1 0.2 0.3 0.4];
Simulink.sdi.enablePCTSupport("manual");

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 do not already have one.

p = gcp("nocreate");
if isempty(p)
parpool(4);
end

You can use spmd (Parallel Computing Toolbox) to run initialization code common to all workers. For example, load the ThreeSigs model and use Simulink.sdi.markSignalForStreaming to select signals to log to the Simulation Data Inspector. To avoid data concurrency issues when simulating with sim in parfor, create a temporary directory on each worker.

spmd
    load_system("ThreeSigs")
    Simulink.sdi.markSignalForStreaming("ThreeSigs/Sine Wave",1,"on")
    
    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, run parallel simulations using parfor (Parallel Computing Toolbox). Each worker runs a simulation of the ThreeSigs model with a different amplitude value. The software cannot access the contents of the parfor loop, so the variable ampVal is defined in the worker's workspace, where the model can see it, using assignin.

parfor (index = 1:4)
    assignin("base","ampVal",ampVals(index));
    set_param("ThreeSigs/Sine Wave","Amplitude","ampVal")
sim("ThreeSigs");

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 the amplitude used in the simulation with the Tag property.

    IDs = Simulink.sdi.getAllRunIDs;
    lastIndex = length(IDs);
    runID = Simulink.sdi.getRunIDByIndex(lastIndex);
    parRun = Simulink.sdi.getRun(runID);
    parRun.Tag = strcat("Amplitude = ",num2str(ampVals(index)));
    
    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.

spmd
    cd(workDir)
    rmdir(tempDir,"s")
    rmpath(workDir)
end

In each simulation, the Simulink.sdi.sendWorkerRunToClient function imported runs from all the workers into the Simulation Data Inspector. You can view the data and check the run properties to see the amplitude values used during simulation.

Simulink.sdi.view

Input Arguments

collapse all

Simulation Data Inspector run, specified as a positive integer valued 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