Borrar filtros
Borrar filtros

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

14 visualizaciones (últimos 30 días)
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 comentarios
Benjamin Thompson
Benjamin Thompson el 6 de Jul. de 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.
Philippus van der Merwe
Philippus van der Merwe el 7 de Jul. de 2022
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.

Iniciar sesión para comentar.

Respuestas (0)

Categorías

Más información sobre Run Multiple Simulations en Help Center y File Exchange.

Productos


Versión

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by