How to add folders containing class definitions to matlab parpool object
Show older comments
I would like to use Matlab's parfor to expedite some parts of my code. Some functions needed for the execution reside in a directory that contains a class definition. Hence I add the requisite directory along with required files to the pool object as follows:
% instantiate parallel pool object
poolobj = gcp;
% add file containing class definition
poolobj.addAttachedFiles(fullfile(pwd,'@Gaussian','Gaussian.m'));
% add specific methods required in parfor loop
poolobj.addAttachedFiles(fullfile(pwd,'@Gaussian','logpredictive.m'));
I confirm by checking that poolobj contains requisite files in the AttachedFiles field. However, when I run parfor, Matlab throws an error:
An UndefinedFunction error was thrown on the workers for 'logpredictive'.
This might be because the file containing 'logpredictive' 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.
Any help would be highly appreciated.
Answers (1)
Edric Ellis
on 21 Apr 2017
The function addAttachedFiles can accept folder names as well as file names, so you need to do simply:
poolobj.addAttachedFiles(fullfile(pwd, '@Gaussian'));
1 Comment
Kedar Prabhudesai
on 21 Apr 2017
Edited: Kedar Prabhudesai
on 21 Apr 2017
Categories
Find more on Parallel Computing Fundamentals 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!