Program x06agfe

!     X06AGF Example Program Text

!     Mark 26 Release. NAG Copyright 2015.

!     .. Use Statements ..
      Use nag_library, Only: x06aaf, x06agf, x06ahf
!     .. Implicit None Statement ..
      Implicit None
!     .. Parameters ..
      Integer, Parameter               :: nout = 6
!     .. Local Scalars ..
      Integer                          :: ifail, nesting, nesting_set,         &
                                          num_inner, num_outer, total
!     .. Executable Statements ..
      Write (nout,*) 'X06AGF Example Program Results'
      Write (nout,*)

!     Enable the nesting of OpenMP parallel regions
      nesting_set = 1
      Call x06agf(nesting_set)

      ifail = 0
      num_inner = 3
      num_outer = 5

!     Set the number of OpenMP threads for an outer parallel region
      Call x06aaf(num_outer,ifail)

!     Spawn an OpenMP parallel region and have the master thread check whether
!     nesting of parallel regions has been enabled.

      total = 0

      !$Omp Parallel Shared (num_inner,total), Private (ifail,nesting),        &
      !$Omp   Default (None)

        nesting = x06ahf()

        !$Omp Master
          Write (nout,99999) 'Nesting enabled:', nesting
          Write (nout,*)
        !$Omp End Master

        ifail = 0
!       Set the number of OpenMP threads for an inner parallel region
        Call x06aaf(num_inner,ifail)

!       Spawn a nested parallel region
        !$Omp Parallel Shared (total), Default (None)

!         Add up the total number of threads in all teams
          !$Omp Atomic
            total = total + 1

        !$Omp End Parallel

      !$Omp End Parallel

      Write (nout,99999) 'Total number of threads requested:',                 &
        num_outer*num_inner
      Write (nout,99999) 'Total number of threads from sum: ', total
      Write (nout,*)

99999 Format (1X,A,I5)

    End Program x06agfe