How to use libpointer inside parfor loop?
14 views (last 30 days)
i have a problem with libpointer and parfor loops. I want to access several USB cable(same time) with callib(read/write) but i need libpointer for them.
If i try to open the channel and init the channel before parfor loop, i cant use the libpointer inside because they disappear.
Is there any way to access libpointer inside parfor loop without to open/init/close the whole usb channel each time ?
% here some description
%open the channel and save the pointer under pChannelHandleArr
% init the channel with given pChannelHandle
%transfer data to USB or read USB(SPI_Read) with given pChannelHandle
%clse channel with given handleArr
if my communication is every 500ms then i need to do this all again.
here is a small example of libpointer and my problem
fprintf(' before loop');
%create var as libpointer
pNumchannels = libpointer('uint32Ptr',255);
fprintf(' LOOP BEGINN');
%the var pNumchannels is empty here, the pointer is not there anymore
is there any way to open the channel, save the pointer var and use it inside parfor loops?
Edric Ellis on 11 Jan 2022
I think you should be able to do this using parallel.pool.Constant. I think you might need 2 of them in fact - one to manage the channel, and one for the libpointer. Something a little bit like this:
chanConst = parallel.pool.Constant(@setupChannel, @teardownChannel);
ptrConst = parallel.pool.Constant(@() libpointer('uint32Ptr',255));
parfor i = 1:N
% use ptrConst.Value
% Function to setup the channel. This must return a value. You will need to
% deal with the arguments to `calllib` somehow...
function v = setupChannel()
% Stuff like this
% I don't think you need an actual value?
v = ;
% Function to tear down the channel. This gets the value returned by