How to load file with variables to each worker in parsim without TransferBaseWorkspaceVariables?
13 views (last 30 days)
Show older comments
I would like to know how one can load a .mat file (filled with initialization variables) to each worker when using the parsim function (with the Parallel Computing Toolbox). This will be for a local machine with 8 workers. I am using parsim to run a large Monte Carlo analysis of a complex simulink model. This model requires a large set of initialization variables, which slows the total runtime considerably when I manually load the full set of them and use the "TransferBaseWorkspaceVariables" argument of parsim.
I have tried to use the "AttachedFiles" and "SetupFcn" arguments to load my initialization file (called "initVars.mat" and is located in my current work directory), as suggested in the following link: https://imathworks.com/matlab/matlab-how-to-avoid-transferring-all-the-workspace-variables-to-the-workers-when-using-parsim/
My code for the parsim implementation is as follows (with "inPar" being an array of Simulink Input objects):
parsim(inPar,'AttachedFiles',{'initVars.mat'},'SetupFcn',evalin('base','load initVars.mat'));
I get the following error when trying to run the code:
Invalid expression. Check for missing multiplication operator, missing or unbalanced delimiters, or other syntax error. To construct matrices, use
brackets instead of parentheses.
I have tried different forms of the arguments in "evalin", with no success. I have also tried the same arguments with the "PreLoadFcn" modifier to the Input objects, also with no success.
It works when "evalin('base','load initVars.mat')" is directly placed into the Simulink Model Properties' Callbacks tab (into PreLoadFcn). However, I want to do things programmatically as far as possible.
I am using Matlab v2021b Update 3, with Simulink v10.4.
Any help would be appreciated.
2 Comments
Benjamin Thompson
on 6 Jul 2022
What is wrong with using TransferBaseWorkspaceVariables? A memory transfer to worker threads would be more efficient than having each of them load a big MAT file from disk. The MAT file is the same for each Monte Carlo run right?
Other options might be to load the model workspace or a data dictionary with the contents of your MAT file but that may not help with anything since the workers would have to perform that load from disk on each run.
Answers (0)
See Also
Categories
Find more on Run Multiple Simulations in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!