- Make sure you perform the initialisation steps on the workers
- Use the form of parallel.pool.Constant constructor that takes a function handle so that the object is built directly on the worker. (The code you have builds the object on the client and then attempts to send it to the worker)
Pass NET-Object to parallel function (parfeval)
5 views (last 30 days)
Show older comments
Hello,
I am trying to pass a NET-Object to a function running on a parallel pool in Matlab.
While I thought "parallel.pool.Constant" is the way to do it - as it works fine for other objects (like database connection) - it doesn't work in this case.
As I don't receive an error message, I can't find out why it won't work as it should.
Also the display function doesn't show anything.
Sample-Code:
%% Import Ads.dll
AdsAssembly = NET.addAssembly('D:\TwinCat3\AdsApi\.NET\v4.0.30319\TwinCAT.Ads.dll');
import TwinCAT.Ads.*;
%Create TcAdsClient instance
tcClient = TcAdsClient;
tc = parallel.pool.Constant(tcClient); %Create pool constant to pass object
pool = gcp();
parfeval(pool,@pfcn,0,tc);
function pfcn(tc)
disp(tc.Value);
tcClient = tc.Value;
tcClient.Connect(851); %Connect to ADS port 851 on the local machine
end
An example where it works fine (OPC UA Client):
pool = gcp();
Q = parallel.pool.DataQueue;
serverList = opcuaserverinfo('192.168.60.200');
hsInfo = findDescription(serverList, 'K6');
uaClient = opcua(hsInfo);
opc_const = parallel.pool.Constant(uaClient);
parfeval(pool, @pfcn, 0, Q, opc_const);
listener = afterEach(Q, @disp);
function pfcn(Q, opc_const)
par_uaClient = opc_const.Value;
connect(par_uaClient);
conn_state = isConnected(par_uaClient);
send(Q, par_uaClient)
end
0 Comments
Answers (1)
Edric Ellis
on 14 Apr 2020
I do think parallel.pool.Constant is probably part of the answer here, but you need to make two changes:
Something like this:
% Call NET.addAssembly on all workers
parfevalOnAll(@() NET.addAssembly('D:\TwinCat3\AdsApi\.NET\v4.0.30319\TwinCAT.Ads.dll'), 0);
% Build parallel.pool.Constant using function handle
tcClient = parallel.pool.Constant(@TwinCAT.Ads.TcAdsClient);
If there's a specific "cleanup" function needed for your object, you can specify this as the second argument to the parallel.pool.Constant constructor.
0 Comments
See Also
Categories
Find more on Parallel Computing Fundamentals in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!