Dynamic memory allocation error in Fortran2003 using LAPACK

后端 未结 1 1947
不思量自难忘°
不思量自难忘° 2021-01-24 13:21

I\'m struggling with LAPACK\'s dgetrf and dgetri routines. Below is a subroutine I\'ve created (the variable fit_coeffs is defined externally and is al

相关标签:
1条回答
  • 2021-01-24 13:55

    1) Where is fit_coeffs declared? I can't see how the above can even compile 1b) Implicit None is your friend!

    2) You do have an interface in scope at the calling point, don't you?

    3) dgertf and dgetri want "double precision" while you have single. So you need sgetrf and sgetri

    "Fixing" all these and completeing the program I get

    Program testit
    
      Implicit None
    
      Real, Dimension( 1:100 ) :: x, y
    
      Integer :: D
    
      Interface 
         subroutine polynomial_fit(x_array, y_array, D)
           Implicit None ! Always use this!!
           integer, intent(in) :: D
           real, intent(in), dimension(:) :: x_array, y_array
         End subroutine polynomial_fit
      End Interface
    
      Call Random_number( x )
      Call Random_number( y )
    
      D = 6
    
      Call polynomial_fit( x, y, D )
    
    End Program testit
    
    subroutine polynomial_fit(x_array, y_array, D)
    
      Implicit None ! Always use this!!
    
        integer, intent(in) :: D
        real, intent(in), dimension(:) :: x_array, y_array
        real, allocatable, dimension(:,:) :: A, AT, ATA
        real, allocatable, dimension(:) :: work, fit_coeffs
        integer, dimension(:), allocatable :: pivot
        integer :: l, m, n, lda, lwork, ok
    
        l = D + 1
        lda = l
        lwork = l
    
        allocate(fit_coeffs(l))
        allocate(pivot(l))
        allocate(work(l))
        allocate(A(size(x_array),l))
        allocate(AT(l,size(x_array)))
        allocate(ATA(l,l))
    
        do m = 1,size(x_array),1
          do n = 1,l,1
            A(m,n) = x_array(m)**(n-1)
          end do
        end do
    
        AT = transpose(A)
        ATA = matmul(AT,A)
    
        call sgetrf(l, l, ATA, lda, pivot, ok)
        ! ATA is now represented as PLU (permutation, lower, upper)
        if (ok /= 0) then
          write(6,*) "HERE"
        end if
        call sgetri(l, ATA, lda, pivot, work, lwork, ok)
        ! ATA now contains the inverse of the matrix ATA
        if (ok /= 0) then
          write(6,*) "HERE"
        end if
    
        fit_coeffs = matmul(matmul(ATA,AT),y_array)
    
        deallocate(pivot)
        deallocate(fit_coeffs)
        deallocate(work)
        deallocate(A)
        deallocate(AT)
        deallocate(ATA)
      end subroutine polynomial_fit
    

    This runs to completion. If I omit the interface I get "HERE" printed twice. If I use the d versions I get seg faults.

    Does this answer your question?

    0 讨论(0)
提交回复
热议问题