ccccccccccccccccccccccccc     Program 12.6     ccccccccccccccccccccccccc
c
cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc
c                                                                      c
c Please Note:                                                         c
c                                                                      c
c (1) This computer program is part of the book, "An Introduction to   c
c     Computational Physics," written by Tao Pang and published and    c
c     copyrighted by Cambridge University Press in 1997.               c
c                                                                      c
c (2) No warranties, express or implied, are made for this program.    c
c                                                                      c
cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc
c
      PROGRAM EULER_CONST
      INCLUDE 'mpif.h'
      DOUBLE PRECISION SUMI, SUM, SUM25
      PARAMETER (SUM25 = 0.577215664901532860606512D0)
      INTEGER N,K,IERR,IRANK,IPROC,IFINISH
C
      CALL MPI_INIT (IERR)
      CALL MPI_COMM_RANK (MPI_COMM_WORLD,IRANK,IERR)
      CALL MPI_COMM_SIZE (MPI_COMM_WORLD,IPROC,IERR)
C
      IF (IRANK.EQ.0) THEN
        PRINT*, 'Enter total number of terms in the series: '
        READ (5,*) N
      END IF
C
C Broadcast the total number of terms to every process
C
      CALL MPI_BCAST (N,1,MPI_INTEGER,0,MPI_COMM_WORLD,IERR)
      K = (N/IPROC)
      SUMI = 0.D0

      IF (IRANK.NE.(IPROC-1)) then
        DO   100 I = IRANK*K+1, (IRANK+1)*K
          SUMI = SUMI + 1.D0/DFLOAT(I)
  100   CONTINUE
      ELSE
        DO   200 I = IRANK*K+1, N
          SUMI = SUMI + 1.D0/DFLOAT(I)
  200   CONTINUE
      END IF
C
C Collect the sums from every process
C
      CALL MPI_REDUCE (SUMI,SUM,1,MPI_DOUBLE_PRECISION,
     *                 MPI_SUM,0,MPI_COMM_WORLD,IERR)
      IF (IRANK.EQ.0) THEN
        SUM = SUM - DLOG(DFLOAT(N))
        PRINT*, 'The evaluated Euler constant is ', SUM,
     *'with the estimated error of ', DABS(SUM-SUM25)
      END IF
C
      CALL MPI_FINALIZE(IFINISH)
      STOP
      END
