
! A driver atmosphere model code for Multi-Process Handshaking utility
! to facilitate a plug & play style programming on
! using multiple component executables.
! Written by Yun (Helen) He and Chris Ding, NERSC/LBNL.


      program main

#ifdef MPH
      use MPH_Multi_Exec
      implicit none
      integer Global_World, COMM_cpl_pop
      integer global_sum1, global_sum2
      integer a2c(100)
#else
      implicit none
      include 'mpif.h'
      integer ierr, myProc
#endif

      integer CCM_World
      integer local_sum

      call MPI_INIT (ierr)
#ifdef MPH
      call MPH_setup_ME ("atmosphere", CCM_World)
      call MPI_COMM_DUP (MPI_COMM_WORLD, Global_World, ierr)
      if (myProc_global==0) call MPH_help ('on')
      if (myProc==0) call MPH_redirect_output ('ccm')
      write(*,*) myProc, ' in ccm === ', myProc_global, ' in global'
#else
!--- Stand-alone mode.
      call MPI_COMM_DUP( MPI_COMM_WORLD, CCM_World, ierr)
      call MPI_COMM_RANK (CCM_World, myProc, ierr)
#endif

!------ Do MPI operations as if no other components are involved -------

      call MPI_REDUCE (myProc, local_sum, 1, MPI_INTEGER, MPI_SUM,
     &                 0, CCM_World, ierr)
      if (myProc == 0) then
         write(*,*)' sum of local ranks in ccm is ', local_sum
      endif

#ifdef MPH

!--- Some additional tests with MPH

      call MPI_REDUCE (myProc_global, global_sum1, 1, MPI_INTEGER,
     &                 MPI_SUM, 0, CCM_World, ierr)
      call MPI_ALLREDUCE (myProc_global, global_sum2, 1, MPI_INTEGER, 
     &                    MPI_SUM, Global_World, ierr)
      if (myProc == 0) then
         write(*,*)' sum of global ranks in ccm is ', global_sum1
         write(*,*)' sum of global ranks in world is ', global_sum2
      endif

!--- Send a message to Proc 0 on coupler as sepecified in components.in 
!--- Note the use of MPH_global_id() function.

      a2c= 100
      if (myProc == 1) then
         write(*,*)'I am ccm 1 (g=3), send to cpl 0 (g=7)'
         call MPI_SEND (a2c, 100, MPI_INTEGER,
     &                  MPH_global_id("coupler", 0), 
     &                  1000, Global_World, ierr)
      endif

!--- to create a joined communicator for "coupler" and "ocean".
!--- ccm also has to take part in the creation 

      call comm_join ("coupler", "ocean", COMM_cpl_pop) 

#endif

      write(*,*)
      call MPI_FINALIZE (ierr)
      end
