MATLAB: How to avoid transferring all the workspace variables to the workers when using ‘parsim’

alternativeparallelParallel Computing Toolboxsimulationsimulinktransferbaseworkspacevariablesvariableworkspace

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?

Best Answer

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: