我想使用matlab中的Par集群来并行地执行一些步骤。如果我在matlab交互模式下做下面的行,它将正确工作。但是,如果我将它放入一个函数中并执行它,那么它将产生下面的错误。
Par群集在函数中不工作吗?我犯了什么错?
代码:
function []=test()
clust = parcluster();
clust.NumWorkers = 4;
job = createJob(clust);
createTask(job,@(a,b)sum([a,b]),1,{1,2});
%submit(job);
end错误:
[Warning: Objects of class 'parallel.cluster.Local' cannot be saved to MAT files.]
[> In CustomGetSet>CustomGetSet.saveobj at 48
In distcomp.fileserializer.pPutFields>iSaveMat at 84
In distcomp.fileserializer.pPutFields at 62
In distcomp.fileserializer.createFields at 9
In CJSSupport>CJSSupport.initProperties at 224
In CJSSupport>CJSSupport.buildTasks at 531
In CJSJobMethods>CJSJobMethods.createTask at 113
In CJSIndependentJob>CJSIndependentJob.createTaskOnOneJob at 70
In Job.Job>Job.createTask at 269
In test at 7]
[Warning: Objects of class 'parallel.job.CJSIndependentJob' cannot be saved to MAT files.]
[> In CustomGetSet>CustomGetSet.saveobj at 48
In distcomp.fileserializer.pPutFields>iSaveMat at 84
In distcomp.fileserializer.pPutFields at 62
In distcomp.fileserializer.createFields at 9
In CJSSupport>CJSSupport.initProperties at 224
In CJSSupport>CJSSupport.buildTasks at 531
In CJSJobMethods>CJSJobMethods.createTask at 113
In CJSIndependentJob>CJSIndependentJob.createTaskOnOneJob at 70
In Job.Job>Job.createTask at 269
In test at 7]
[Warning: Objects of class 'parallel.cluster.Local' cannot be saved to MAT files.]
[> In CustomGetSet>CustomGetSet.saveobj at 48
In distcomp.fileserializer.pPutFields>iSaveMat at 84
In distcomp.fileserializer.pPutFields at 62
In distcomp.fileserializer.createFields at 9
In CJSSupport>CJSSupport.initProperties at 224
In CJSSupport>CJSSupport.buildTasks at 531
In CJSJobMethods>CJSJobMethods.createTask at 113
In CJSIndependentJob>CJSIndependentJob.createTaskOnOneJob at 70
In Job.Job>Job.createTask at 269
In test at 7]
[Warning: Objects of class 'parallel.job.CJSIndependentJob' cannot be saved to MAT files.]
[> In CustomGetSet>CustomGetSet.saveobj at 48
In distcomp.fileserializer.pPutFields>iSaveMat at 84
In distcomp.fileserializer.pPutFields at 62
In distcomp.fileserializer.createFields at 9
In CJSSupport>CJSSupport.initProperties at 224
In CJSSupport>CJSSupport.buildTasks at 531
In CJSJobMethods>CJSJobMethods.createTask at 113
In CJSIndependentJob>CJSIndependentJob.createTaskOnOneJob at 70
In Job.Job>Job.createTask at 269
In test at 7] 发布于 2015-02-10 07:35:46
这里的问题实际上与parcluster和您使用它的方式无关。如果您在您的功能中提交了该职务,我怀疑它会像预期的那样工作,尽管发出了警告。
这些警告的原因相当模糊--正如警告所指出的那样,集群对象和作业对象无法以正常方式保存和加载。然而,你并没有(明确地)要求--那么为什么要警告呢?实际上,当您创建一个匿名函数时,工作空间中的所有变量都会附加到匿名函数中,以防它需要它们。(这是目前匿名函数的一个限制--它们捕获的上下文太多了)。然后,当函数存储在任务中时,将其保存到磁盘,并发出警告。
您可以通过两种方法中的一种避免此警告:如果您不感兴趣,可以禁用它,或者使用匿名函数以外的其他东西作为您的任务函数。
% Option 1: suppress warning
warning off 'parallel:cluster:CannotSaveCorrectly'
% Option 2: use an internal function
function []=test()
clust = parcluster();
clust.NumWorkers = 4;
job = createJob(clust);
createTask(job,@iSum,1,{1,2});
submit(job);
end
function x = iSum(a, b)
x = sum([a,b]);
end编辑:从MATLAB R2015b开始,匿名函数工作区不再捕获太多的上下文,因此这个问题不应该再以同样的方式发生。
https://stackoverflow.com/questions/28381574
复制相似问题