我想使用 Matlabparfor
来加快我的代码的某些部分。执行所需的一些函数驻留在包含类定义的目录中。因此,我将必要的目录以及所需的文件添加到池对象中,如下所示:
% 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'));
我通过检查poolobj
该字段中是否包含必要文件来确认AttachedFiles
。但是,当我运行时parfor
,Matlab 会抛出一个错误:
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.
编辑:
根据下面的答案,我尝试添加整个目录,但没有奏效:
poolobj.addAttachedFiles(fullfile(pwd,'@Gaussian'));