C**********************************************************
C                                                         *
C  Test all the coloring algorithms on different meshes.  *
C                                                         *
C**********************************************************
C
      PROGRAM TESTER

      INCLUDE "irreg.h"

      INTEGER seed,ncol

C
C-- Start up Express and find out if we need to swap bytes.
C
      CALL KXINIT
      ISWAP = KXTSWA()
C
C-- Get runtime parameters.
C
      CALL KXPARA(ENV)
      PROCNUM = ENV(1)
ccc      Print *,"Iswap is",iswap,procnum
c$$$      NPROCS = ENV(2)

      CALL get_seed(seed,ncol)
 
C
C --- Initialize the parallel random number generator
C
      CALL randinit(seed, nprocs, procnum) 

      DO WHILE (seed .GT. 0)
         
         CALL get_mesh
         CALL update_node(ncol)

         CALL tohost(999,procnum)

         CALL get_seed(seed,ncol)
         
      ENDDO

      END

C----------------------------------------------------------------------C

C
C-- This function is used to pass information about our status back
C-- to the host.
C
      SUBROUTINE TOHOST(TYPE, V1)
      INTEGER TYPE, V1
      INTEGER BUF(1), ISTAT
      INTEGER KXWRIT, KXTSWA
      COMMON /XPRESS/ NOCARE, NORDER, NONODE, IHOST, IALNOD, IALPRC
      INTEGER         NOCARE, NORDER, NONODE, IHOST, IALNOD, IALPRC

      BUF(1) = V1
      IF(KXTSWA() .NE. 0) CALL KXSWAW(BUF, BUF, 4)
      ISTAT = KXWRIT(BUF, 4, IHOST, TYPE)
c***      print *,"I",procnum,type,buf(1)

      IF(TYPE .EQ. 99) STOP
      RETURN
      END


C----------------------------------------------------------------------C

C --- Do the work.

      subroutine update_node(ncol)

      include 'irreg.h'

      integer ncol,i
      
      integer findmaxd,findmind
      integer findmaxcolor
      integer test_colors,OK
      integer mind,maxd,maxcolor,elaptime

      integer*4 starttime
      integer*4 kxtime
      external kxtime

C
C --- Find the data communications that need to be done.
C
      CALL set_up_comm_sched
      maxd=findmaxd()
      mind=findmind()

      if (procnum .eq. 0) print 110,maxd
 110  format(1x,"Maximum Degree is ",i4)
         
      
C
C --- Time (and test) the different coloring algorithms.
C
      DO i=1,ncol
         
 112     format(1x,a,i4," colors with ",i4," passes in ",i10, 
     $                  " microsec  test =", i2)

C
C --- Jones/Plassmann
C
         starttime=KXTIME()
         call jonesplass
         elaptime=KXTIME()-starttime
         maxcolor=findmaxcolor()
	 OK = test_colors()
         if (procnum .eq. 0) then
            print 112,"Jones Plassman  ",
     $           maxcolor,color_count-1,elaptime, OK
            call tohost(111,1)
            call tohost(112,maxd)
            call tohost(113,maxcolor)
            call tohost(114,color_count-1)
            call tohost(115,elaptime)
            status=kxread(dummy,4,ihost,321)
         endif
         
C
C --- Largest-Degree-First
C
         starttime=KXTIME()
         call degreegreedy
         elaptime=KXTIME()-starttime
         maxcolor=findmaxcolor()
	 OK = test_colors()
         if (procnum .eq. 0) then
            print 112,"Degree Greedy   ",
     $           maxcolor,color_count-1,elaptime, OK
            call tohost(111,2)
            call tohost(112,maxd)
            call tohost(113,maxcolor)
            call tohost(114,color_count-1)
            call tohost(115,elaptime)
            status=kxread(dummy,4,ihost,321)
         endif
         
C
C --- Smallest-Degree-Last
C
         starttime=KXTIME()
         call smalldeglast(maxd,mind)
         elaptime=KXTIME()-starttime
         maxcolor=findmaxcolor()
	 OK = test_colors()
         if (procnum .eq. 0) then
            print 112,"Small Deg Last  ",
     $           maxcolor,color_count-1,elaptime, OK
            call tohost(111,3)
            call tohost(112,maxd)
            call tohost(113,maxcolor)
            call tohost(114,color_count-1)
            call tohost(115,elaptime)
            status=kxread(dummy,4,ihost,321)
         endif
         
         
CC
CC --- Balanced Smallest-Degree-Last
CC
c$$$         starttime=KXTIME()
c$$$         call balsmalldeglast(maxd,mind)
c$$$         elaptime=KXTIME()-starttime
c$$$         maxcolor=findmaxcolor()
c$$$         if (procnum .eq. 0) then
c$$$            print 112,"Balanced SDL    ", 
c$$$     $           maxcolor,color_count-1,elaptime
c$$$            call tohost(111,4)
c$$$            call tohost(112,maxd)
c$$$            call tohost(113,maxcolor)
c$$$            call tohost(114,color_count-1)
c$$$            call tohost(115,elaptime)
c$$$            status=kxread(dummy,4,ihost,321)
c$$$         endif

      ENDDO
      
      call tohost(111,-1)

      return
      end


C----------------------------------------------------------------------C

      SUBROUTINE get_seed(seed,ncol)
      INCLUDE 'irreg.h'
      INTEGER seed,ncol
      
      status=kxbrod(seed,ihost,4,IALNOD,DUMMY,150)
      status=kxbrod(ncol,ihost,4,IALNOD,DUMMY,160)
      end
      

