Find maximum with OpenMP?

General OpenMP discussion

Find maximum with OpenMP?

Hi,

I wonder if you could help me with a little problem I have with OpenMP in a Fortran 95-program...

I have the following Fortran 95-code which I try to run in parallel with OpenMP... It is supposed to find the largest element (Amax) of the calculation...
BUT as it is now it seems that when the threads exit the do-loop, they have each found the max-value (priv_max) in their part of the calculation, as they should - No problem there!
But then they have to compare these results between the different threads and then things go wrong! It seems the code just ends up giving the value of the last thread to enter the comparison... It seems to be a problem with the threads not beeing able to compare with the shared memory???

Here is the code:

Code: Select all
`Amax = 0.0_dbl!\$OMP PARALLEL DEFAULT(SHARED) PRIVATE(j,alpha,beta,nu,A,priv_max,priv_numax)priv_max = 0.0_dbl!\$OMP DOdo j = 1,Nspec    nu = nu_min+dF*(j-1)    ! Calculate alpha and beta:    call CalcAlphaBeta(t,f,w,nu,alpha,beta)       ! Calculate amplitude and phase:    A = sqrt(alpha**2 + beta**2)         if (A > priv_max) then        priv_max = A        priv_numax = nu    endifenddo!\$OMP END DO!\$OMP FLUSH (Amax)if (priv_max > Amax) then    !\$OMP CRITICAL        if (priv_max > Amax) then            print *, priv_max            Amax = priv_max            numax = priv_numax        endif    !\$OMP END CRITICALendif!\$OMP END PARALLEL`

What is going on?!
If anything is unclear, please just ask... I could really use your help!

/Rasmus
Killer_R

Re: Find maximum with OpenMP?

Unless I am totally brain dead this morning (which is always possible), the only problem I see is the use of flush with a list. Because of how flush with a list has been defined, it is usually better to use flush without a list. Try that first. If that doesn't work, then try adding a flush after the update to Amax, like the following:
Code: Select all
`!\$OMP FLUSH                                   <=== there are problems with flush using listsif (priv_max > Amax) then    !\$OMP CRITICAL        if (priv_max > Amax) then            print *, priv_max            Amax = priv_max            !\$omp flush                       <=== note added flush should not be necessary            numax = priv_numax        endif    !\$OMP END CRITICAL`

The added flush should not be needed, but I am trying to figure out if the implementation of OpenMP you are using has a problem. Which begs the question of what implementation are you using??

The only other comment I have, is that with a correct implementation, you should be able to speed this up a little by adding a "nowait" to your "!\$omp end do" directive.
ejd

Posts: 1025
Joined: Wed Jan 16, 2008 7:21 am

Re: Find maximum with OpenMP?

I tried what you said, but it didn't seem to make any difference...
The code now looks like this:

Code: Select all
`   Amax = 0.0_dbl   !\$OMP PARALLEL DEFAULT(SHARED) PRIVATE(j,alpha,beta,nu,A,priv_max,priv_numax)   priv_max = 0.0_dbl   !\$OMP DO   do j = 1,Nspec      nu = nu_min+dF*(j-1)         ! Calculate alpha and beta:      call CalcAlphaBeta(t,f,w,nu,alpha,beta)            ! Calculate amplitude and phase:      A = sqrt(alpha**2 + beta**2)              if (A > priv_max) then         priv_max = A         priv_numax = nu      endif   enddo   !\$OMP END DO NOWAIT   !\$OMP FLUSH   if (priv_max > Amax) then      !\$OMP CRITICAL         if (priv_max > Amax) then            print *, priv_max            Amax = priv_max            !\$OMP FLUSH            numax = priv_numax         endif      !\$OMP END CRITICAL   endif   !\$OMP END PARALLEL`

I'm using the Portland Group Fortran compiler "pgf95" on a 64 bit machine.
It is compiled with the command:
pgf95 -O3 -mp -c FindSpectrumMax.f95
Killer_R

Re: Find maximum with OpenMP?

WTF!?

I just found out that it seems to work if I put this line after the code shown above:
Code: Select all
`print *, 'Result: ', Amax`

But if remove the line again, it doesn't work?! Any ideas?
Killer_R

Re: Find maximum with OpenMP?

By the way, the compiler is version 6.1!
Killer_R

Re: Find maximum with OpenMP?

Unfortunately, this usually means that the compiler is doing something that it shouldn't be doing. I am afraid that my suggestion at this point is to post this to one PGI's forums on their web site and see what they have to say. Let me know what they have to say.
ejd

Posts: 1025
Joined: Wed Jan 16, 2008 7:21 am