program main
    !-------- include MAGMA module
    !use magma_dfortran
    use magma

    !-------- declarations
    double precision,   allocatable, dimension(:,:) :: h_A, h_R
    integer, allocatable, dimension(:) :: ipiv
    magma_devptr_t :: d_A   ! magma_devptr_t is integer(8), for 64-bit pointers
    integer :: i, M=1024, N=1024, lda=1024
    REAL  :: rnumber
    ! integer*8 off
    ! INTEGER info( 2 )
    integer :: info
    ! integer(8) :: sizeof_double
    !INTEGER ISEED
    integer :: iseed(4) = (/ 1, 2, 3, 4 /)
    integer :: idist=1
    
    REAL X
    
    
    !--------- initialize CUBLAS
    call cublas_init()
    call magmaf_init()
    
    !---------- allocate CPU memory
    allocate(h_A(lda,N), h_R(lda,N), ipiv(min(M,N)))
    
    !---------- allocate GPU memory
    call cublas_alloc(lda*N, sizeof_double, d_A)
    
    !---------- Initialize the matrix
    !do i=1,n*n
    !    CALL RANDOM_SEED   (SIZE=ISEED)
    !    CALL RANDOM_NUMBER(HARVEST=X)
    !    !call random_number(rnumber)
    !    h_A(i) = HARVEST(1)
    !    h_R(i) = HARVEST(1)
    !end do
    call dlarnv( idist, iseed, lda*n, h_A )
    
    !-----------  d_A = h_A
    call cublas_set_matrix( M, N, sizeof_double, h_A, lda, d_A, lda )
    
    !----------- call MAGMA GPU interface
    call magmaf_dgetrf_gpu( M, N, d_A, lda, ipiv, info )
    
    call cublas_get_matrix( M, N, sizeof_double, d_A, lda, h_R, lda )
    
    
    !------------ Free GPU memory and exit
    call cublas_free( d_A )
    call magmaf_finalize()
    call cublas_shutdown()
end
