      program cbsu3time2mpib ! Berg Feb 5 2009. 
C MPI checkerboard for SU3.
C Version a: Plain    send/receive (mpi a-extensions to b-extensions).
C Version b: Buffered send/receive (mpi b-extensions to a-extensions).
C SU3 nhit Cabibbo-Marinari with double-layered torus and two beta values.
C For lat2=.true.: recalculate action before each measurement
C                  (necessary when also nlat=2).
C Update action before each measurement. Needed for nlat=2, lat2=.true..
C Average plaquettes, spacelike, timelike, Polyakov loops measurements.      
      include '../../Libs/Fortran/implicit.08'
      include 'mpif.h'
      include '../../Libs/Fortran/constants.08'
      character cmy*4
      include 'latmpi.par'
      include 'mc.par'
      include '../../Libs/MPI_par/common_cblat.f'
      include '../../Libs/MPISU3/common_cbsu3.f'
      ltest=.true.
      ltest=.false.
      call mpi_init(ierr)
      call mpi_comm_rank(mpi_comm_world,my_id,ierr)
c
      if(my_id.eq.0) write(iuo,'(/,"MPI cbsu3time2mpib:",
     &  "nlat,nhit,beta0,beta1 =",I2,I3,2F12.8)') nlat,nhit,beta0,beta1
      if(nhit.le.0.or.nhit.gt.3) ltest=.true.
c
      write(cmy,'(i4.4)') my_id
      call cbsu3_2init_mpi             ! Initialization.
      if(.not.lswp) ltest=.true.
      if(ltest) then ! --------------------------------------------
        if(my_id.eq.0) write(iuo,'(/,"cbsu3time2mpib STOP ltest.",/)')
        call mpi_finalize(ierr)
      stop "cbsu3_2mpi: ltest."
      endif ! ----------------------------------------------------
      call cbsu3_actdif_mpi(actdif,actsum1,actsum2,my_id) 
      if(my_id.eq.0) write(iuo,'(/,"  call write_act_mpi:")')
      call write_act_mpi(my_id,izero,acpt,actdif,actsum1,actsum2) 
      call mpi_barrier(mpi_comm_world,ierr)
c
      if(my_id.eq.0) write(iuo,'(/," Equilibration started...")')
      do iequi=1,nequi ! Sweeps for reaching equilibrium.
        call cbsu3_2hbnhit_mpi(my_id) ! Heatbath on double-layered torus.
      end do ! Check action:
      call cbsu3_actdif_mpi(actdif,actsum1,actsum2,my_id) 
      acpt=acpt/a0prop
      call write_act_mpi(my_id,izero,acpt,actdif,actsum1,actsum2) 
      if(my_id.eq.0) write(iuo,'(/," Equilibration, actdif done,",
     &               " acpt, a0prop =",F10.4,G15.6)') acpt,a0prop
      call mpi_barrier(mpi_comm_world,ierr)      
c
      call mpi_finalize(ierr)
      stop
      end

      BLOCK DATA
      include '../../Libs/Fortran/implicit.08'
      include '../../Libs/Fortran/constants.08'
      include 'latmpi.par'
      parameter(npointer=nd*(msc+mbcsh))
      include '../../Libs/MPI_par/common_cblat.f'
      include 'latmpi.dat'
C For test purposes only:
      data ipf1/npointer*mione/, ipf2/npointer*mione/
      data ipb1/npointer*mione/, ipb2/npointer*mione/ 
      END


C Modular Fortran routines:
      include '../../Libs/Fortran/isfun.f'
      include '../../Libs/Fortran/ipointer.f'
      include '../../Libs/Fortran/ixcor.f'
      include '../../Libs/Fortran/lat_init.f'
      include '../../Libs/Fortran/nsfun.f'
      include '../../Libs/Fortran/razero.f'
      include '../../Libs/Fortran/ranmar.f'
      include '../../Libs/Fortran/rmafun.f'
      include '../../Libs/Fortran/rmaset.f'
      include '../../Libs/Fortran/rmasave.f'
      include '../../Libs/Fortran/sum_fun.f'
C LGT Checkerboard:
      include '../../Libs/Fortran/istoic.f' ! Checkerbord ic from is.
C SU2 routine:
      include '../../Libs/Fortran/su2_a0nhit.f'   ! Generates SU2 a0.

C SU3 modular routines:
      include '../../Libs/SU3/su3init0.f'     ! SU3 matrix put to 0.
      include '../../Libs/SU3/su3init1.f'     ! SU3 matrix put to 1.
      include '../../Libs/SU3/su3add_m_m.f'
      include '../../Libs/SU3/su3addb_m_m.f'
      include '../../Libs/SU3/su3copy_m_m.f'
      include '../../Libs/SU3/su3mult_mh_mh_m.f'
      include '../../Libs/SU3/su3mult_m_mh_m.f'
      include '../../Libs/SU3/su3mult_m_m_m.f'
      include '../../Libs/SU3/su3reunit.f'
      include '../../Libs/SU3/su3_b2nhitupdt.f' ! nhit update for beta(plq).
      include '../../Libs/SU3/csu3_start.f'     ! Checkerboard start conf.

C Modular MPI routines:
      include '../../Libs/MPI/lat2a_init.f' ! Double-layered torus: 
      include '../../Libs/MPI/lat2b_init.f' ! Couple lattice 1 and 2.

C LGT MPI checkerboard routines:
      include '../../Libs/MPI_par/cblat_init.f'
      include '../../Libs/MPI_par/cblgtpnt2.f'      ! Final LGT pointers.
      include '../../Libs/MPI_par/cblgtpointer.f'
      include '../../Libs/MPI_par/cbpointer.f'
      include '../../Libs/MPI_par/cpointer.f'
      include '../../Libs/MPI_par/isglobal.f'
      include '../../Libs/MPI_par/write_act_mpi.f' ! Write act (nlat=1,2).

C SU3 MPI checkerboard routines:
      include '../../Libs/MPISU3/cbsu3_act_mpi.f'   ! Calculates action.
      include '../../Libs/MPISU3/cbsu3_actdif_mpi.f'! Check action diffs.
      include '../../Libs/MPISU3/cbsu3_bnd_mpi.f'   ! Gather boundaries.
      include '../../Libs/MPISU3/cbsu3_bnd1a_mpib.f' ! Gather boundaries.
      include '../../Libs/MPISU3/cbsu3_bnd1b_mpib.f' ! Gather boundaries.
      include '../../Libs/MPISU3/cbsu3_bnd2a_mpib.f' ! Gather boundaries.
      include '../../Libs/MPISU3/cbsu3_bnd2b_mpib.f' ! Gather boundaries.
      include '../../Libs/MPISU3/cbsu3_wloops_mpi.f' ! Measures Wloops.
C Double-layered torus SU3 routines:
      include '../../Libs/MPISU3/cbsu3_2init_mpi.f'  ! SU3 Initialization.
      include '../../Libs/MPISU3/cbsu3_2hbnhit_mpi.f'! MCHB updating.
      include '../../Libs/MPISU3/cbsu3_bstaple1.f'   ! Staple checkerb. 1.
      include '../../Libs/MPISU3/cbsu3_bstaple2.f'   ! Staple checkerb. 2.
      include '../../Libs/MPISU3/cbsu3_iba_mpi.f'    ! Define iba arrays.
