"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 CenterFile Exchange에서 Direction 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