How to use a Com server handle in parfor?

6 views (last 30 days)
ParallelCom
ParallelCom on 16 Dec 2013
Commented: Edric Ellis on 17 Dec 2013
When I use a com server handle in the parfor, I get the error "Attempt to reference field of non-structure array".
Here is a sample of the code:
h=actxserver('......');
parfor i=1:4
b1=h.Do-something
c=b1{1}{1}
end
where Do-something generates an output in variant-type variable b1. Using "for" I can get the value for the variable of interest c but with "parfor" I get the error.

Answers (2)

Walter Roberson
Walter Roberson on 16 Dec 2013
My understanding is that each parfor worker is in a different process. The complete active COM server does not get copied to each process. This is similar to the way that you cannot do graphics inside a parfor, as only the parent process has the connection to the graphics session.
If you were to use SPMD then you could have one of the labs act as a server for the other labs, fielding requests via labsend() from the other labs, labreceive() on the one lab, get the responses from the server, and pass the responses back with labsend(). Or you could perhaps open a separate activex within each lab.

Edric Ellis
Edric Ellis on 16 Dec 2013
As Walter says, each worker is a separate MATLAB process. Also, each variable is transferred to the workers as if it had been saved to disk and then loaded again (although that's not exactly how it happens in practice). After this transfer, handle-type variables are no longer "connected". Does it work to do this:
parfor i = 1:4
h = actxserver('...');
...
end
? (If so, you might get further benefit by using a worker object wrapper around it - especially if you need 'h' in multiple PARFOR loops).
  2 Comments
ParallelCom
ParallelCom on 16 Dec 2013
Edited: ParallelCom on 17 Dec 2013
Thanks for the answer. Yes, this code works
parfor i = 1:4
h = actxserver('...');
...
end
but the problem is that creating h takes a long time and thus it would not be efficient to put it inside the parfor. I was not familiar with the worker object wrapper but it seems that it would fix this problem. The only problem is that I could not figure out the right syntax for the worker object wrapper. I use the following code to open a case from h using h.opencase but it does not work.
wrapper = WorkerObjWrapper(h,{});
parfor i=1:4
wrapper.opencase('Test.txt');
end
Edric Ellis
Edric Ellis on 17 Dec 2013
Because the actxserver object appears not to be transmitted correctly from the client to the workers, you need to build it on the workers. With WorkerObjWrapper, here's one way you could do that:
wrapper = WorkerObjWrapper(@actxserver, '...');
parfor i = 1:4
h = wrapper.Value;
... use h ...
end

Sign in to comment.

Categories

Find more on Parallel for-Loops (parfor) in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!