      subroutine mpipif(mpi_comm,n,outpi)
      include "mpif.h"
      double precision  mypi, sumpi
      double precision  h, sum, x, f, a
      double precision  pi
      parameter (pi=3.14159265358979323846)
      integer n, rank, procs, i, ierr
      integer mpi_comm
      double precision  outpi
      f(a) = 4.d0 / (1.d0 + a*a)

c     COMM
      call MPI_COMM_RANK(mpi_comm,
     &                   rank, ierr)
      call MPI_COMM_SIZE(mpi_comm,
     &                   procs, ierr)

      call MPI_BCAST(n,1,MPI_INTEGER,0,
     &               mpi_comm,ierr)
      if ( n .le. 0 ) goto 30
      h = 1.0d0/n
      sum  = 0.0d0
      do 20 i = rank+1, n, procs
         x = h * (dble(i) - 0.5d0)
         sum = sum + f(x)
 20   continue
      mypi = h * sum

      call MPI_REDUCE(mypi,sumpi,1,
     &                MPI_DOUBLE_PRECISION,
     &                MPI_SUM,0, 
     &                mpi_comm,ierr)

      outpi=sumpi
 30   continue
      return
      end
