# Calling a Fortran routine from a library which uses LAPACK

**URL:** <https://discourse.julialang.org/t/calling-a-fortran-routine-from-a-library-which-uses-lapack/49124>\
**Category:** General Usage\
**Tags:** question\
**Created:** [October 27, 2020, 2:46pm UTC](https://discourse.julialang.org/t/calling-a-fortran-routine-from-a-library-which-uses-lapack/49124 "2020-10-27T14:46:02Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![andreasvarga](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/andreasvarga/32/11634_2.png) [@andreasvarga](https://discourse.julialang.org/u/andreasvarga)\
**Post date:** [October 27, 2020, 2:46pm UTC](https://discourse.julialang.org/t/calling-a-fortran-routine-from-a-library-which-uses-lapack/49124/1 "2020-10-27T14:46:02Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [October 27, 2020, 3:40pm UTC](https://discourse.julialang.org/t/calling-a-fortran-routine-from-a-library-which-uses-lapack/49124/2 "2020-10-27T15:40:17Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![Elrod](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/elrod/32/22461_2.png) [@Elrod](https://discourse.julialang.org/u/Elrod)\
**Post date:** [October 27, 2020, 3:46pm UTC](https://discourse.julialang.org/t/calling-a-fortran-routine-from-a-library-which-uses-lapack/49124/3 "2020-10-27T15:46:26Z")

</div>

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

```julia
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"

```

---

<div class="post-metadata">

**Author:** ![andreasvarga](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/andreasvarga/32/11634_2.png) [@andreasvarga](https://discourse.julialang.org/u/andreasvarga)\
**Post date:** [October 27, 2020, 3:56pm UTC](https://discourse.julialang.org/t/calling-a-fortran-routine-from-a-library-which-uses-lapack/49124/4 "2020-10-27T15:56:13Z")

</div>

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 f`unction\_name`is 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 directory`julia\bin` on my Windows computer) can be provided.

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [October 27, 2020, 4:52pm UTC](https://discourse.julialang.org/t/calling-a-fortran-routine-from-a-library-which-uses-lapack/49124/5 "2020-10-27T16:52:35Z")

</div>

A minimal example here:

The Fortran code, which calls dsyev from Lapack:

File: uselapack.f90

```fortran
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:

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

```

Then, in Julia:

```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

```

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [October 27, 2020, 6:34pm UTC](https://discourse.julialang.org/t/calling-a-fortran-routine-from-a-library-which-uses-lapack/49124/6 "2020-10-27T18:34:49Z")

</div>

> [@andreasvarga](#):
>
> I would like to call now subroutines from another Fortran library (called SLICOT), which heavily relies on LAPACK and BLAS calls.

In the long run it would be nice to have a [Yggdrasil build script](https://github.com/JuliaPackaging/Yggdrasil) 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](https://github.com/JuliaPackaging/Yggdrasil/blob/b0de8d3ccbea62ce8255b02d5a5915202ef20b29/H/HelloWorldFortran/build_tarballs.jl); you can look at build scripts for things like [SCALAPACK](https://github.com/JuliaPackaging/Yggdrasil/blob/8922e3bc46c9ff0e0faaf6326b86a42d60448317/S/SCALAPACK/build_tarballs.jl) or [COSMA](https://github.com/JuliaPackaging/Yggdrasil/blob/master/C/COSMA/build_tarballs.jl) that link BLAS/LAPACK.

(There was an early [Slicot.jl package](https://github.com/jcrist/Slicot.jl) 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.)

---

<div class="post-metadata">

**Author:** ![andreasvarga](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/andreasvarga/32/11634_2.png) [@andreasvarga](https://discourse.julialang.org/u/andreasvarga)\
**Post date:** [October 27, 2020, 6:48pm UTC](https://discourse.julialang.org/t/calling-a-fortran-routine-from-a-library-which-uses-lapack/49124/7 "2020-10-27T18:48:18Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![romanWS](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/romanws/32/19066_2.png) [@romanWS](https://discourse.julialang.org/u/romanWS)\
**Post date:** [October 30, 2020, 8:51pm UTC](https://discourse.julialang.org/t/calling-a-fortran-routine-from-a-library-which-uses-lapack/49124/8 "2020-10-30T20:51:47Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![andreasvarga](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/andreasvarga/32/11634_2.png) [@andreasvarga](https://discourse.julialang.org/u/andreasvarga)\
**Post date:** [October 30, 2020, 10:21pm UTC](https://discourse.julialang.org/t/calling-a-fortran-routine-from-a-library-which-uses-lapack/49124/9 "2020-10-30T22:21:48Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![romanWS](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/romanws/32/19066_2.png) [@romanWS](https://discourse.julialang.org/u/romanWS)\
**Post date:** [October 30, 2020, 11:07pm UTC](https://discourse.julialang.org/t/calling-a-fortran-routine-from-a-library-which-uses-lapack/49124/10 "2020-10-30T23:07:02Z")

</div>

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