Page 1 of 1

compiled LAPACK into shared library

PostPosted: Sat Feb 28, 2009 4:01 am
by peng.du
Per a user's request for a way of compiled LAPACK in shared library, I did a little homework for it. Post it here so it might be useful to some others.

http://pduwork.pbwiki.com/lapack-as-a-shared-library

This following applies to lapack3.1.1.

(1) Adding -fPIC to

OPTS & NOOPT in make.inc

(2) Change the names in make.inc

BLASLIB = ../../blas$(PLAT).so
LAPACKLIB = ../lapack$(PLAT).so
TMGLIB = tmglib$(PLAT).so

(3) In ./SRC, change the Makefile from

../$(LAPACKLIB): $(ALLOBJ)
$(ARCH) $(ARCHFLAGS) $@ $(ALLOBJ)
$(RANLIB) $@

to

../$(LAPACKLIB): $(ALLOBJ)
$(LOADER) $(LOADOPTS) -shared -Wl,-soname,liblapack.so -o $@ $(ALLOBJ)

(4) In ./BLAS/SRC, change the Makefile from

$(BLASLIB): $(ALLOBJ)
$(ARCH) $(ARCHFLAGS) $@ $(ALLOBJ)
$(RANLIB) $@

to

$(BLASLIB): $(ALLOBJ)
$(LOADER) $(LOADOPTS) -z muldefs -shared -Wl,-soname,libblas.so -o $@ $(ALLOBJ)

And "-z muldefs" is used to handle multiple definitions in $(ALLOBJ)...whew~! [see [2]]

(5) In ./TESTING/MATGEN, change the Makefile from

../../$(TMGLIB): $(SMATGEN) $(CMATGEN) $(SCATGEN) $(DMATGEN) \
$(ZMATGEN) $(DZATGEN)
$(ARCH) $(ARCHFLAGS) $@ $(ALLOBJ)
$(RANLIB) $@

to

../../$(TMGLIB): $(ALLOBJ)
$(LOADER) $(LOADOPTS) -shared -Wl,-soname,libtmglib.so -o $@ $(ALLOBJ)

(6) create a bunch of soft links:

ln -sf tmglib_LINUX.so libtmglib.so
ln -sf lapack_LINUX.so liblapack.so
ln -sf blas_LINUX.so libblas.so

Add these to ./Makefile
lib: blaslib lapacklib tmglib
ln -sf tmglib_LINUX.so libtmglib.so
ln -sf lapack_LINUX.so liblapack.so
ln -sf blas_LINUX.so libblas.so

which used to be just

lib: blaslib lapacklib tmglib

Note that for sure you could do it without having to do these softlinks, for example when create the refblas, instead of doing

$(LOADER) $(LOADOPTS) -z muldefs -shared -Wl,-soname,libblas.so -o $@ $(ALLOBJ)

you could do

$(LOADER) $(LOADOPTS) -z muldefs -shared -Wl,-soname,blas_LINUX.so -o $@ $(ALLOBJ)

But the softlink give a good view of how shared library is only loaded when executing a program that requires them, eg:

with the softlink liblapack.so, you'll get this:

du:/home/du/lib/tmp/lapack-3.1.1/TESTING@grig-compile ~> ./xlintstc

./xlintstc: error while loading shared libraries: liblapack.so: cannot open shared object file: No such file or directory

And without setting LD_LIBRARY_PATH, you might get:

du:/home/du/lib/tmp/lapack-3.1.1/TESTING@grig-compile ~> ./xlintstc

./xlintstc: error while loading shared libraries: libtmglib.so: cannot open shared object file: No such file or directory

(7) TO USE

i. Compile and create libblas.so, liblapack.so and libtmglib.so by typing make in the lapack directory
ii. Add the path of the three .so files to LD_LIBRARY_PATH, eg:
export LD_LIBRARY_PATH=/home/du/lib/lapack-3.1.1.shared_lib:LD_LIBRARY_PATH
iii. link with your own program using -llapack -lblas and maybe -ltmglib

That's it. And please let me know if something isn't right. I have a modified version of lapack3.1.1 that's been tested and works well. Ask if you need it. I don't have time for pushing it to 3.2, but it shouldn't be too much of a difference.


Reference:
[1] http://www.yolinux.com/TUTORIALS/Librar ... namic.html
[2] http://sources.redhat.com/binutils/docs ... tions.html

Re: compiled LAPACK into shared library

PostPosted: Mon Mar 09, 2009 3:23 pm
by linuxl4
very good work!

I think this should be integrated into lapack's Makefile.

Re: compiled LAPACK into shared library

PostPosted: Mon Jun 01, 2009 10:40 pm
by mredu
./xlintstc: error while loading shared libraries: libtmglib.so: cannot open shared object file: No such file or directory

Is this error ok to ignore?

Re: compiled LAPACK into shared library

PostPosted: Wed Jun 03, 2009 3:43 pm
by peng.du
this only means the compiling of lapack into shared library is done. you need to set the path so it can be loaded at run time.

Re: compiled LAPACK into shared library

PostPosted: Tue Jan 12, 2010 11:42 am
by Jacques_Menu
Hello,

I followed the above recommendations for Lapack 3.2.1, they worked fine, leading to the following differences.

Thanks peng.du!

--

root@cal1:/Download/lapack-3.2.1 > diff make.inc INSTALL/make.inc.gfortran
20c20
< OPTS = -O2 -fPIC
---
> OPTS = -O2
22c22
< NOOPT = -O0 -fPIC
---
> NOOPT = -O0
24c24
< LOADOPTS = -lpthread
---
> LOADOPTS =
60,66c60,64
< #BLASLIB = ../../blas$(PLAT).a
< #gotoblas2 used instead:
< BLASLIB = /opt/gotoblas2/1.10/gcc-4.2.3/libgoto2.so
< LAPACKLIB = lapack$(PLAT).so
< TMGLIB = tmglib$(PLAT).so
< EIGSRCLIB = eigsrc$(PLAT).so
< LINSRCLIB = linsrc$(PLAT).so
---
> BLASLIB = ../../blas$(PLAT).a
> LAPACKLIB = lapack$(PLAT).a
> TMGLIB = tmglib$(PLAT).a
> EIGSRCLIB = eigsrc$(PLAT).a
> LINSRCLIB = linsrc$(PLAT).a

--

root@cal1:/Download/lapack-3.2.1/SRC > diff Makefile Makefile_ORG
369,371d368
< # ../$(LAPACKLIB): $(ALLOBJ) $(ALLXOBJ)
< # $(ARCH) $(ARCHFLAGS) $@ $(ALLOBJ) $(ALLXOBJ)
< # $(RANLIB) $@
373c370
< $(LOADER) $(LOADOPTS) -shared -Wl,-soname,liblapack.so -o $@ $(ALLOBJ) $(ALLXOBJ)
---
> $(ARCH) $(ARCHFLAGS) $@ $(ALLOBJ) $(ALLXOBJ)

--

root@cal1:/Download/lapack-3.2.1/BLAS/SRC > diff Makefile Makefile_ORG
140,142d139
< # $(BLASLIB): $(ALLOBJ)
< # $(ARCH) $(ARCHFLAGS) $@ $(ALLOBJ)
< # $(RANLIB) $@
144c141
< $(LOADER) $(LOADOPTS) -z muldefs -shared -Wl,-soname,libblas.so -o $@ $(ALLOBJ)
---
> $(ARCH) $(ARCHFLAGS) $@ $(ALLOBJ)

--

root@cal1:/Download/lapack-3.2.1/TESTING/MATGEN > diff Makefile Makefile_ORG
61,66c61,64
< # ../../$(TMGLIB): $(SMATGEN) $(CMATGEN) $(SCATGEN) $(DMATGEN) \
< # $(ZMATGEN) $(DZATGEN)
< # $(ARCH) $(ARCHFLAGS) $@ $(ALLOBJ)
< # $(RANLIB) $@
< ../../$(TMGLIB): $(ALLOBJ)
< $(LOADER) $(LOADOPTS) -shared -Wl,-soname,libtmglib.so -o $@ $(ALLOBJ)
---
> ../../$(TMGLIB): $(SMATGEN) $(CMATGEN) $(SCATGEN) $(DMATGEN) \
> $(ZMATGEN) $(DZATGEN)
> $(ARCH) $(ARCHFLAGS) $@ $(ALLOBJ)
> $(RANLIB) $@

Re: compiled LAPACK into shared library

PostPosted: Wed May 05, 2010 6:08 am
by linuxl4
what about to support xblas and "make cleanall" ?

so ALLXOBJS should be added to gcc -shared line,

as to make cleanall,
rm *.so should be added.

Re: compiled LAPACK into shared library

PostPosted: Wed May 05, 2010 7:06 am
by linuxl4
I have a question also. Why don't you change LAPACKLIB to liblapack.so directly in make.inc to avoiding making those soft links?

Re: compiled LAPACK into shared library

PostPosted: Wed May 19, 2010 3:02 am
by mikeross
Thanks for sharing peng! very helpful..

Re: compiled LAPACK into shared library

PostPosted: Thu Jul 01, 2010 7:57 am
by helium
cheers man will try it out in a few days

Re: compiled LAPACK into shared library

PostPosted: Thu Jul 08, 2010 11:32 am
by cottrell
Those wanting to compile lapack/blas as shared libraries
might like to take a look at the forum item
viewtopic.php?f=2&t=1895
which announces lapack-autoconf, available at
http://www.wfu.edu/~cottrell/lapack/

Allin Cottrell

Re: compiled LAPACK into shared library

PostPosted: Mon Jul 12, 2010 2:52 pm
by admin
Greenfun,
you want to set LD_LIBRARY_PATH to where you shared library is.

Re: compiled LAPACK into shared library

PostPosted: Sun Jan 23, 2011 6:36 pm
by tzartzar
Works for me. Thanks alot.