主要内容

Resolve Setup Issues on Parallel Workers When Running Parallel Simulations with parsim

Issue

Running parallel simulations

In some cases, running parallel simulations with parsim and batchsim with Parallel Computing Toolbox™ may cause setup issues on the parallel workers. Common causes of setup issues include user-written callbacks causing unintended consequences when running on workers, and inlined file locations that don't exist on workers.

Possible Solutions

Since R2023a

To debug setup issues while running parallel simulations, you can use the Diary property that is available on the SimulationMetadata of the Simulink.SimulationOutput. The Diary property shows everything that is printed to the command window on parallel workers. To access the diary property, use the syntax as:

simOut(1).SimulationMetadata.ExecutionInfo.Diary
Where simOut is the Simulink.SimulatinOutput object returned on running multiple simulations with parsim.

You can use the setPreSimFcn function on the Simulink.SimulationInput object and parsim. The setPreSimFcn allows you to configure printing the desired information that is present on the parallel workers, on theDiary property. The example illustrates a PreSimFcn that is configured to print the information that is present on the parallel workers.

First, create an array of Simulink.SimulationInput object for N number of simulations. The functions preSimDiary and postSimDiary are registered as callbacks on the array of Simulink.SimulationInput object.

simin(1:N_sims) = Simulink.SimulationInput(modelName);
for i = 1:N_sims
    simin = setPreSimFcn(simin, @(x) preSimDiary(x));
    simin = setPostSimFcn(simin, @(x) postSimDiary(x));
end

The preSimDiary function captures and prints the information that is present on the parallel workers.

function in = preSimDiary(in)
   %print out information
    fprintf('%%%%Worker Info%%%%\n')
    disp(getCurrentWorker)
    fprintf('%%%%Worker''s Current Directory and Contents%%%% \n %s \n %s\n',pwd,ls);
    fprintf('%%%%Model File Location%%%%\n %s \n',which(in.ModelName));
    fprintf('%%%%Cache Folder Location and Contents%%%% \n') 
    cfg = Simulink.fileGenControl('getConfig');
    disp(cfg.CacheFolder)
    disp(ls(cfg.CacheFolder))
    fprintf('%%%%Attached Files Location and Contents%%%% \n %s \n',getAttachedFilesFolder('myFile.mat')) 
    disp(ls(getAttachedFilesFolder('myFile.mat')))
    fprintf('%%%%Base Workspace on Worker%%%% \n')
    evalin('base','whos')
end

See Also

|