c @(#) lsd.f 1.6 (IDS) 12/19/94


C
C     Find the farthest node that needs to be referred by
C     the current node to perform an interpolation.
C
C--

      subroutine get_lsd_comm_rng(width, lo1, up1, jj, max_iter,
     *                            ny, ny0, nysth)

      include "slgcm.slgprm"
      include "slgcm.mp"

      integer lo1,
     &        up1,
     &        width,
     &        max_iter(2),
c
c    !!! Change Dimensions of jj to jj(imp3, max_col_p3)
c
C     &        jj(4,4),
     &         jj(imp3, max_col_p3),
     &        ny, ny0, nysth

        integer mp_timer_clear
        integer mp_timer_start
        integer mp_timer_stop
        real*8 mp_elapsed_time
        real*8   etime

      implicit none

        integer lmax_iter(2),
     &           dist,
     &           ownp2, ownp3, i, j, tmp
  
C        lmax_iter(0) = 0
	lmax_iter(1) = 0
	lmax_iter(2) = 0
        dist      = 0
  
          do j = lo1, up1
           do i = 1, imp3
C          do i = 1, 4
              ownp2 = int((jj(i,j)+2) / width)
              ownp3 = int((jj(i,j)+3) / width)

              tmp = mod(jj(i,j)+2, width)
              if (tmp .eq. 0) then
                 ownp2 = ownp2 -1
              else if (ownp2 .ge. ny) then
                 ownp2 = ny -1
              endif
                 
              if (mod(jj(i,j)+3, width) .eq. 0) then
                 ownp3 = ownp3 -1
              else if (ownp3 .ge. ny) then
                 ownp3 = ny -1
              endif

C
C	      Shift address of current node ny0 to 0.  
C             Thus ownp2, ownp3 will be shifted accordingly.
C             Then try to find the shortest path to the further
C             of the two in either the southerly or northerly 
C             direction.
C
		ownp3 = ownp3 - ny0
		ownp2 = ownp2 - ny0

		if (ownp3 .lt. 0) ownp3 = ny + ownp3
		if (ownp2 .lt. 0) ownp2 = ny + ownp2

                if (ownp3 .gt. ny / 2) then
    		  dist = ny - ownp2
		  if (lmax_iter(2) .lt. dist) lmax_iter(2) = dist  
                else 
		  dist = ownp3
		  if (lmax_iter(1) .lt. dist) lmax_iter(1) = dist
		endif
           enddo
        enddo

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 

        max_iter(1) = cmmd_reduce_int(lmax_iter(1), CMMD_combiner_max)
        max_iter(2) = cmmd_reduce_int(lmax_iter(2), CMMD_combiner_max)
C***    Timing Code 
        tmp = mp_timer_stop(1)
        if (tmp .eq. -1) then
           print *, 'Failure in stopping timer'
           stop
        end if
        etime = mp_elapsed_time(1)

D         print 200, ">Node : ", ny0, " , Global Comm. Time: ",
D     &             etime
 200    format (A, I4, A, E22.15.3)

C***    Timing Code 
D        print *, "L Max Iter is : ", lmax_iter(1), " (N), ", 
D     &            lmax_iter(2), " (S)", " Node : ", ny0


      end

C--

c***

        subroutine set_lsd_commons(nx,nx0,ny,ny0, nynth, 
     *                             nysth, nxest, nxwst)
        
          implicit none
          
          include "slgcm.mp"

          integer nx,
     &            nx0,
     &            ny,
     &            ny0,
     &            nynth,
     &            nysth,
     &            nxest,
     &            nxwst

          nx = 1
          nx0 = 0
          ny  = CMMD_partition_size()
          ny0 = CMMD_self_address()
          nynth  = mod( ny0 + 1, ny )
          if (ny0 .eq. 0) then
            nysth = 31
          else
             nysth  = mod( ny0 - 1, ny )
          endif
          
C1          if (nysth .eq. -1) then
C1            nysth = nysth + ny
C1          endif

          nxest = -1
          nxwst = -1
D          print 201, ny0, nynth, nysth
 201      format("Node : ", I4, " SRC : ", I4, " DEST ", I4 )

        end


C*** get host status
C*   Does host want to perform a bilinear interpolation, 
C*   bi-cubic interpolation, or exit ?

        function host_stat()

          implicit none

          include 'slgcm.mp'

          integer host_stat
          integer status

          call cmmd_receive_bc_from_host(status, size_of_int)

          host_stat = status

        end








