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

8 Ansichten (letzte 30 Tage)
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

Akzeptierte Antwort

Edric Ellis
Edric Ellis am 4 Jan. 2024
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 Kommentar
Jordan
Jordan am 4 Jan. 2024
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.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Walter Roberson
Walter Roberson am 3 Jan. 2024
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.

Kategorien

Mehr zu Parallel for-Loops (parfor) finden Sie in Help Center und File Exchange

Produkte


Version

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by