
C----------------------------------------------------------------------C
C                                                                      C
C   Initialize some Express stuff, including the domain                C
C   decomposition of the lattice across the processors.                C
C                                                                      C
C----------------------------------------------------------------------C

	SUBROUTINE decompose
	INCLUDE "common.h"
   	INTEGER proc_array_num
   	INTEGER d1,d2,d3,d4

C ----  Set up node data. 
	CALL KXPARA(nddata)
	procnum = nddata(1)
	nprocs = nddata(2)

C ----  Domain decomposition using gridmap tools

C ----  Get the dimensions of the 2-d processor array in procdim.
   	d1 = KXGDSP(nprocs,2,procdim)
   	d2 = KXGDIN(2,procdim)

C ----  Get the coords of this processor in the proc grid.
   	d3 = KXGDCO(procnum,proccoord)

C ----  Get the coords of neighboring processors. 
   	left   = KXGDNO(procnum, 0, -1)
   	right  = KXGDNO(procnum, 0,  1)
   	bottom = KXGDNO(procnum, 1, -1)
   	top    = KXGDNO(procnum, 1,  1)

C ----  Actual position of the proc in the proc array */
   	proc_array_num = (proccoord(2) * procdim(1)) + proccoord(1)

C ----  Debug! 
C	CALL KORDER(6,proc_array_num)
C	CALL KMULTI(6)
C	WRITE(6,*) 'procnum =',procnum,' left =',left,' right =',right,
C    &             'bottom =',bottom,' top =',top
C	CALL KFLUSH(6)
C	CALL KSINGL(6)

C ----  Decompose the global arrays onto processors 
   	globsize(1) = globisiz
   	globsize(2) = globjsiz
   	d4 = KXGDSI(procnum,globsize,localsiz,orig)
   	isiz = localsiz(1)
   	jsiz = localsiz(2)
   	sites = isiz * jsiz

C ----  Sizes (in bytes) for edges to be passed. 
    	isiz_int = isiz * 4
    	jsiz_int = jsiz * 4
    	imax_int = MAX_BUFF * 4
    	two_isiz_int = 2 * isiz_int
    	two_jsiz_int = 2 * jsiz_int

C ----  Debug! 
   	IF ((d1.LT.0).OR.(d2.LT.0).OR.(d3.LT.0).OR.(d4.LT.0)) THEN
     	  CALL KABORT(3)
   	ENDIF
   	IF ((isiz.GT.MAX_SIDE) .OR. (jsiz.GT.MAX_SIDE)) THEN
      	  WRITE(6,*) 'array size too large'
	  CALL KFLUSH(6)
     	  CALL KABORT(4)
   	ELSE 
      	  WRITE(6,11) 'Using a',procdim(1),' x',procdim(2),
     &                ' =',nprocs,' processor array'
      	  WRITE(6,12) ' with a',isiz,' x',jsiz,
     &	              ' lattice segment on each processor'
      	  CALL KFLUSH(6)
  11      FORMAT(A,I4,A,I4,A,I4,A)
  12      FORMAT(A,I4,A,I4,A)
   	ENDIF

	RETURN
	END

