"Invalid or deleted object" when using clib object in parfor loop
2 ビュー (過去 30 日間)
古いコメントを表示
Hello,
I am trying to find some way to use the function I created using clibgen in a parfor loop. In all cases, the workers that execute the loop are not able to use the object I've created outside the loop. I have tried (shown below) making copies of the method I'm interested in parallelizing (as in this doc), but I'm still getting an "Invalid or deleted object" error.
Can anyone see what I'm doing wrong here?
myCppFunction = clib.libMyLib.SomeCppFunction();
myMethodArr = {@myCppFunction.foo(), ...
@myCppFunction.foo(), ...
@myCppFunction.foo(), ...
@myCppFunction.foo()...
};
parfor i = 1:4
single_method=myMethodArr{i};
single_method();
end
Thanks,
Jordan
0 件のコメント
採用された回答
Edric Ellis
2024 年 1 月 4 日
I'm not terribly familiar with clibgen I'm afraid, but I think the problem here is almost certainly that the function myCppFunction cannot be transferred from client to workers (which is equivalent to saving and the loading the value). To work around this, you can use parallel.pool.Constant. Like this:
% The provided function handle is evaluated on each worker
c = parallel.pool.Constant(@() clib.libMyLib.SomeCppFunction());
parfor i = 1:4
fcn = c.Value; % Extract the function created locally
fcn(); % Call that function
end
This should work because the function that you pass in to the Constant constructor is executed locally on each worker, and so the payload never needs to be transmitted from the client.
その他の回答 (1 件)
Walter Roberson
2024 年 1 月 3 日
I would not expect this to work.
Each of the workers is going to execute inside a different process. The pointer that is established outside of the parfor loop is not going to be valid inside the separate processes that are inside the loop.
0 件のコメント
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!