Invoke function in a parfor loop

Hi all, I run a for loop where the iterations are independent and it's works well. To speed up my simulation, I'm trying to use a parfor loop. But I'm having some trouble with the invoke function I use to call another software to make some calculation. I get the following error :
An UndefinedFunction error was thrown on the workers for 'invoke'. This might be because the
file containing 'invoke' is not accessible on the workers. Use addAttachedFiles(pool, files)
to specify the required files to be attached. See the documentation for
'parallel.Pool/addAttachedFiles' for more details.
I understand that the file containing invoke could not be found but I could not find in the documentation the location of this file. Has someone already obtained this error and fix it ?

2 Commenti

Edric Ellis
Edric Ellis il 6 Dic 2016
Modificato: Edric Ellis il 6 Dic 2016
A few questions. What cluster type are you using? (Normally, if you are using the 'local' cluster type, then the workers can see the same code on the path as your desktop MATLAB). Is your invoke a function or a method? What happens if you try
addAttachedFiles(gcp(), {'invoke'})
?
Thank you for your answer Edric, invoke is a matlab method on COM object : here is the concerning page and i'm using the local cluster.
After some research I found that the handle of the COM object I use is lost when the parfor loop starts. How could I give this handle to the parfor loop, I cannot find in the documentation how to do that ?

Accedi per commentare.

 Risposta accettata

The problem appears to be with passing COM objects to the workers. You need to build the COM object on the workers. One way (if you've got R2015b or later) is to use parallel.pool.Constant, like so:
% Set up a COM object on each worker, and specify that "delete"
% should be called when "comConstant" is cleared.
comConstant = parallel.pool.Constant(@() actxserver(...), @delete);
parfor idx = 1:N
comValue = comConstant.Value;
% ... use comValue ...
end
If you've got a release of MATLAB prior to R2015b, you can use Worker Object Wrapper in place of parallel.pool.Constant.

3 Commenti

Thank you for your answer, it's very helpfull. But I have one more question : in your example, the COM object is created only for the parfor loop and deleted after. If I want to use an existing COM object that I want to keep after, can I write this like that :
Object=actxserver(...)
%...some calculation...
%comConstant=parallel.pool.Constant(Object);
parfor idx1:N
comValue = comConstant.Value;
%...use comValue...
end
No, you need to create the COM object in the process where you're going to use it (I think).
I have a problem very related to this issue. I would like to create not only COM object outside of the parfor but also to start application (Myapplication.ApplicationStart) outside the parfor and use the already started application inside parfor. I really appreciate if you could guide me.

Accedi per commentare.

Più risposte (0)

Community Treasure Hunt

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

Start Hunting!

Translated by