  PROGRAM Min
   IMPLICIT NONE

   INTEGER, PARAMETER :: MAX = 10000000

   DOUBLE PRECISION, DIMENSION(MAX) :: x
   DOUBLE PRECISION, DIMENSION(10)  :: my_min
   DOUBLE PRECISION :: rmin

   INTEGER :: num_threads
   INTEGER :: i, n
   INTEGER :: id, start, stop

   INTEGER, EXTERNAL :: OMP_GET_THREAD_NUM, OMP_GET_NUM_THREADS 




   DO  i = 1, MAX
     CALL RANDOM_NUMBER(x(i))
     x(i) = 10 * x(i) + 10
   END DO

   x(1234) = 4

!$OMP  PARALLEL  PRIVATE(i, id, start, stop, num_threads, n)

   num_threads = omp_get_num_threads()
   n = MAX/num_threads

   id = omp_get_thread_num()

   if ( id == 0 ) then
      print *, "num_threads = ", num_threads
      print *, "n = ", n
   end if

   ! ----------------------------------
   ! Find my own starting index
   ! ----------------------------------
   start = id * n + 1          !! Array start at 1

   ! ----------------------------------
   ! Find my own stopping index
   ! ----------------------------------
   if ( id <> (num_threads-1) ) then
      stop = start + n
   else
      stop = MAX
   end if

   ! ----------------------------------
   ! Find my own min
   ! ----------------------------------
   my_min(id+1) = x(start)

   DO i = start+1, stop
      IF ( x(i) < my_min(id+1) ) THEN
         my_min(id+1) = x(i)
      END IF  
   END DO

!$OMP END PARALLEL

   rmin = my_min(1)

   DO i = 2, num_threads
      IF ( rmin < my_min(i) ) THEN
         rmin = my_min(i)
      END IF  
   END DO


   print *, "min = ", rmin

   END PROGRAM

