"Invalid or deleted object" when using clib object in parfor loop
2 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
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 Commenti
Risposta accettata
Edric Ellis
il 4 Gen 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.
Più risposte (1)
Walter Roberson
il 3 Gen 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.
0 Commenti
Vedere anche
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!