How can I avoid transferring all the workspace variables to the workers when using 'parsim'?

56 views (last 30 days)
I am using 'parsim' to simulate a model in parallel. I am looking for a way to transfer the variables I need to the workers, without using the option 'TransferBaseWorkspaceVariables'. 
In fact, when I perform a large number of runs the variables get very large and MATLAB crashes halfway through my simulations. I believe this is related to a RAM problem with transferring the variables to each of the workers. 
Is there an alternative approach I can use?

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 2 Sep 2021
Edited: MathWorks Support Team on 8 Oct 2021
Instead of transferring the whole workspace to the workers, you can consider transferring only a few variables by using either of the following approaches, both of which take advantage of other properties of the 'parsim' function:
1) By using 'evalin':
  • Save the variables you want to transfer to the workers in a MAT file, by using the save function.
  • Add the MAT file as an attachment to the 'parsim' simulation by using the option 'AttachedFiles'.
  • Add a setup function to the simulation by using the 'SetupFcn' option. This function is going to load the MAT file using the load function, but since we want the loading to occur in the worker, we use the evalin function:
>> evalin( 'base', 'load MATFILE.mat' )
2) By using 'assignin':
  • Add a setup function to the simulation by using the 'SetupFcn' option. This function is going to assign some variables to the workspace of the workers, by using the assignin function:
>> assignin( 'base', 'myvar = 10;' )
Both of these methods should achieve the reduction in RAM usage you are seeking. Do let me know if this is not the case. As a final note, please do not forget to turn off the 'TransferBaseWorkspaceVariables' option. 
Please find more information on the Name-Value pairs of the 'parsim' function on the following page of the documentation:

More Answers (2)

Max Helbing
Max Helbing on 6 Jun 2020
I'm using a 'SetupFcn' to initialize all model parameters on each parallel worker.
simOut = parsim(simIn,'ShowSimulationManager','on',...

krishna teja
krishna teja on 1 Aug 2020
here is complete solution which worked well for me
This is not restricted to parsim but in general to transfer variables to worker workspace
transferVars(var1,var2) % transfer var1, var2 to parallel workers. you can send more variables by adding additional arguments to local functions
resetWorkers() % clear all variables on all workers
out = checkWorkers() % check workspaces of all workers
%% local functions
function [] = transferVars(var1,var2)
F = parfevalOnAll(gcp,@TransferVars,0,var1, var2);
t = toc;
fprintf('| variables transferred in %4.3f s |\n',t);
function [] = resetWorkers()
fclear = parfevalOnAll(gcp,@() evalin('base','clear'),0);
t = toc;
fprintf('| worker workspaces cleared in %4.3f s |\n',t);
function out = checkWorkers()
f = parfevalOnAll(gcp,@() evalin('base','whos'),1);
out = fetchOutputs(f);
function [] = TransferVars(var1,var2)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by