Calling a Fortran routine from a library which uses LAPACK

I made several wrappers to LAPACK, so I am basically familiar with the mechanism of calling Fortran based LAPACK programs from Julia. I would like to call now subroutines from another Fortran library (called SLICOT), which heavily relies on LAPACK and BLAS calls. I wonder if this is possible, relying on the already compiled LAPACK and BLAS codes used inside Julia. I would like to avoid compiling LAPACK and BLAS as separate libraries (which I guess would be possible).

I thank in advance for any hint on how to approach this problem. And sorry if such a question has been already answered.

You will need to build a library of the “slicot” package anyway. At least in linux, the inclusion of lapack or blas libraries in that process reduces to adding a flag to the compilation (-llapack and -lblas, if I remember well) because these libraries can be installed, already compiled, with standard package managers. With that in place, I do not think there is any more practical alternative than that.

Is there any specific scenario in which this does not seem a good alternative?

If you’d like to find the path for linking:

julia> using LinearAlgebra, Libdl

julia> filter(lib->occursin(BLAS.libblas, lib), Libdl.dllist())
1-element Vector{String}:
 "/home/chriselrod/Documents/languages/julia/usr/bin/../lib/libopenblas64_.so"

julia> filter(lib->occursin(BLAS.liblapack, lib), Libdl.dllist())
1-element Vector{String}:
 "/home/chriselrod/Documents/languages/julia/usr/bin/../lib/libopenblas64_.so"

Thanks for your reply.

In Julia, you need to call a library routine using ccall, for example,

ccall((function_name, library), ...

Here 'library is the name of my library, say SLICOT, and function_nameis the name of the routine. But, I wonder how the internal calls to LAPACK are resolved and where the information on the Julia library "libopenblas64_" (which contains the LAPACK library routines in the directoryjulia\bin` on my Windows computer) can be provided.

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

In the long run it would be nice to have a Yggdrasil build script for SLICOT, which could automatically build a SLICOT_jll package with precompiled binaries for every Julia platform. Given this, it would be much easier to build Julia packages on top of SLICOT, maybe starting with a SLICOT.jl package to provide some higher-level interfaces.

Here is a simple example of a Yggdrasil build script for a Fortran program; you can look at build scripts for things like SCALAPACK or COSMA that link BLAS/LAPACK.

(There was an early Slicot.jl package from 6 years ago that started putting together SLICOT wrappers, but Julia has changed so much since 2014 that one would probably be better off mostly starting over.)

3 Likes

This is a very interesting information. Incidentaly, today, the NICONET association, who cares for SLICOT, decided to follow my proposal and make the last version of SLICOT, an open software under a MIT licence (similar to LAPACK) in GITHUB. Until now, only version 4.5 was in GITHUB under a GPL license.

5 Likes

I am also interested in exactly the same library (namly SLICOT). But i just started to play around with wrapping of c/fortran libraries in julia. Can you please provide an Link to the information NICONET wants tp make the last version of SLICOT an open software under a MIT licence? Thanks in advance. Thanks also for the information already posted here.

I am the 2nd chairman of NICONET, the organization who developed and maintains SLICOT. So, you have this information directly from me.

3 Likes

Wow fine. Thanks very much for the fast reply. And i appreciate this move. Thanks very much.