matlab createtask 与 parcluster

matlab createtask with parcluster

我想在 matlab 中使用 parcluster 并行执行一些步骤。如果我在 matlab 交互模式下执行以下几行,它会正常工作。但是,如果我将它放入一个函数中并执行它,则会产生以下错误。

parcluster在函数中不起作用吗?我的错误是什么?

代码:

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] 

这里的问题实际上与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 开始,匿名函数工作区不再捕获太多上下文,因此此问题不应再以相同方式出现。