Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Using (external) lapack functions in a library

Subject: Using (external) lapack functions in a library

From: Jan-Gerrit

Date: 31 Oct, 2011 16:26:27

Message: 1 of 5

Hey,
I've been having some trouble getting a library I wrote to work in a matlab mex file.
The Library uses some lapack functions and works well in the c++ environment.

But now i want to load and use the library functions in a mex file.
I've tried several things:

1. Using the library as a shared library
   Compiling and linking works fine, and running ldd indicates that the mexa64 file links to my external lapack library (located in /usr/lib/lapack.so)
When i run the file, i get some error-messages along the line of:

MKL ERROR: Parameter 8 was incorrect on entry to ZGESDD.

I checked with a profiler and have come to the conclusion, that matlab tries to run the commands with the internal lapack version.
[...]
182,400 ???:mkl_lapack_zgesdd
[matlab/bin/glnxa64/mkl.so]

Unfortunately I can't seem to change that behaviour.

2. Using the library as a static library
Here it gets even weirder. My next thought was to compile my library as static and hereby including all my working lapack calls into it.
So i compiled lapack as static and my library as static and included the library into the mex file, but i still got the same error messages because matlab would still call the mkl.so

I didn't really understand why, but as a last resort, i tried to rename the function i call so it wouldn't be included in the mkl.so hoping to outsmart whatever is against me..

So here is the (relevant) output from nm on my mexa64 file.
nm createQuadripole.mexa64 | grep zgesdd
0000000000182c18 T zgesdd2_
So the function zgesdd2 (which was just zgesdd) is statically linked into my file.
But still, when i call it from matlab it tries to load the mkl.so and crashes because there is no such function in it..
Here is the part of the crash report:
[ 10] 0x00007f0717c33f31 matlab/bin/glnxa64/mkl.so+20770609 mkl_serv_z_abs+000001
[ 11] 0x00007f0716bc3556 matlab/bin/glnxa64/mkl.so+03532118 mkl_lapack_zlange+000182
[ 12] 0x00007f071693dc26 matlab/bin/glnxa64/mkl.so+00887846 ZLANGE+000086
[ 13] 0x00007f070d711a6a createQuadripole.mexa64+01596010 zgesdd2_+011858

I'm really lost now.. Hopefully, somebody knows what i'm doing wrong :)

Thanks
Jan

Subject: Using (external) lapack functions in a library

From: Duncan Po

Date: 1 Nov, 2011 14:23:27

Message: 2 of 5

Hi Jan,

At least your last resort solution is partially working.

> Here is the part of the crash report:
> [ 10] 0x00007f0717c33f31 matlab/bin/glnxa64/mkl.so+20770609 mkl_serv_z_abs+000001
> [ 11] 0x00007f0716bc3556 matlab/bin/glnxa64/mkl.so+03532118 mkl_lapack_zlange+000182
> [ 12] 0x00007f071693dc26 matlab/bin/glnxa64/mkl.so+00887846 ZLANGE+000086
> [ 13] 0x00007f070d711a6a createQuadripole.mexa64+01596010 zgesdd2_+011858
>

From the crash log, MATLAB has found zgesdd2 in your mex file. But when zgesdd2 tries to call other lapack functions (zlange), zlange from MATLAB's internal lapack has already been loaded, so the function call is directed to the loaded version instead of your version in the mex file.
A brute force way is to rename all functions in your lapack.
Why do you need your own implementation of LAPACK? Did you make extensive changes to the original LAPACK? Why can you not use MATLAB's internal LAPACK?

Duncan Po
Software Developer
MathWorks

Subject: Using (external) lapack functions in a library

From: Jan-Gerrit

Date: 2 Nov, 2011 09:18:09

Message: 3 of 5

"Duncan Po" <Duncan.Po@mathworks.com> wrote in message <j8ovcv$81o$1@newscl01ah.mathworks.com>...
> Hi Jan,
>
> At least your last resort solution is partially working.
>
> > Here is the part of the crash report:
> > [ 10] 0x00007f0717c33f31 matlab/bin/glnxa64/mkl.so+20770609 mkl_serv_z_abs+000001
> > [ 11] 0x00007f0716bc3556 matlab/bin/glnxa64/mkl.so+03532118 mkl_lapack_zlange+000182
> > [ 12] 0x00007f071693dc26 matlab/bin/glnxa64/mkl.so+00887846 ZLANGE+000086
> > [ 13] 0x00007f070d711a6a createQuadripole.mexa64+01596010 zgesdd2_+011858
> >
>
> From the crash log, MATLAB has found zgesdd2 in your mex file. But when zgesdd2 tries to call other lapack functions (zlange), zlange from MATLAB's internal lapack has already been loaded, so the function call is directed to the loaded version instead of your version in the mex file.
> A brute force way is to rename all functions in your lapack.
> Why do you need your own implementation of LAPACK? Did you make extensive changes to the original LAPACK? Why can you not use MATLAB's internal LAPACK?
>
> Duncan Po
> Software Developer
> MathWorks

Oh wow thanks. I didn't notice that. At least something is working ;)
I want to use the external lapack because my library (which i'm loading and which uses lapack) should continue to function and will hopefully get extended at some point. (plus its free and open source)
From the MKL error i guessed that the mkl-lapack is not compatible so changes in my code would be nessesary and the whole thing would not run if you don't have the mkl library.

I guess the real question is how to get matlab to load the external lapack and not its own..
Its a pain to rename everything ;)

Thanks
Jan

Subject: Using (external) lapack functions in a library

From: Jan-Gerrit

Date: 3 Nov, 2011 10:18:10

Message: 4 of 5

"Jan-Gerrit " <angerrit@gmail.com> wrote in message <j8r1sh$7s5$1@newscl01ah.mathworks.com>...
> "Duncan Po" <Duncan.Po@mathworks.com> wrote in message <j8ovcv$81o$1@newscl01ah.mathworks.com>...
> > Hi Jan,
> >
> > At least your last resort solution is partially working.
> >
> > > Here is the part of the crash report:
> > > [ 10] 0x00007f0717c33f31 matlab/bin/glnxa64/mkl.so+20770609 mkl_serv_z_abs+000001
> > > [ 11] 0x00007f0716bc3556 matlab/bin/glnxa64/mkl.so+03532118 mkl_lapack_zlange+000182
> > > [ 12] 0x00007f071693dc26 matlab/bin/glnxa64/mkl.so+00887846 ZLANGE+000086
> > > [ 13] 0x00007f070d711a6a createQuadripole.mexa64+01596010 zgesdd2_+011858
> > >
> >
> > From the crash log, MATLAB has found zgesdd2 in your mex file. But when zgesdd2 tries to call other lapack functions (zlange), zlange from MATLAB's internal lapack has already been loaded, so the function call is directed to the loaded version instead of your version in the mex file.
> > A brute force way is to rename all functions in your lapack.
> > Why do you need your own implementation of LAPACK? Did you make extensive changes to the original LAPACK? Why can you not use MATLAB's internal LAPACK?
> >
> > Duncan Po
> > Software Developer
> > MathWorks
>
> Oh wow thanks. I didn't notice that. At least something is working ;)
> I want to use the external lapack because my library (which i'm loading and which uses lapack) should continue to function and will hopefully get extended at some point. (plus its free and open source)
> From the MKL error i guessed that the mkl-lapack is not compatible so changes in my code would be nessesary and the whole thing would not run if you don't have the mkl library.
>
> I guess the real question is how to get matlab to load the external lapack and not its own..
> Its a pain to rename everything ;)
>
> Thanks
> Jan

Well.. renaming is not working. if i rename everything i get compile errors..
lapack-3.3.1/BLAS/SRC/strmm.f:170.47:

      ELSE IF ((.NOT.LSAME2(DIAG,'U')) .AND. (.NOT.LSAME2(DIAG,'N'))) TH

something about an invalid character. it is complaining about the . in front of the second .NOT. ;)

does anybody know how to get matlab to use my library?
...

Jan

Subject: Using (external) lapack functions in a library

From: Jan-Gerrit

Date: 3 Nov, 2011 11:52:13

Message: 5 of 5

"Jan-Gerrit " <angerrit@gmail.com> wrote in message <j8tpp2$e1a$1@newscl01ah.mathworks.com>...
> "Jan-Gerrit " <angerrit@gmail.com> wrote in message <j8r1sh$7s5$1@newscl01ah.mathworks.com>...
> > "Duncan Po" <Duncan.Po@mathworks.com> wrote in message <j8ovcv$81o$1@newscl01ah.mathworks.com>...
> > > Hi Jan,
> > >
> > > At least your last resort solution is partially working.
> > >
> > > > Here is the part of the crash report:
> > > > [ 10] 0x00007f0717c33f31 matlab/bin/glnxa64/mkl.so+20770609 mkl_serv_z_abs+000001
> > > > [ 11] 0x00007f0716bc3556 matlab/bin/glnxa64/mkl.so+03532118 mkl_lapack_zlange+000182
> > > > [ 12] 0x00007f071693dc26 matlab/bin/glnxa64/mkl.so+00887846 ZLANGE+000086
> > > > [ 13] 0x00007f070d711a6a createQuadripole.mexa64+01596010 zgesdd2_+011858
> > > >
> > >

Ok. I solved the problem.. I set the LD_PRELOAD variable to the lapack library. Now it loads the correct one and my algorithms work. It's not the perfect solution, because as of now it only works on this machine but it's good enough for now..

Jan
> > > From the crash log, MATLAB has found zgesdd2 in your mex file. But when zgesdd2 tries to call other lapack functions (zlange), zlange from MATLAB's internal lapack has already been loaded, so the function call is directed to the loaded version instead of your version in the mex file.
> > > A brute force way is to rename all functions in your lapack.
> > > Why do you need your own implementation of LAPACK? Did you make extensive changes to the original LAPACK? Why can you not use MATLAB's internal LAPACK?
> > >
> > > Duncan Po
> > > Software Developer
> > > MathWorks
> >
> > Oh wow thanks. I didn't notice that. At least something is working ;)
> > I want to use the external lapack because my library (which i'm loading and which uses lapack) should continue to function and will hopefully get extended at some point. (plus its free and open source)
> > From the MKL error i guessed that the mkl-lapack is not compatible so changes in my code would be nessesary and the whole thing would not run if you don't have the mkl library.
> >
> > I guess the real question is how to get matlab to load the external lapack and not its own..
> > Its a pain to rename everything ;)
> >
> > Thanks
> > Jan
>
> Well.. renaming is not working. if i rename everything i get compile errors..
> lapack-3.3.1/BLAS/SRC/strmm.f:170.47:
>
> ELSE IF ((.NOT.LSAME2(DIAG,'U')) .AND. (.NOT.LSAME2(DIAG,'N'))) TH
>
> something about an invalid character. it is complaining about the . in front of the second .NOT. ;)
>
> does anybody know how to get matlab to use my library?
> ...
>
> Jan

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us