program WEImpi_main
  use sep
  use mpi_sep 
  use wei_util 
  use wei_par 
  use wei_pro 
  use wei_tag 
  use wei_cls 
  use weiop_cig 
  use weiop_mwc 
  use weiop_sll 
  use weiop_ssf 
  use weiop_wem 

  implicit none
  character(len=3) :: operation
  integer :: stat 

  integer :: mpierr 

  !----------------------------------------------------------------
  call MPI_INIT(mpierr)
  call sep_init()
  !----------------------------------------------------------------
  !! init params for MPI and OMP
  call weipar_init()
  call weipar_report()

  if(wpar%nmpi>0) then
     if(wpar%impi==0) then
        call MPI_SEP_SEND_ARGS(wpar%nmpi,1,0)
     else
        call MPI_SEP_RECEIVE_ARGS()
     end if
  end if

  !! init tag names on all nodes
  call weitag_init()

  !! init process parameters on the MASTER
  if(wpar%impi==0) call weipro_init()
  !! broadcast parameters from MASTER to NODES
  if(wpar%nmpi >1) call weipro_bcast()
  if(wpar%impi==0) call from_param("operation",operation,'mig')
  if(wpar%nmpi >1) call MPI_BCAST(operation, 3,MPI_CHARACTER,0,MPI_COMM_WORLD,mpierr) 
  !----------------------------------------------------------------
  call weicls_init(SLin=weiop_sllN_init &
  ,                WCin=weiop_mwcN_init &
  ,                FKin=weiop_wem3_init &
  ,                FXin=weiop_ssf3_init &
  ,                IGin=weiop_hcig_init )
  !----------------------------------------------------------------
  if(wpar%impi==0) call weipro_report()
  !----------------------------------------------------------------
  if(wpar%impi==0 .and. wpro%verb) then
     if(wpro%adj) then
        call weimessage("start adjoint operator")
     else
        call weimessage("start forward operator")
     end if
  end if
  stat = weicls(wpro%adj,wpro%add,wtag%R,wtag%D &
  ,              SLop=weiop_sllN &
  ,              WCop=weiop_mwcN &
  ,              FKop=weiop_wem3 &
  ,              FXop=weiop_ssf3 &
  ,              IGop=weiop_hcig)
  if(wpar%impi==0 .and. wpro%verb) call weimessage("OK")
  !----------------------------------------------------------------  
  !! shutdown MPI
  call MPI_FINALIZE(mpierr) 
  !----------------------------------------------------------------
  call exit(0)
end program WEImpi_main
