1

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

1 回答 1

0

该函数addAttachedFiles可以接受文件夹名和文件名,所以你需要做的很简单:

poolobj.addAttachedFiles(fullfile(pwd, '@Gaussian'));
于 2017-04-21T06:33:59.497 回答