matlab createtask with parcluster

时间:2015-02-07 11:27:59

标签: matlab parallel-processing

我想在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] 

1 个答案:

答案 0 :(得分:2)

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