Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
wrong parfor results

Subject: wrong parfor results

From: Jean-Fran├žois

Date: 6 Oct, 2013 13:51:09

Message: 1 of 2

Hi

i have a program that outputs differents numbers when i use parfor or without. When i don't use parfor, i get the good results, and when i use parfor the numbers are wrong.

here is the most minimal code i could come up with:

        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);
        
                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;
        end;
    end;
    end;
end;

somehow, 'moy' is changing

thanks for help.

Jeff

Subject: wrong parfor results

From: Steven Lord

Date: 7 Oct, 2013 14:25:30

Message: 2 of 2


"Jean-Franois " <rufford.shop@gmail.com> wrote in message
news:l2rpsd$lpv$1@newscl01ah.mathworks.com...
> Hi
>
> i have a program that outputs differents numbers when i use parfor or
> without. When i don't use parfor, i get the good results, and when i use
> parfor the numbers are wrong.
>
> here is the most minimal code i could come up with:
>
> parfor p4 = 1: 10
>
> s2 = ['f1.txt'];

*snip*

> 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;
> end;
> end;
> end;
> end;
>
> somehow, 'moy' is changing

As Jan Simon indicated on MATLAB Answers:

http://www.mathworks.com/matlabcentral/answers/89321-parfor-makes-wrong-computations

you're overwriting the contents of f1.txt each iteration, so the only value
of moy that's written into the file is the one from the LAST iteration to
execute. The suggestion in Jan's third paragraph seem reasonable to me.

--
Steve Lord
slord@mathworks.com
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us