matlab createtask with parcluster

2019-09-17 15:54发布

I want to use the parcluster in matlab to do some steps parallel. If I do the below lines in matlab interactive mode it works correctly. But if I put it into a function and execute it, then it produces the error below.

Does parcluster not work in a function? What is my mistake?

Code:

function []=test()
  clust = parcluster();
  clust.NumWorkers = 4;

  job = createJob(clust);

  createTask(job,@(a,b)sum([a,b]),1,{1,2});
  %submit(job);
end

Error:

[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条回答
Luminary・发光体
2楼-- · 2019-09-17 16:00

The problem here is actually nothing to do with parcluster and the way you're using it. If you submitted the job from within your function, I suspect it would work as expected despite the warnings.

The reasons for the warnings are rather obscure - as the warnings indicate, cluster objects and job objects can't be saved and loaded in the normal way. However, you're not (explicitly) asking for that - so why the warning? Well, it turns out that when you make an anonymous function, all the variables in the workspace get attached to the anonymous function in case it needs them. (This is a current limitation of anonymous functions - they capture too much context). Then, when the function is stored in the task, it gets saved to disk, and the warning is issued.

You can avoid this warning in one of two ways: either suppress it if you're not interested, or use something other than an anonymous function as your task function.

% 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

EDIT: Starting in MATLAB R2015b, anonymous function workspaces no longer capture too much context, so this problem should no longer occur in the same way.

查看更多
登录 后发表回答