How to use a Com server handle in parfor?
4 次查看(过去 30 天)
显示 更早的评论
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.
0 个评论
回答(2 个)
Walter Roberson
2013-12-16
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.
0 个评论
Edric Ellis
2013-12-16
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 个评论
Edric Ellis
2013-12-17
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
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Parallel for-Loops (parfor) 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!