Calling a Fortran routine from a library which uses LAPACK

A minimal example here:

The Fortran code, which calls dsyev from Lapack:

File: uselapack.f90

subroutine uselapack(M,A)

  implicit none
  double precision :: M(4,4), A(4)

  ! For dsyev
  double precision :: work(12)
  integer :: info

  ! Computing the eigenvalues 'A' and eigenvectors 'M' of the M matrix
  call dsyev('V','U',4,M,4,A,work,12,info)

end subroutine uselapack

Compile it with:

gfortran uselapack.f90 -llapack -shared -o uselapack.so

Then, in Julia:

julia> M = [ i == j ? Float64(i+j) : 0. for i in 1:4, j in 1:4 ]
4×4 Array{Float64,2}:
 2.0  0.0  0.0  0.0
 0.0  4.0  0.0  0.0
 0.0  0.0  6.0  0.0
 0.0  0.0  0.0  8.0

julia> A = zeros(4)
4-element Array{Float64,1}:
 0.0
 0.0
 0.0
 0.0

julia> ccall((:uselapack_,"uselapack.so"),Nothing,(Ref{Float64},Ref{Float64}),M,A)

julia> M
4×4 Array{Float64,2}:
 1.0  -0.0  -0.0  0.0
 0.0   1.0  -0.0  0.0
 0.0   0.0   1.0  0.0
 0.0   0.0   0.0  1.0

julia> A
4-element Array{Float64,1}:
 2.0
 4.0
 6.0
 8.0


3 Likes