MATLAB Answers

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

31 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 22 Aug 2019
Edited: MathWorks Support Team on 22 Aug 2019
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:

  0 Comments

Sign in to comment.

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',...
'ShowProgress','off',...
'UseFastRestart','on',...
'SetupFcn',@()fct_mySetUpFct);

  0 Comments

Sign in to comment.


krishna teja
krishna teja on 1 Aug 2020 at 5:40
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)
tic;
F = parfevalOnAll(gcp,@TransferVars,0,var1, var2);
wait(F)
t = toc;
disp('------------------------------------')
fprintf('| variables transferred in %4.3f s |\n',t);
disp('------------------------------------')
end
function [] = resetWorkers()
tic;
fclear = parfevalOnAll(gcp,@() evalin('base','clear'),0);
t = toc;
disp('----------------------------------------')
fprintf('| worker workspaces cleared in %4.3f s |\n',t);
disp('----------------------------------------')
end
function out = checkWorkers()
f = parfevalOnAll(gcp,@() evalin('base','whos'),1);
out = fetchOutputs(f);
end
function [] = TransferVars(var1,var2)
assignin('base','var1',var1)
assignin('base','var2',var2)
end

  0 Comments

Sign in to comment.


Translated by