I got NaN using parfor while the same code works with for loop

3 Ansichten (letzte 30 Tage)
Fan Li
Fan Li am 26 Jan. 2018
Beantwortet: Edric Ellis am 30 Jan. 2018
Hi everyone
I found I got NaN using parfor while the same code works with for-loop.
My input is very large. So I tested it with a small part of my input (only three times loop) for both for-loop and parfor loop. I got the same results. Then I used "dbstop if naninf" to debug.The code stopped at the beginning of parfor loop for parfor. While the code can give me the same results for the for-loop as if the there is no "dbstop if naninf". Finally, I tested with my entire input for both loop without "dbstop if naninf". The parfor gave me NaN to the arrays which should have values.While the for loop works fine. The part code of parfor is listed below.
parfor i=1:num_atom
tdelta_f(:,i)=xcorr(atom_for_v(:,1,i),atom_for_v(:,1,i),'biased')+xcorr(atom_for_v(:,2,i),atom_for_v(:,2,i),'biased')
+xcorr(atom_for_v(:,3,i),atom_for_v(:,3,i),'biased');
tdelta_fv(:,i)=xcorr(atom_for_v(:,4,1),atom_for_v(:,1,i),'biased')+xcorr(atom_for_v(:,5,1),atom_for_v(:,2,i),'biased')
+xcorr(atom_for_v(:,6,1),atom_for_v(:,3,i),'biased');
end
(1)It is bug of my code or bug of parfor?
(2)If my code is fine. How can I use parfor for my code in this case?
Fan Li

Antworten (2)

Fan Li
Fan Li am 30 Jan. 2018
Hi Edric
This a part of my code. It reads the data and does the correlation calculations. You can reproduce the problem with code and attached files. Thanks for your time.
%tot_step=1000000;
tot_step=40;%use the integer which is the multiple of ten, the maximum is 40.
dump_fre=10;
num_atom=4323;
file='outputfile.txt';
save1='3.8atom_for_v_spce_rerun.mat';
num_dump=(tot_step/dump_fre)+1;
num_col=7;
atom_for_v=zeros(num_dump,num_col-1,num_atom,'single');
temcell=zeros(1,num_col+1,'single');
tdelta_f=zeros(2*num_dump-1,num_atom,'single');
tdelta_fv=zeros(2*num_dump-1,num_atom,'single');
fileID = fopen(file);
if (fileID==-1)
disp('Can not open the file');
else
disp('Open the file sucessfully');
end
dbstop if naninf;
disp('Reading the data ...');
for i=1:num_dump
%skip the 9 lines header
for j=1:9
tline=fgetl(fileID);
end
%read the data and retrieve them to the multimensional array according
%to the atom id
for k=1:num_atom
tline=fgetl(fileID);
temcell=strsplit(tline);
atom_for_v(i,:,k)=cellfun(@str2double,temcell(:,2:7));
end
end
fclose(fileID);
save(save1,'atom_for_v','-v7.3');
disp ('Calculating the correlation function ...');
%it can pass if use for instead of parfor
parfor i=1:num_atom
tdelta_f(:,i)=xcorr(atom_for_v(:,1,i),atom_for_v(:,1,i),'biased')+xcorr(atom_for_v(:,2,i),atom_for_v(:,2,i),'biased')+xcorr(atom_for_v(:,3,i),atom_for_v(:,3,i),'biased');
tdelta_fv(:,i)=xcorr(atom_for_v(:,4,1),atom_for_v(:,1,i),'biased')+xcorr(atom_for_v(:,5,1),atom_for_v(:,2,i),'biased')+xcorr(atom_for_v(:,6,1),atom_for_v(:,3,i),'biased');
end
aucf_f=sum(tdelta_f,2);
crof_fv=sum(tdelta_fv,2);

Edric Ellis
Edric Ellis am 30 Jan. 2018
dbstop if naninf always causes execution to stop during setup of a parfor loop. This turns out to be expected behaviour (parallel_function.m has an explicit assignment to Inf), and I believe there is nothing to worry about - either in your code, or the implementation of parfor.

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by