Issue using a parfor loop to control two instruments at the same time

11 visualizaciones (últimos 30 días)
Hi all,
I am trying to control two instruments, 1) to tell a laser to sweep a range of wavelengths, 2) signal and read data from a NI data aquisition unit. As I am trying to record the signal as the laser sweeps I need both to run at the same time hence the parfor. I have tested the code individually and they are working my issue is getting them to work in a parfor loop. I am new to using libaries and setting up parpools so I might be doing something stupid (fingercrossed).
When I run the code below, i get the error ---> Library was not found
Below is the relevent code.
function output = main4
main1; %<------ Connects to laser and starts connection etc (WORKS FINE)
dq = MCTsetup %<----- Connects to data logger (WORKS FINE)
readout = 1;
delete(gcp('nocreate'));
parpool('AttachedFiles',{'MIRcatSDK'});
parfor i=1:2
if i == 1
pause(0.1);
runlasersweep; %<------ begins laser sweep scan (Code below)
end
if i == 2
readout = read(dq, seconds(5)); %<------ begins the data logger
end
end
main3; %<---- turn off laser emission (WORKS FINE)
output = readout;
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%runlasersweep - code
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function runlasersweep
load('MIRcatSDKconstants.mat');
fprintf('========================================================\n');
ret = calllib('MIRcatSDK','MIRcatSDK_StartSweepScan', ...
single(1200), single(1900), single(500), ...
MIRcatSDK_UNITS_CM1, uint16(1), true, uint8(1));
if MIRcatSDK_RET_SUCCESS == ret
fprintf(' Successful\n' );
else
% If the operation fails, unload the library and raise an error.
fprintf(' Failure\n' );
calllib('MIRcatSDK','MIRcatSDK_DeInitialize');
unloadlibrary MIRcatSDK;
error('Error! Code: %d', ret);
end
isScanInProgress = true;
isScanInProgressPtr = libpointer('bool', isScanInProgress);
isScanActive = false;
isScanActivePtr = libpointer('bool', isScanActive);
isScanPaused = false;
isScanPausedPtr = libpointer('bool', isScanPaused);
curScanNum = uint16(0);
curScanNumPtr = libpointer('uint16Ptr', curScanNum);
curScanPercent = uint16(0);
curScanPercentPtr = libpointer ('uint16Ptr', curScanPercent);
curWW = single(0);
curWWPtr = libpointer('singlePtr', curWW);
isTECinProgress = false;
isTECinProgressPtr = libpointer('bool', isTECinProgress);
isMotionInProgress = false;
isMotionInProgressPtr = libpointer('bool', isMotionInProgress);
units = uint8(0);
unitsPtr = libpointer('uint8Ptr', units);
fprintf('========================================================\n');
fprintf('Test: Get Scan Status\n');
while isScanInProgress
calllib('MIRcatSDK','MIRcatSDK_GetScanStatus', ...
isScanInProgressPtr, isScanActivePtr, isScanPausedPtr, ...
curScanNumPtr, curScanPercentPtr, curWWPtr, unitsPtr, ...
isTECinProgressPtr, isMotionInProgressPtr);
isScanInProgress = isScanInProgressPtr.value;
isScanActive = isScanActivePtr.value;
isScanPaused = isScanPausedPtr.value;
curScanNum = curScanNumPtr.value;
curScanPercent = curScanPercentPtr.value;
curWW = curWWPtr.value;
units = unitsPtr.value;
isTECinProgress = isTECinProgressPtr.value;
isMotionInProgress = isMotionInProgressPtr.value;
fprintf(['\tIsScanInProgress: %d \tIsScanActive: %d \tisScanPaused: %d', ...
'\tcurScanNum: %d \tcurWW: %.3f \tunits: %u \tcurScanPercent: %.2f', ...
'\tisTECinProgress: %d \tisMotionInProgress: %d\n'], ...
isScanInProgress, isScanActive, isScanPaused, curScanNum, curWW, ...
units, curScanPercent, isTECinProgress, isMotionInProgress);
pause(0.3);
end
end
  1 comentario
Matt J
Matt J el 1 de Jul. de 2022
Editada: Matt J el 1 de Jul. de 2022
Please show error messages in full.
Also, consider using parfeval rather than parfor.
Also, are you sure the two instrument units aren't supposed to be synchronized in some way? The Parallel Computing Toolbox will run parallel jobs asynchronously.

Iniciar sesión para comentar.

Respuesta aceptada

Raymond Norris
Raymond Norris el 1 de Jul. de 2022
I see a couple of potential issues
main1; %<------ Connects to laser and starts connection etc (WORKS FINE)
dq = MCTsetup %<----- Connects to data logger (WORKS FINE)
readout = 1;
Your setup is happening on the client side, but you're actual compute is on the workers. Getting a "library not found" error could be because
  • You've initialized on the client but the worker has no idea what you're talking about (the initialization didn't happen there)
  • The client path might have the dll/library on its path, but the workers don't, so it can't find the library
So the assignment to dq might happen in the parallel code. Not sure about the call to main1.
delete(gcp('nocreate'));
parpool('AttachedFiles',{'MIRcatSDK'});
I wouldn't suggest deleting and restarting a parallel pool within your code. This is better left outside of the code at it can be costly to retstart everytime you run your code. If MATLAB needs a pool, it'll start one. If you want to tailor it (size, attached files), start it up before calling your code (only if needed).
parfor i=1:2
if i == 1
pause(0.1);
runlasersweep; %<------ begins laser sweep scan (Code below)
end
if i == 2
readout = read(dq, seconds(5)); %<------ begins the data logger
end
end
Although you have a loop of two iterations, I think a spmd block would work better.
spmd
if labindex==1
pause(0.1)
runlasersweep
else
readout = read(dq, seconds(5));
end
end
As you've written it, readout is a temporary variable, not a sliced or reduction. Therefore, when you assign output below, you're really assigining it to the value 1 (the value before you entered the parfor), not what you assigned from calling read.
Secondly, as @Matt J points out, I think you need to synchronize the two workers. What happens if you read from the DAQ board before the laser starts? Is read a single shot? If not, what tells it to stop reading? With spmd, you'll be able to send message between the workers (i.e., when to start and stop).
main3; %<---- turn off laser emission (WORKS FINE)
output = readout;
Based on above, main3 might need to be done inside the parallel code.

Más respuestas (0)

Categorías

Más información sobre Parallel for-Loops (parfor) en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by