フィルターのクリア

How to load file with variables to each worker in parsim without TransferBa​seWorkspac​eVariables​?

23 ビュー (過去 30 日間)
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 件のコメント
Benjamin Thompson
Benjamin Thompson 2022 年 7 月 6 日
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.
Philippus van der Merwe
Philippus van der Merwe 2022 年 7 月 7 日
Enabling TransferBaseWorkspaceVariables slows the operation of "parsim" down considerably. During testing, a simulation with only 32 runs/iterations takes approximately 150 seconds more than the equivalent operation using a .mat file. I plan to run at least 3000 iterations, where this delay becomes even more important.
The .mat file is indeed the same for each Monte Carlo iteration. The variations are given through the input objects. That said, when attaching and loading the .mat file, it should do it once for each worker, not once for each iteration.
I am unsure of the exact reason why the enabling of TransferBaseWorkspaceVariables induces this delay. The .mat file is about 8 MB in size. The workspace file size is larger than this, as variables are added on top of the initialization variables during the .m script used to run the program. TransferBaseWorkspaceVariables probably has other overheads as well.

サインインしてコメントする。

回答 (1 件)

Karan Singh
Karan Singh 2023 年 9 月 29 日
Hi Philippus,
From what I understand, you are loading a .mat file containing initialization variables to each worker when using the parsim function in MATLAB with the Parallel Computing Toolbox. As manually loading the full set of initialization variables and using the TransferBaseWorkspaceVariables argument of parsim slows down the runtime considerably. However, there is a small error in your code that is causing the syntax error.
Here's the corrected code snippet:
inPar = <Your array of Simulink Input objects>
% Define the SetupFcn to load the initialization file
SetupFcn = @() load('initVars.mat');
% Run the Monte Carlo analysis using parsim
parsim(inPar, 'SetupFcn', SetupFcn);
Here, define the SetupFcn as a function handle that loads the initialization file using the load function. In this example, the initialization file is assumed to be in the current working directory. Also the SetupFcn function handle is passed as an argument to the parsim function.
Attached below are some documentation links that you may find helpful:
Hope this helps!
Karan Singh Khati

カテゴリ

Help Center および File ExchangeSimulink Environment Customization についてさらに検索

製品


リリース

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by