如何将包含类定义的文件夹添加到matlab parpool对象

时间:2017-04-20 17:14:57

标签: matlab parallel-processing matlabpool

我想使用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'));

1 个答案:

答案 0 :(得分:0)

函数addAttachedFiles可以接受文件夹名称和文件名,因此您只需要这样做:

poolobj.addAttachedFiles(fullfile(pwd, '@Gaussian'));