How to pass class method handle to createTask

5 ビュー (過去 30 日間)
sun peng
sun peng 2015 年 3 月 24 日
コメント済み: sun peng 2015 年 3 月 27 日
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

採用された回答

Edric Ellis
Edric Ellis 2015 年 3 月 25 日
編集済み: Edric Ellis 2015 年 3 月 25 日
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 件のコメント
sun peng
sun peng 2015 年 3 月 26 日
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 2015 年 3 月 27 日
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.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeCluster Configuration についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by