How to pass class method handle to createTask

2 views (last 30 days)
Hi,
The Matlab parallel computing toolbox function createTask needs a function handle as the input which it executes on a worker machine. But in all the documents and examples I can find the handle is a plain function handle. I'm wondering if it is possible to pass in a class method handle? That is, I first create a class on the worker machine, and later I want its method be called as a task. If it is impossible, is there any working around (e.g., certain Design Pattern, a trick like message queue, etc.)?
Thanks :)
Peng

Accepted Answer

Edric Ellis
Edric Ellis on 25 Mar 2015
Edited: Edric Ellis on 25 Mar 2015
Remember that MATLAB class methods can always be invoked in the "functional" form, like so:
e = MException('some:identifier', 'This is the message');
report = getReport(e); % same as e.getReport();
Therefore, you can simply specify the method you wish to invoke as the function, and pass the object as the first argument, like so:
j = createJob(parcluster());
t = createTask(j, @getReport, 1, ...
{MException('some:identifier', 'This is the message.')});
submit(j); wait(j);
fetchOutputs(j)
  2 Comments
sun peng
sun peng on 26 Mar 2015
Hi Edric,
Thanks for your reply!
I just realized I didn't make things clear in my initial question. I actually want the class instance be created on the worker machine, holding some states and its method being called many times by the server (i.e., the sponsor of the task, sorry I don't know the exact term here but I guess you know what I mean:) ) where the behavior depends on the states and will change the states.
In your example, the class instance e is destroyed once the task is done, right? Also, I guess the instance e is firstly created on the caller machine and then passed to the worker machine via the network transmission which may be slow? Can we avoid these difficulties?
sun peng
sun peng on 27 Mar 2015
For the updated question, I just found that "spmd" syntax does exactly what I want.
As for the initial question, Edric's answer is the best solution.

Sign in to comment.

More Answers (0)

Categories

Find more on Cluster Configuration in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!