!**********************************************************************
module nbi_mpi_com

  use iso_c_binding, only: fp=>c_double
  implicit none
  private :: fp
#ifdef __MPI
      include 'mpif.h'
#endif
      save

      ! local communicator
#ifdef __MPI
      integer :: nbi_comm = mpi_comm_world
#else
      integer :: nbi_comm = -1
#endif

      integer :: numprocs = 0
      integer:: myid,numprocd,myidd

      real(fp) :: stime1,stime2,stime3,stime4,stime5,     &
     &          stime6,stime7,stime8,stime9,stime10
      real(fp) :: stime31,stime32,stime41,stime42,stime51,stime52,  &
     &          stime61,stime62,stime71,stime72,stime81,stime82, &
     &          stime91,stime92,stime101,stime102
      real(fp) :: ttime11,ttime12,ttime21,ttime22,ttime31,ttime32, &
                ttime41,ttime42
      real(fp) :: htime1,htime2,htime_alc !3d halo allocation
      real(fp) :: htime3,htime4,htime_add !3d halo addn0 routine
      real(fp) :: htime5,htime6,htime_map !3d halo mapping
      real(fp) :: htime7,htime8,htime_all !3d halo all time in depbox_all
      real(fp) :: htime9,htime10,htime_at !3d halo atomic phys
      real(fp) :: htime11,htime12,htime_gm !3d halo geom
      real(fp) :: htime13,htime14,htime_aim !3d halo in nbi_aimingangle
      real(fp) :: htime15,htime16,htime_tr !3d halo in nbi_track_call_halo

      integer, parameter :: ncfnm = 256

      integer:: iscreen
      character(len=ncfnm)   :: iscreenfile


contains

  subroutine nbi_mpi_com_init

    ! initialize nbi

    ! caution -- nbi_comm must be correctly set before this is called!
    !            mpi_init must have been called by driver!

    integer :: mpierr

    !-----------------------------
    if(numprocs.eq.0) then
       
#ifdef __MPI
       call mpi_comm_rank(nbi_comm,myid,mpierr)
       call mpi_comm_size(nbi_comm,numprocs,mpierr)
#else
      numprocs=1
       myid=0
#endif
      numprocd = numprocs
      myidd = myid
      
    endif

  end subroutine nbi_mpi_com_init

end module nbi_mpi_com
!*********************************************************************
