	program FFThost

	include '../include/express.h'

	include '../include/fft.h'

	complex		cTmp(0:NN-1)
	complex		x(0:NN-1)
	integer		numpts
	integer 	allNode,npid,gtype,intSize,cmplxSize
	integer		numNode,numDim,numLoc
	data 		allNode,npid,gtype,intSize,cmplxSize / -1,0,999,4,8 /

        integer pgind, nodes, status, src, dummy
        common /PROGINFO/ pgind, nodes
        character*40 prog
        common /PROGNAME/ prog
        character*40 device
        parameter (device='/dev/intel')

        call KXINIT
        write(6,*) 'Enter number of Nodes'
        read(5,*) nodes
        numNode = nodes
        prog = 'fftnode'

        if(nodes .LT. 0) then
            nodes = -nodes
            call KXPAUS
        endif
        pgind = KXOPEN(device, nodes, NOCARE)
        if(pgind .LT. 0) stop 'Failed to allocate nodes'
        status = KXLOAD(pgind, prog)
        if(status .LT. 0) then
                write(6,*) 'Failed to load program'
                stop
        endif

	print *, 'Enter # of points: '
	read  *, numpts

	if (numpts.GT.NN)print *,'Warning!! Vector size out of bounds'
	
	numLoc  = numpts/numNode

	status = KXBROD(numpts,IHOST,intSize,IALNOD,dummy,gtype)
	print *, '# of points = ',numpts,'  ',numLoc,' poins in each node'
	print *, 'please wait...'
	do i = 0, numNode-1
	    src = i
	    status = KXREAD(cTmp,cmplxSize*numLoc,src,gtype+src)


	  k = i*numLoc
	  do j = 0, numLoc-1
	    x(k) = cTmp(j)
	    k = k + 1
	  end do
	end do



   	call GlbRev(x,numpts)

	print *, 'all done.'
	print *, 'HERE ARE THE RESULTS'
	do j = 0, numpts
	  print *, 'x = ', x(j)
	end do

	stop
	end

CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC
C			GlbRev					C
CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC#` 

   	subroutine GlbRev(y,n)
	integer n
	complex y(0:n-1)

	include '../include/fft.h'

	complex		x(0:NN-1)
	integer Permute
	integer temp

	do i=0,n-1
	  temp = Permute(i,n)
	  x(i) = y(temp)
	end do
	do i=0,n-1
	  y(i) = x(i)
	end do

	return
	end

CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC
C			Permute					C
CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC
	integer function Permute(l,m)
	integer l,m

	integer numBit, k, power2, Log2
	logical IsOdd

	numBit 	= Log2(m)
	k	= l
	power2 	= m / 2
	Permute = 0
	do i = 1, numBit
	    if (IsOdd(k)) Permute = Permute + power2
	    power2 = power2 / 2
	    k	   = k / 2
	end do

	return
	end

CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC
C			IsOdd					C
CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC
	logical function IsOdd(n)
	integer n

	if (2*(n/2) .EQ. n) then
	    IsOdd = .FALSE.
	else
	    IsOdd = .TRUE.
	endif

	return
	end

CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC
C			Log2					C
CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC
	integer function Log2(n)
	integer n

	integer k

	k = n
	Log2 = 0
	do i = 1, n
	    if (k .GT. 1) then
	    	Log2 = Log2 + 1
	    	k    = k / 2
	    else
		return
		endif
	end do

	end
