# Calling Intel MKL as an external library on Mac

**URL:** <https://discourse.julialang.org/t/calling-intel-mkl-as-an-external-library-on-mac/33722>\
**Category:** General Usage\
**Tags:** ccall, mkl\
**Created:** [January 24, 2020, 2:44am UTC](https://discourse.julialang.org/t/calling-intel-mkl-as-an-external-library-on-mac/33722 "2020-01-24T02:44:15Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![razvangheorghe](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/razvangheorghe/32/12436_2.png) [@razvangheorghe](https://discourse.julialang.org/u/razvangheorghe)\
**Post date:** [January 24, 2020, 2:44am UTC](https://discourse.julialang.org/t/calling-intel-mkl-as-an-external-library-on-mac/33722/1 "2020-01-24T02:44:15Z")

</div>

Hi, I’d like to use the Fortran subroutine [ZGELSS](http://www.netlib.org/lapack/explore-html/d6/d10/group__complex16_g_esolve_ga61e68db68886c3f80753fac87ca35a6e.html) from MKL in Julia and I’m a bit confused.

```julia
julia> versioninfo()
Julia Version 1.3.0
Commit 46ce4d7933 (2019-11-26 06:09 UTC)
Platform Info:
  OS: macOS (x86_64-apple-darwin19.0.0)
  CPU: Intel(R) Core(TM) i7-6700HQ CPU @ 2.60GHz
  WORD_SIZE: 64
  LIBM: libopenlibm
  LLVM: libLLVM-6.0.1 (ORCJIT, skylake)
Environment:
  JULIA_NUM_THREADS = 4

```

I tried to find the subroutine in libmkl\_rt.dylib and I’m confused about what everything is.

```julia
nm /opt/intel/compilers_and_libraries_2020.0.166/mac/mkl/lib/libmkl_rt.dylib | grep zgelss
00000000003e28f0 T _LAPACKE_zgelss
00000000003e2ed0 T _LAPACKE_zgelss_work
00000000003e2590 T _lapacke_zgelss
00000000003e27d0 T _lapacke_zgelss_
00000000003e2a20 T _lapacke_zgelss_work
00000000003e2d40 T _lapacke_zgelss_work_
0000000000528960 T _mkl_lapack__zgelss_
00000000005284e0 T _zgelss
0000000000528660 T _zgelss_

```

The closest topic I could find regarding this was [Call Intel MKL as external library](https://discourse.julialang.org/t/call-intel-mkl-as-external-library/2097). The code shown there worked, so I tried to ccall [LAPACKE\_zgelss](https://www.netlib.org/lapack/explore-html/d9/dca/lapacke__zgelss_8c_a30946dd72159b32005cd2a78b5c7fc1b.html) that way and I get

```julia
signal (11): Segmentation fault: 11
in expression starting at /path to julia file/MKL_zgelss_test.jl:46
mkl_lapack_zgelss at /opt/intel/compilers_and_libraries_2020.0.166/mac/mkl/lib/libmkl_core.dylib (unknown line)
ZGELSS at /opt/intel/compilers_and_libraries_2020.0.166/mac/mkl/lib/libmkl_intel_ilp64.dylib (unknown line)
LAPACKE_zgelss_work at /opt/intel/compilers_and_libraries_2020.0.166/mac/mkl/lib/libmkl_intel_ilp64.dylib (unknown line)
LAPACKE_zgelss at /opt/intel/compilers_and_libraries_2020.0.166/mac/mkl/lib/libmkl_intel_ilp64.dylib (unknown line)
Allocations: 53473789 (Pool: 53462622; Big: 11167); GC: 57

```

the code for this is

```julia
using Libdl

const global librt = Libdl.find_library(["libmkl_rt"],
 ["path to libmkl_rt"])

Libdl.dlopen(librt)

function zgelssMKL_C(A::StridedMatrix{ComplexF64}, b::StridedMatrix{ComplexF64})
  m, n = size(A)
  lda = ldb = max(1,stride(A, 2))
  s = similar(A, Float64, lda)
  rcond = -1.0
  rank = 0
  # lapack_int LAPACKE_zgelss(
  # int matrix_layout,
  # lapack_int m,
  # lapack_int n,
  # lapack_int nrhs,
  # lapack_complex_double * a,
  # lapack_int lda,
  # lapack_complex_double * b,
  # lapack_int ldb,
  # double * s,
  # double rcond,
  # lapack_int * rank)
  dd = ccall(("LAPACKE_zgelss", librt),
              Cint, # Return type
                (Int64, Int64, Int64, Int64,
                Ptr{ComplexF64}, Int64, Ptr{ComplexF64}, Int64,
                Ptr{Float64}, Float64, Int64),
                102, m, n, 1,
                A, lda, b, ldb,
                s, rcond, rank)
  dd, b # dd = 0 if calculation is done, b = output
end # function

N = 10;
A = rand(ComplexF64,N,N);
AA = copy(A);
b = rand(ComplexF64, N,1);
c = copy(b);

dd, x = zgelssMKL_C(A, b)

```

What could be causing this?  
Is it possible to call [ZGELSS](http://www.netlib.org/lapack/explore-html/d6/d10/group__complex16_g_esolve_ga61e68db68886c3f80753fac87ca35a6e.html)?  
Would there be any differences between [LAPACKE\_zgelss](https://www.netlib.org/lapack/explore-html/d9/dca/lapacke__zgelss_8c_a30946dd72159b32005cd2a78b5c7fc1b.html) and [ZGELSS](http://www.netlib.org/lapack/explore-html/d6/d10/group__complex16_g_esolve_ga61e68db68886c3f80753fac87ca35a6e.html)

Thanks

---

<div class="post-metadata">

**Author:** ![razvangheorghe](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/razvangheorghe/32/12436_2.png) [@razvangheorghe](https://discourse.julialang.org/u/razvangheorghe)\
**Post date:** [January 24, 2020, 12:46pm UTC](https://discourse.julialang.org/t/calling-intel-mkl-as-an-external-library-on-mac/33722/2 "2020-01-24T12:46:36Z")

</div>

Partly solved by @mkitti. He used the gelsd! definition in lapack.jl to hack a gelss! function together.

> [@On solving sets of linear equations in Fortran vs Julia](https://discourse.julialang.org/t/on-solving-sets-of-linear-equations-in-fortran-vs-julia/33729/10):
>
> I got ZGELSS working in Julia: [https://gist.github.com/mkitti/07a804050d07891e7d63011302246ad9](https://gist.github.com/mkitti/07a804050d07891e7d63011302246ad9)

I’d still like to know what the functions shown in nm are, but I’ll mark it as solved if no one answers in 24h as it’s not really a Julia thing.

---

<div class="post-metadata">

**Author:** ![mkitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkitti/32/12459_2.png) [@mkitti](https://discourse.julialang.org/u/mkitti)\
**Post date:** [January 24, 2020, 4:05pm UTC](https://discourse.julialang.org/t/calling-intel-mkl-as-an-external-library-on-mac/33722/3 "2020-01-24T16:05:33Z")

</div>

Between my answer and @mfh 's answer, I believe you have a full solution to use the MKL and call ZGELSS:

> [@On solving sets of linear equations in Fortran vs Julia](https://discourse.julialang.org/t/on-solving-sets-of-linear-equations-in-fortran-vs-julia/33729/2):
>
> From what you describe it seems indeed that something wrong is happening on the FORTRAN side. However, to rule out that your making an error in the process of extracting data from FORTRAN to Julia I’d suggest to take a look at two options: Compile your FORTRAN code into a shared object and directly call it from Julia with ccall to get the system matrices, the right-hand side and the solution from FORTRAN and then you can check bit by bit from Julia. Perhaps a bit easier: Use an established fil…
