!-------------------------------------------------------!
! The current code has two purposes:                    !
! (1) using Intel and PGI compilers and see how         !
!     OMP_NUM_THREADS was set by default.               !  
! (2) demonstrate how private variables behave outside  !
!     of parallell regions.                             !
!-------------------------------------------------------!
! Xiaoxu Guan at LSU HPC.                               !
! March 07, 2016                                        !
!-------------------------------------------------------!

      module thpr
      implicit none
      integer            :: id,istart,iend
      integer, parameter :: idp = 8, nsize = 410 
      real(kind=idp) :: array_a(1:nsize),array_b(1:nsize),array_c(1:nsize)
      end module thpr


      program threadprivate_v0
      use thpr
      implicit none
      integer   :: i,nothread,nchunk,omp_get_num_threads,omp_get_thread_num

         array_c = 0.0_idp
      do i = 1, nsize
         array_a(i) = dble(i)
         array_b(i) = dble(i+i)
      end do


!$omp parallel private (id,nchunk,istart,iend)
         nothread = omp_get_num_threads() 
         id = omp_get_thread_num()
         nchunk = dble(nsize)/dble(nothread)
         istart = 1 + id*nchunk
         iend = istart + nchunk - 1
      if(id == nothread-1) then
         istart = 1 + id*nchunk
         iend = nsize
         nchunk = iend - istart + 1
         write(*,'(1x,a,3(i3,3x))') 'Hello from ', id, istart, iend
      end if
!$omp end parallel


      write(*,'(1x,a,i3)') 'Hello I am master. id = ', id


!$omp parallel private (id,istart,iend)
      write(*,'(1x,a,3(i3,3x))') 'Hello from ',id, istart,iend
!$omp end parallel


      end program threadprivate_v0
