Skip to content

Building blas and lapack as shared libraries #611

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
smijolovic opened this issue Aug 24, 2021 · 7 comments
Closed

Building blas and lapack as shared libraries #611

smijolovic opened this issue Aug 24, 2021 · 7 comments

Comments

@smijolovic
Copy link

smijolovic commented Aug 24, 2021

Digging everywhere, there doesn't seem to be any working or consistent method for building librefblas.so and liblapack.so using make. The cmake option is not an option, as inconsistencies in the results would not classify as a production ready build process, and the currently supported process is make.

Is it possible to get some guidance on how to do this? I've seen some old examples online but none of them are working on the 3.10.0 branch.

@weslleyspereira
Copy link
Collaborator

weslleyspereira commented Aug 25, 2021

Could you try the follwing quick tweak?

  1. Add -fPIC to FFLAGS in make.inc (Actually, I didn't need this flag. Please, see cmake build: more modularity, linker fix, pkg-config fixup #556 for a discussion about it). Then

  2. Replace

$(BLASLIB): $(ALLOBJ)
	$(AR) $(ARFLAGS) $@ $^
	$(RANLIB) $@

by

$(BLASLIB): $(ALLOBJ)
	$(AR) $(ARFLAGS) $@ $^
	$(RANLIB) $@
	ld -shared -o ../../librefblas.so $^

in BLAS/src/Makefile.

  1. If it works, do a similar tweak for the LAPACKLIB

This is not a solution, just something you can do to get a shared library quickly. It works on my Linux machine. Please, feel welcomed to submit a PR to add this functionality on LAPACK. Thanks!

@smijolovic
Copy link
Author

smijolovic commented Aug 25, 2021

That worked to produce the librefblas.so. Thank you!

For the liblapack.so...is it required to link the librefblas library?

For example, does it need to be linked as such:
ld -shared -soname=liblapack.so -o ../liblapack.so -L.. -lrefblas $^

@weslleyspereira
Copy link
Collaborator

One correction in my last suggestion: use $(FC) -shared instead of ld -shared. ld -shared will create a static library. Sorry about that.

That worked to produce the librefblas.so. Thank you!

For the liblapack.so...is it required to link the librefblas library?

For example, does it need to be linked as such:
ld -shared -soname=liblapack.so -o ../liblapack.so -L.. -lrefblas $^

I would just use $(FC) -shared -o ../liblapack.so $^ for simplicity, and then link your executable with both librefblas.so and liblapack.so. There is another way of introducing such dependency between libraries, I just can't remember now.

@smijolovic
Copy link
Author

Sounds good to me - thanks for that! Easy enough two liner for a pull request ... could use the "how-to" there..

$(FC) -shared -o ../../librefblas.so $^ in lapack/BLAS/SRC
$(FC) -shared -o ../liblapack.so $^ in lapack/SRC/Makefile

@boboshaq
Copy link

boboshaq commented Feb 10, 2023

Hi,

Compilatin librefblas.so was sucessfully.
Compilation liblapack(3.10.1) fails in this moment:
gfortran -shared -o ../liblapack.so sbdsvdx.o spotrf2.o sgetrf2.o ...

It is reproduces many of this information:

bin/ld: sbdsvdx.o: relocation R_X86_64_32 against `.rodata' can not be used when making a shared object; recompile with -fPIC
/bin/ld: spotrf2.o: relocation R_X86_64_32 against `.rodata' can not be used when making a shared object; recompile with -fPIC
/bin/ld: sgetrf2.o: relocation R_X86_64_32 against `.rodata' can not be used when making a shared object; recompile with -fPIC
...

and finally it is end with:

/bin/ld: final link failed: Nonrepresentable section on output
collect2: error: ld returned 1 exit status
make[1]: *** [Makefile:558: ../liblapack.a] Error 1
rm la_xisnan.mod
make[1]: Leaving directory '/soft/lapack/lapack-3.10.1/SRC'
make: *** [Makefile:25: lapacklib] Error 2

In SRC/Makefile I was change:

$(LAPACKLIB): $(LAPACKLIB_DEPS)
$(AR) $(ARFLAGS) $@ $^
$(RANLIB) $@

to:

$(LAPACKLIB): $(LAPACKLIB_DEPS)
$(AR) $(ARFLAGS) $@ $^
$(RANLIB) $@
$(FC) -shared -o ../liblapack.so $^

I was use: "sudo make lapacklib" command - sudo because folder ovner is root.

What I was doing wrong? How to fix this?
Should I first compile static library?
It is possible that permissions to folders are wrong?

Regards!

@boboshaq
Copy link

OK, in my case, properly solution is:

  1. Edit file lapack/make.inc:
  • add to options FFLAGS and FFLAGS_NOOPT: -fPIC
  1. Edit file BLAS/SRC/Makefile:
  • change this for BLAS:
$(BLASLIB): $(ALLOBJ)
        $(AR) $(ARFLAGS) $@ $^
        $(RANLIB) $@

to:

$(BLASLIB): $(ALLOBJ)
        $(AR) $(ARFLAGS) $@ $^
        $(RANLIB) $@
        $(FC) -shared -o ../../librefblas.so $^
  • and change this for LAPACK:
$(LAPACKLIB): $(LAPACKLIB_DEPS)
        $(AR) $(ARFLAGS) $@ $^
        $(RANLIB) $@

to:

$(LAPACKLIB): $(LAPACKLIB_DEPS)
        $(AR) $(ARFLAGS) $@ $^
        $(RANLIB) $@
        $(FC) -shared -o ../liblapack.so $^
  1. Run commands:
  • for BLAS: make blaslib
  • for LAPACK: make lapacklib

This should produce 4 files in lapack dir:

  1. libfrefblas.a
  2. librefblas.so
  3. liblapack.a
  4. liblapack.so

So we have both: static (.a) and shared(.so) libraries for BLAS and LAPACK.

Regards!

@langou
Copy link
Contributor

langou commented Feb 19, 2023

Great. Thanks for the "how-to" @boboshaq. Sounds good to me. Closing the issue now.

@langou langou closed this as completed Feb 19, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants