"Invalid or deleted object" when using clib object in parfor loop

2 ビュー (過去 30 日間)
Jordan
Jordan 2024 年 1 月 3 日
コメント済み: Jordan 2024 年 1 月 4 日
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(), ...
Invalid expression. When calling a function or indexing a variable, use parentheses. Otherwise, check for mismatched delimiters.
@myCppFunction.foo(), ...
@myCppFunction.foo(), ...
@myCppFunction.foo()...
};
parfor i = 1:4
single_method=myMethodArr{i};
single_method();
end
Thanks,
Jordan

採用された回答

Edric Ellis
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 件のコメント
Jordan
Jordan 2024 年 1 月 4 日
Thank you, this worked! This makes each worker construct its own copy of my clibgen function, but saves it and reuses it for the remainder of the time that the worker is active. Exactly what I needed.

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

その他の回答 (1 件)

Walter Roberson
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.

カテゴリ

Help Center および File ExchangeDirection of Arrival Estimation についてさらに検索

製品


リリース

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by