MatlabCreateTask与parcluster

MatlabCreateTask与parcluster,matlab,parallel-processing,Matlab,Parallel Processing,我想使用matlab中的parcluster来并行执行一些步骤。如果我在matlab交互模式下执行以下几行,它将正常工作。但是如果我把它放到一个函数中并执行它,那么它会产生下面的错误 parcluster在函数中不起作用吗?我犯了什么错 代码: function []=test() clust = parcluster(); clust.NumWorkers = 4; job = createJob(clust); createTask(job,@(a,b)sum([a,b]

我想使用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
编辑:从MatlabR2015B开始,匿名函数工作区不再捕获太多的上下文,因此此问题不应再以相同的方式出现