!
!   -- MAGMA (version 2.0) --
!      Univ. of Tennessee, Knoxville
!      Univ. of California, Berkeley
!      Univ. of Colorado, Denver
!      @date
!
!  @precisions normal z -> c d s
!

      program testing_zgetrf_gpu_f

      use cudafor
      use iso_c_binding

      external zaxpy, zlange, zgetrf, magma_zgetrf_gpu
      double precision zlange

      double precision rnumber(2), matnorm, lapnorm, magnorm, work
      complex*16, allocatable  :: h_A(:), h_R(:)
      complex*16, device, allocatable  :: d_A(:)
      integer,    allocatable          :: ipiv(:)

      integer i, n, nb, info, lda

      n   = 2048
      nb  = 128
      lda = n

!------ Allocate CPU memory
      allocate(h_A(n*n), h_R(n*n), ipiv(n))

!------ Allocate GPU memory
      allocate(d_A(n*n))

!---- Initializa the matrix
      do i=1,n*n
        call random_number(rnumber)
        h_A(i) = rnumber(1)
        h_R(i) = rnumber(1)
      end do

      d_A = h_A

!---- Call magma -------------------
      call magma_zgetrf_gpu(n, n, d_A, lda, ipiv, info)
      write(*,*) 'exit magma LU'

!---- Call LAPACK ------------------
      call zgetrf(N, N, h_A, lda, ipiv, info)
      write(*,*) 'exit lapack LU'

      h_R = d_A

!---- Compare the two results ------
      lapnorm = zlange('f', n, n, h_A, lda, work)
      magnorm = zlange('f', n, n, h_R, lda, work)

      call zaxpy(n*n, -1., h_A, 1, h_R, 1)
      matnorm = zlange('M', n, n, h_R, lda, work)

      write(*,105) 'Norm Lapack = ', lapnorm
      write(*,105) 'Norm MAGMA  = ', magnorm
      write(*,105) 'error = ', matnorm

!---- Free CPU memory
      deallocate(h_A, h_R, ipiv)

!---- Free GPU memory
      deallocate(d_A)
      !call cublas_shutdown

 105  format((a10,es10.3))

      end
