C
C --- Copy label and bond edge values between neighboring processors.
C


      SUBROUTINE bond_edges
      INCLUDE "common.h"
      INCLUDE "cluster.h"
      INTEGER type, len,vlen,voffset,items
      INTEGER d1,d2

      type = EDGE

C --- right edge -> left edge  (left node passes to right node). 
      vlen = 1
      voffset = MAX_BUFF
      items = jsiz
      d1 = KXVCHA(ibond(0,1),   vlen,voffset,items,left, type,
     &            ibond(isiz,1),vlen,voffset,items,right,type)

C --- top edge -> bottom edge  (bottom node passes to top node). 
      len = isiz
      d2 = KXCHAN(jbond(1,0),   len,bottom,type,
     &            jbond(1,jsiz),len,top,   type)

C ----  Debug!
C       CALL KMULTI(6)
C       IF ((d1.NE.items) .OR. (d2.NE.len)) THEN
C         WRITE(6,*) 'bond exchange', d1,items, d2,len
C      	ENDIF
C      	CALL KFLUSH(6)
C      	CALL KSINGL(6)
      
      RETURN
      END



      SUBROUTINE get_edges
      INCLUDE "common.h"
      INCLUDE "cluster.h"
      INTEGER type 
      INTEGER len1,len2
      INTEGER i,j,k

C --- Pack first cluster then new spin into sendi & sendj.
C --- Cluster labels:
      DO i=1,isiz
         sendi(i) = Npoint(label(i,1))
      END DO
      DO j=1,jsiz
         sendj(j) = Npoint(label(1,j))
      END DO
C --- New spins:
      i = isiz + 1
      DO k=1,isiz
         sendi(i) = new_spin(label(k,1))
         i = i + 1
      END DO
      j = jsiz + 1
      DO k=1,isiz
         sendj(j) = new_spin(label(1,k)) 
         j = j + 1
      END DO

C --- Get cluster and new_spin edges.
      type = EDGE

C --- left edge -> right edge  (right node passes to left node). 
      len1 = two_jsiz_int
      CALL KXCHAN(recvj(1),len1,right,type,
     &            sendj(1),len1,left, type)

C --- bottom edge -> top edge  (top node passes to bottom node). 
      len2 = two_isiz_int
      CALL KXCHAN(recvi(1),len2,top,   type,
     &            sendi(1),len2,bottom,type)
      
      RETURN
      END



      SUBROUTINE return_edges
      INCLUDE "common.h"
      INCLUDE "cluster.h"
      INTEGER type 
      INTEGER len1,len2
      INTEGER i,j,k

C --- Pack first cluster then new spin into sendi & sendj.
C --- Cluster labels:
      DO i=1,isiz
         sendi(i) = Npoint(label(i,jsiz))
      END DO
      DO j=1,jsiz
         sendj(j) = Npoint(label(isiz,j))
      END DO
C --- New spins:
      i = isiz + 1
      DO k=1,isiz
         sendi(i) = new_spin(label(k,jsiz)) 
         i = i + 1
      END DO
      j = jsiz + 1
      DO k=1,isiz
         sendj(j) = new_spin(label(isiz,k)) 
         j = j + 1
      END DO

C --- Return cluster and new_spin edges.
      type = EDGE

C --- right edge -> left edge  (left node passes to right node). 
      len1 = two_jsiz_int
      CALL KXCHAN(recvj(1),len1,left,type,
     &            sendj(1),len1,right,type)

C --- top edge -> bottom edge  (bottom node passes to top node). 
      len2 = two_isiz_int
      CALL KXCHAN(recvi(1),len2,bottom,type,
     &            sendi(1),len2,top,   type)
      
      RETURN
      END



      SUBROUTINE label_edges
      INCLUDE "common.h"
      INCLUDE "cluster.h"
      INTEGER type, i,j, len1,len2

      DO i=1,isiz
         sendi(i) = label(i,1)
      END DO
      DO j=1,jsiz
         sendj(j) = label(1,j)
      END DO

      type = EDGE
C --- left edge -> right edge  (right node passes to left node). 
      len1 = jsiz_int
      CALL KXCHAN(recvj(1),len1,right,type,
     &            sendj(1),len1,left, type)

C --- bottom edge -> top edge  (top node passes to bottom node). 
      len2 = isiz_int
      CALL KXCHAN(recvi(1),len2,top,   type,
     &            sendi(1),len2,bottom,type)
      
      DO i=1,isiz
         label(i,jsiz+1) = recvi(i)
      END DO
      DO j=1,jsiz
         label(isiz+1,j) = recvj(j)
      END DO

      RETURN
      END

