c %Z% %M% %I% (IDS) %G%

C***     make the IO independent

      subroutine mp_init()

        implicit none

        include 'slgcm.mp'

        integer tmp

        tmp = CMMD_set_io_mode(6, CMMD_independent)
        if (tmp .ne. 0) then
           print *, 'Failure in setting io mode on unit 6'
           stop
        end if
        tmp = CMMD_set_io_mode(5, CMMD_independent)
        if (tmp .ne. 0) then
           print *, 'Failure in setting io mode on unit 5'
           stop
        end if
  
      end

C*** get self address

      function get_self_addr()
      
        implicit none
        
        include 'slgcm.mp'

        integer get_self_addr

        get_self_addr = cmmd_self_address()
        
      end


c*** syncrhonise

      subroutine mp_sync()

        implicit none
        
        include 'slgcm.mp'

        call cmmd_sync_with_nodes()
        
      end
      
C***  Clear timer
        function mp_timer_clear(timer)

        implicit none

        include 'slgcm.mp'

          integer mp_timer_clear
          integer timer
          
          call cmmd_sync_with_nodes()
          mp_timer_clear = cmmd_node_timer_clear(timer)
        end

C***  Start timer
        function mp_timer_start(timer)

        implicit none

        include 'slgcm.mp'

          integer mp_timer_start
          integer timer

          mp_timer_start = cmmd_node_timer_start(timer)
        end

C***  Stop timer
        function mp_timer_stop(timer)

        implicit none

        include 'slgcm.mp'

          integer mp_timer_stop
          integer timer

          mp_timer_stop = cmmd_node_timer_stop(timer)
        end

C***  Get Elapsed Time
        function mp_elapsed_time(timer)

        implicit none

        include 'slgcm.mp'

          real*8 mp_elapsed_time
          integer timer

          mp_elapsed_time = cmmd_node_timer_elapsed(timer)
        end

C***  Get Busy Time
        function mp_busy_time(timer)

        implicit none

        include 'slgcm.mp'

          real*8 mp_busy_time
          integer timer

          mp_busy_time = cmmd_node_timer_busy(timer)
        end

C***  Get Idle Time
        function mp_idle_time(timer)

        implicit none

        include 'slgcm.mp'

          real*8 mp_idle_time
          integer timer

          mp_idle_time = cmmd_node_timer_idle(timer)
        end


c*** Enable host program

        subroutine mp_enable_host()

          implicit none

          include "slgcm.mp"

          call cmmd_enable_host()

      end

c*** get host address

        function mp_host_addr()

          implicit none

          integer mp_host_addr

          include "slgcm.mp"

          mp_host_addr = cmmd_host_node()

      end


c*** get self address

        function mp_self_addr()

          implicit none

          integer mp_self_addr

          include "slgcm.mp"

          mp_self_addr = cmmd_self_address()

      end



c*** Enable host program

        subroutine mp_suspend_server()

          implicit none

          include "slgcm.mp"

          call cmmd_global_suspend_servers()

      end

c*** Check if message is pending

      
      function mp_msg_pending(node, tag)

        implicit none

        integer mp_msg_pending

        integer node,
     &          tag

      include 'slgcm.mp'

      integer tmp

        tmp = cmmd_msg_pending(node, tag)

        mp_msg_pending = tmp
      end



c*** send and receive

      
      function mp_send_and_rcvint( src_pn, src_tag, in,
     *                             dest_pn, dest_tag, out )

        implicit none

        integer mp_send_and_rcvint

        integer mp_timer_clear
        integer mp_timer_start
        integer mp_timer_stop

        integer src_pn,
     &           src_tag,
     &           in,
     &           dest_pn,
     &           dest_tag,
     &           out

      include 'slgcm.mp'
CT
         real*8 gcbtime, lcbtime, cabtime, llitime, loitime,
     &          brtime, cstime, cntime, reatime, fbtime, intime
         common /ntime/ cabtime, gcbtime, lcbtime, llitime, 
     &          loitime, brtime, cstime, cntime, reatime, fbtime,
     &          intime

D         real*8 gcbtime, lcbtime, cabtime
D         common /ntime/ cabtime, gcbtime, lcbtime

         real*8 mp_busy_time
CT
      integer tmp

C***    Timing Code 
	tmp = mp_timer_clear(1)
        if (tmp .eq. -1) then
           print *, 'Failure in clearing timer'
           stop
        end if

        tmp = mp_timer_start(1)
        if (tmp .eq. -1) then
           print *, 'Failure in starting timer'
           stop
        end if
C***    Timing Code 

        tmp = cmmd_send_and_receive(src_pn, src_tag, in,
     &                         size_of_int,
     &                         dest_pn, dest_tag, out,
     &                         size_of_int )

C***    Timing Code 
        tmp = mp_timer_stop(1)
        if (tmp .eq. -1) then
           print *, 'Failure in stopping timer'
           stop
        end if
        lcbtime = lcbtime + mp_busy_time(1)
C***    Timing Code 

        mp_send_and_rcvint = tmp
        end


C--
C*
C*    circulate wind buffers
C*

      subroutine circ_winbuf(src_pn, src_tag, inbuf, in_i,
     &                        in_j, dest_pn, dest_tag, outbuf, 
     &                        out_i, out_j)

        implicit none

        integer src_pn, src_tag,
     &           in_i, in_j,
     &           dest_pn, dest_tag,
     &           out_i, out_j

        real inbuf(in_i, in_j)
        real outbuf(out_i, out_j)

      integer mp_timer_clear
      integer mp_timer_start
      integer mp_timer_stop

        include 'slgcm.mp'
CT
         real*8 gcbtime, lcbtime, cabtime, llitime, loitime,
     &          brtime, cstime, cntime, reatime, fbtime, intime
         common /ntime/ cabtime, gcbtime, lcbtime, llitime, 
     &          loitime, brtime, cstime, cntime, reatime, fbtime,
     &          intime

D         real*8 gcbtime, lcbtime, cabtime
D         common /ntime/ cabtime, gcbtime, lcbtime

         real*8 mp_busy_time
CT
      
        integer tmp

D        print *, src_pn, " : Creating north buff"

C***    Timing Code 
	tmp = mp_timer_clear(1)
        if (tmp .eq. -1) then
           print *, 'Failure in clearing timer'
           stop
        end if

        tmp = mp_timer_start(1)
        if (tmp .eq. -1) then
           print *, 'Failure in starting timer'
           stop
        end if
C***    Timing Code 

        tmp = cmmd_send_and_receive(src_pn, src_tag, inbuf, 
     &                         in_i * in_j * 4,
     &                         dest_pn, dest_tag, outbuf,
     &                         out_i * out_j * 4)

C***    Timing Code 
        tmp = mp_timer_stop(1)
        if (tmp .eq. -1) then
           print *, 'Failure in stopping timer'
           stop
        end if
        lcbtime = lcbtime + mp_busy_time(1)
C***    Timing Code 

      end

