How to access variables in parfor loop (addAttachedFiles)?

30 views (last 30 days)
Lorysek
Lorysek on 8 Feb 2018
Commented: Walter Roberson on 6 Dec 2018
Hello all, I have some script, which uses parfor loop and I want to read my entry data, which don't change through iterations, only once. So I found function addAttachedFiles and write it in this way into .m script:
poolobj = gcp;
addAttachedFiles(poolobj,{'read.mat'})
,when all my variables (entry data) are stored in read.mat. When I execute this script it calls my another script, which uses parfor loop, but I always got this error (rupar is one of my variables stored in read.mat):
An UndefinedFunction error was thrown on the workers for 'rupar'. This might be because the file
containing 'rupar' is not accessible on the workers. Use addAttachedFiles(pool, files) to specify
the required files to be attached. See the documentation for 'parallel.Pool/addAttachedFiles' for
more details.
But when I check pool, its writing that file is attached:
Pool with properties:
Connected: true
NumWorkers: 4
Cluster: local
AttachedFiles: D:\work\MATLAB\DREAM_TOOLBOX\MTDREAM_ZS\example_108\cteni.mat
AutoAddClientPath: true
IdleTimeout: 30 minutes (30 minutes remaining)
SpmdEnabled: true
So what's wrong on this approach? Why my Pool don't see any variables? Thank you for any help.
  4 Comments
Walter Roberson
Walter Roberson on 6 Dec 2018
load() the mat file on the worker assigning the load to aa variable and extract the needed fields .
If the parfor has a lot of iterations or the file is large then it is not efficient to load it every iteration . If the content can be treated as constant then you can use parfEvalOnAll to load once per worker .
If the file is large and only one row or column is needed per iteration then it might be more efficient to load in the main routine and let parfor take care of sending the slice needed for each iteration .

Sign in to comment.

Answers (0)

Community Treasure Hunt

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

Start Hunting!

Translated by