Passong COM objects within a parfor loop
1 view (last 30 days)
Show older comments
Hi,
it has been months I tried to couple properly a matlab optimization with an external software through an ActiveX sever interface. If I use a simple for I can pass whatever I want within the loop. The objective is to keep one COM instance open and "drive" my software (Finite Element Package) from matlab. The issue arises when I want to exploit the parallel computation toolbox. My idea is to create the COM objects/open the servers for all the different cores to be used during the optimization, before the loop, for then retrieving them. However I need to access the COM object from within the parfor. When I try to pass my instance within the function that is evaluated within every parfor this will show the following mistake:
An UndefinedFunction error was thrown on the workers for 'MN7'. This might be because the file containing 'MN7' 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.
However, following the details of the error seems not to help me, as actually I cannot attach or specify a file. I need to pass the COM object.
The alternative is at every parfor loop iteration open a new active X server. Then close it and reopen it again. Nonetheless this has two drawbacks: I need to get read of all the Log Files created by the software I am calling via ActiveX (I solved it), and since I have a floating license for my FEA software sometimes the license has some brief network "shortages" which prevent activeX to start properly again. Also, opening and closing ActiveX servers takes time and slows down the entire loop, so I need to avoid it.
If you have any suggestion I will really appreciate.
Nick
0 Comments
Answers (1)
Edric Ellis
on 21 Jul 2017
This sounds like a job for parallel.pool.Constant. This lets you build an object on each worker once, and then use it multiple times within your parfor loop. For example:
c = parallel.pool.Constant(@buildCOMObject, @deleteCOMObject);
parfor idx = 1:N
useCOMObject(c.Value);
end
Here buildCOMObject is a function returning your COM object, deleteCOMObject is a function that knows how to clean up the COM object (you can omit this if it is not required).
0 Comments
See Also
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!