我想使用MATLAB的parfor
加快代码的某些部分。执行所需的某些功能位于包含类定义的目录中。因此,我将必需的目录以及必需的文件添加到池对象,如下:
% 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'));
函数addAttachedFiles
可以接受文件夹名称和文件名,因此您只需要做:
poolobj.addAttachedFiles(fullfile(pwd, '@Gaussian'));