1

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.

Edit:

Based on answer below, I tried adding the entire directory, but did not work:

poolobj.addAttachedFiles(fullfile(pwd,'@Gaussian'));
kedarps
  • 851
  • 9
  • 19

1 Answers1

0

The function addAttachedFiles can accept folder names as well as file names, so you need to do simply:

poolobj.addAttachedFiles(fullfile(pwd, '@Gaussian'));
Edric
  • 23,676
  • 2
  • 38
  • 40
  • Hm, strange, I tried exactly that and it worked for me. Once you'd added the files, what did `pctRunOnAll which Gaussian` say? – Edric Apr 24 '17 at 07:44
  • Running this command does show me the correct file which contains the class definition. I am not sure why it still doesn't work. – kedarps Apr 24 '17 at 13:12
  • Also, before the error, it also throws a warning: `Warning: Element(s) of class 'Gaussian' do not match the current constructor definition. The element(s) have been converted to structures. > In parallel.internal.pool.deserialize (line 9) In remoteParallelFunction (line 37)` – kedarps Apr 24 '17 at 13:40