! An atmosphere model subroutine for Multi-Process Handshaking utility
! to facilitate a plug & play style programming using single executable.
! Written by Yun (Helen) He and Chris Ding, NERSC/LBNL, October 2000.


      subroutine ccm3_8 (CCM_World)

      use MPH_module
      implicit none

      integer CCM_World, myProc, myProc_global
      integer local_sum, Global_World, COMM_cpl_pop
      integer lcoal_sum, global_sum1, global_sum2
      integer a2c(100)

      call MPI_COMM_DUP (MPI_COMM_WORLD, Global_World, ierr)

      call MPI_COMM_RANK (MPI_COMM_WORLD, myProc_global, ierr)
      call MPI_COMM_RANK (CCM_World, myProc, ierr)
      write(*,*) myProc, ' in ccm === ', myProc_global, ' in global'
      write(*,*) 'MPH_local_proc_id()=', MPH_local_proc_id()
      write(*,*) 'MPH_global_proc_id()=', MPH_global_proc_id()

!------ 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

!--- 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 local proc 0 of coupler 
!--- Note the use of MPH_global_id() function.

      a2c= 100
      if (myProc == 1) then
         write(*,*)'I am ccm 1 (g=7), send to cpl 0 (g=8)'
         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  MPH_comm_join ("coupler", "ocean", COMM_cpl_pop) 

      end subroutine
