Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
28445 Discussions

!$OMP ATOMIC and !$OMP CRITICAL directives inside conditional statements

Ioannis_K_
New Contributor I
632 Views

Hello,

As the title of my post suggests, I wanted to ask whether it is possible to combine OpenMP ATOMIC/CRITICAL directives inside an IF.... ENDIF statement.

An example code structure is provided below:

!===========================================================

!$OMP PARALLEL DO

do i1 = 1,100000

     if(IndexV(i1).gt.0) then

          !$OMP ATOMIC

           Kff( IndexV(i1) ) = Kff( IndexV(i1) ) + 1

     endif

end do

!=============================================================

Will the !$OMP ATOMIC directive inside the above conditional statement work?

Many thanks in advance!

0 Kudos
4 Replies
Steve_Lionel
Honored Contributor III
632 Views

You need a !$OMP END ATOMIC, but I don't see any reason it shouldn't work.

0 Kudos
WHARR5
New Contributor I
632 Views

!Hello Ioannis,

 !I have run your DO Loop (incorporating your IF-ENDIF structure) as a stand-alone program and it executes just fine! I have added a MAXTHREAD statement just to make sure OMP is in effect, and I have initialized your two arrays (as I am sure you have done earlier in your program). Otherwise, I executed the program in Visual Studio's !RELEASE mode, using Intel's Fortran compiler. Here is the program:

     program TestATOMIC7318

    USE OMP_LIB

     implicit none

     ! Variables

    INTEGER::MAXTHREADS

    INTEGER::i1,IndexV(100000),kff(100000)   

     ! Body of TestATOMIC7318

     MAXTHREADS=OMP_GET_MAX_THREADS()

    WRITE(*,'(24X,A,I3/)')'Local Max Threads by OpenMP: ',MAXTHREADS

     IndexV=0;kff=0   

    !$OMP PARALLEL DO

     do i1 = 1,100000

         if(IndexV(i1).gt.0) then

             !$OMP ATOMIC

             Kff( IndexV(i1) ) = Kff( IndexV(i1) ) + 1

         endif

     end do

     Print*,'Hello World'

       end program TestATOMIC7318

 !Regards,

!Bill

 

0 Kudos
WHARR5
New Contributor I
632 Views

Hello Ioannis,

Initializing your 'IndexV' array with all 0s, as I did, prevents the program from ever entering your IF-ENDIF structure. Sorry about that. However, I have run it with initial values put into array 'IndexV' and the program continues to execute without error.

Regards,

Bill

0 Kudos
Ioannis_K_
New Contributor I
632 Views

Hello Bill,

Thank you for looking into this. I have also managed to successfully run a program with code similar to the one I originally posted. I still decided to ask in the forum, to double-check and make sure that I could use the ATOMIC directive in the specific case, as I wanted to use it for a much more complicated program of mine. I wanted to ensure that I would not spend a significant amount of time making changes which could not work!

I can verify that using the ATOMIC directive in conditional statements of my "complicated program" works fine. In fact, I see a significant enhancement in performance compared to the previous version, which was using a rather large !$OMP CRITICAL construct instead of using individual !$OMP ATOMIC directives at specific lines.

Yannis

0 Kudos
Reply