Parfor gives NaN when for does not

4 views (last 30 days)
I'm having issues parallelizing some code I use for a "homemade" spectrum analyzer.
It works fine in a for loop, however the
data=smooth(data,window);
Data=data(1+floor(window/2):length(data)-ceil(window/2));
part below (BoydFilter) causes issues (if I change it to Data=data; I get actual numbers) and my Data in ReadDAQTraces becomes a matrix of NaNs.
Can anyone tell me what I'm doing wrong?
*I and Q are arrays of 34.4 million doubles, if size is a problem.
function ReadDAQTraces(Folder,NumberOfTraces,Time,Points)
global SampleRate
SampleRate=2e6;
Data=zeros(NumberOfTraces,Points);
parfor i=1:NumberOfTraces
Data(i,:)=ReadInDAQ(Folder,i,Time,Points)
i
end
a=1;%for a debug stop
end
function Data=ReadInDAQ(Folder,i,Time,Points)
if ~ischar(i)
filename = strcat(Folder,'\I_Trace',int2str(i),'.csv');
else
filename = strcat(Folder,'\',i,'I.csv');
end
delimiter = {''};
formatSpec = '%f%[^\n\r]';
fileID = fopen(filename,'r');
dataArray = textscan(fileID, formatSpec, 'Delimiter', delimiter, 'TextType', 'string', 'EmptyValue', NaN, 'ReturnOnError', false);
fclose(fileID);
I = dataArray{:, 1};
if ~ischar(i)
filename = strcat(Folder,'\Q_Trace',int2str(i),'.csv');
else
filename = strcat(Folder,'\',i,'Q.csv');
end
delimiter = {''};
formatSpec = '%f%[^\n\r]';
fileID = fopen(filename,'r');
dataArray = textscan(fileID, formatSpec, 'Delimiter', delimiter, 'TextType', 'string', 'EmptyValue', NaN, 'ReturnOnError', false);
fclose(fileID);
Q = dataArray{:, 1};
Data=ApplyFilter(I,Q,Time,Points);
end
function Data=ApplyFilter(I,Q,Time,Points)
global SampleRate
SamplingRate=SampleRate;
window=Time*SamplingRate;
I=BoydFilter(I,window).^2+BoydFilter(Q,window).^2;
clear Q
Data=AverageDetector(I,Points);
end
function Data=BoydFilter(data,window)
if(window~=floor(window))
window
window=floor(window);
end
if(mod(window,2)==0)
-1*window
error('Boyd Filter Window is not an Odd Number')
end
data=smooth(data,window);
Data=data(1+floor(window/2):length(data)-ceil(window/2));
end
function Output=AverageDetector(Input,TotalPoints)
Output=zeros(1,TotalPoints);
AverageOver=floor(length(Input)/TotalPoints);
for i=1:TotalPoints
Output(i)=mean(Input((i-1)*AverageOver+1:i*AverageOver));
end
end

Accepted Answer

Edric Ellis
Edric Ellis on 11 Mar 2020
I'm not sure if this is the only problem, but the fact that you have a global variable set on the client and used by the workers is definitely a problem. The workers will not see the value set at the client (doc link). I suggest you modify your code to accept the SampleRate as an input parameter to your functions.
  1 Comment
Tim Woodworth
Tim Woodworth on 12 Mar 2020
Yep, that was it. Works now.
Thanks Edric!

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!