parfor makes wrong computations

hi
i've got some code that works correctly in normal mode, but outputs wrong results when run in parallel mode.
here is the simplest code i had to show you
parfor p4 = 1: 10
s2 = ['f1.txt'];
om2=pi*[linspace(0,0.6080,60),linspace(0.6082,1,40)];
D2=[zeros(1,60),exp(-1i*om2(1:40)*25)];
W2=[1000*ones(1,60),1*ones(1,1),1*ones(1,39)];
[b2,a2,e2]=mpiir_l2(25,23,om2,D2,W2,0.98);
Hhigh = dfilt.df1(b2,a2);
[h w] = freqz(Hhigh, 65536);
moy = 0;
p2 = round((0.6086+0.0114)*65536)
while p2<65536+1
mag = abs(h(p2));
if (mag > 1)
maxx = (mag - 1);
end;
if (mag < 1)
maxx = (1 - mag);
end;
moy = moy + maxx;
p2 = p2 +1;
end;
moy = moy / (65536-round((0.6086+0.0114)*65536));
f = fopen(s2, 'w');
fprintf(f, '%d\n', p4);
fprintf(f, 'moy(<0.007)=%f \n', moy);
fclose(f);
end;
thanks
Jeff

2 Comments

Matt J
Matt J on 6 Oct 2013
Edited: Matt J on 6 Oct 2013
We cannot run it. The function mpiir_l2 is unavailable. You also haven't told us how we're to distinguish good output from bad.
okay i'm sorry about that.
here is the full Matlab file: http://khaelis.com/tmp/nouveaudossier.zip
thanks for helping me

Sign in to comment.

 Accepted Answer

Jan
Jan on 6 Oct 2013
Edited: Jan on 6 Oct 2013
No. The results of parfor are correct. Although this opinion is based on the title of your question only, the experiences in the Matlab forums show, that 99.9% of the assumed bugs in Matlab's core functions are caused by bugs in the program.
Opening one file for writing inside a parfor loop is not a good idea. You cannot control in which order the multiple threads are processed, so you cannot predict, which of the 10 iterations will overwrite the file the last time.
Better store the results in a cell and create the file after the loop. Beside the consistency of the result, this is important for the efficiency also. Otherwise the 10 loops compete for the resource to access the file and the collisions will reduce the speed massively.

3 Comments

i've tried what you've said and it doesn't work.
i've narrowed down the program to a simpler version:
moy_=zeros(10);
parfor p4 = 1: 10
[b2,a2,e2]=mpiir_l2(9+p4,22,om2,D2,W2,0.98);
moy_(p4) = b2(1);
end;
for p4 = 1:10
s2 = ['fb' int2str(p4) '.txt'];
f = fopen(s2, 'w');
fprintf(f, '%d\n', p4);
fprintf(f, 'moy(<0.007)=%f \n', moy_(p4));
fclose(f);
end;
and the result is the same : differents results in parallel or normal modes.
I can't give you the function because it's the biggest part of my code and it isn't opensource. I know it would be simpler if you could try the code yourself, but i really can't.
Jeff
@Jean-Francois: I do not get the problem. These are two completely different loops. So what are you comparing? How large is the difference? Please compare it to eps(result), whatever the result is.
We cannot see the code, we do not know the difference, then we cannot guess the problem. How can we help you?
okay i'm sorry about that.
here is the full Matlab file: http://khaelis.com/tmp/nouveaudossier.zip
thanks for helping me
Jeff

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!