I need help regarding parfeval implementation with object handle. I have created an object handle, the member functions of which i want to run as parallel processes.
6 ビュー (過去 30 日間)
Sourav Chatterjee 2022 年 12 月 20 日
回答済み: Edric Ellis 2022 年 12 月 22 日
I have created an object handle like this,
I want to run the methods multiply_A and add_B as parallel processes which can modify the properties of the original handle object i.e A and B. For this I am using parfeval as given in the below code,
We can see that the methods dont have any output. They perform the operation on the handle objects only. But when i am running this code, the properties of the handle object 'class1' are not getting changed. A and B are initialized to 1 and they are still retaining the same value after parfeval ends. Anyone can give any idea, how to solve this so that A and B directly gets updated? It is to be noted that, the member functions should not return any value as that is the requirement of the application which i am working on.
Jeffrey Clark 2022 年 12 月 20 日
@Sourav Chatterjee, Run function in background - MATLAB parfeval (mathworks.com) only schedules the function to execute, you need to Wait for futures to complete - MATLAB wait (mathworks.com) then look to see if there is a failure int the returned future (your fout). It wouldn't suprise me if you have an error due to the background threads possibly having their own address space.
Edric Ellis 2022 年 12 月 22 日
The documentation here applies to parfeval as well as parfor in the sense that the object instances on the workers are not connected to the object instances back at the client. Any changes you make on the workers will not be reflected back at the client.
So, you need to do something else instead. One way is to use afterEach to update the handles. I modified your class a little bit to separate computation from update, like this:
function newA = multiply_A(h,data)
% Returns new value, doesn't modify h
newA = h.A;
newA = newA*data;
function apply_A(h, newA)
% Simply modifies h
h.A = newA;
Then, call it like this:
h = example_class();
% Call "multiply_A(h,2)" and grab the output
fut = parfeval(@multiply_A, 1, h, 2);
% When "fut" is complete, call "apply_A(h, newA)"
% aeFut is a future that becomes complete when the "apply_A" stage
% has happened
aeFut = afterEach(fut, @(newA) apply_A(h, newA), 0);
その他の回答 (0 件)
Find more on Background Processing 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!