jameskermode / f90wrap

F90 to Python interface generator with derived type support
GNU Lesser General Public License v3.0
243 stars 82 forks source link

Link to numerical libraries #100

Open zhucaoxiang opened 4 years ago

zhucaoxiang commented 4 years ago

Hi, thanks for the excellent tool. I wonder if I can find an example of linking to existing libraries, either user-compiled static/shared library or common numerical libraries, like MKL, LAPACK, BLAS. I do notice there is an option --link-, but it seems only link the library it finds out.

I am trying to use f90wrap for a big Fortran project involving a self-compiled library and other numerical libraries. I got the error undefined symbol for some modules.

jameskermode commented 4 years ago

Sure - see for example https://github.com/libAtoms/QUIP/blob/public/quippy/Makefile#L96

You can simply pass -l and -L options to f2py-f90wrap to link to additional libraries.

zhucaoxiang commented 4 years ago

So far, it seems working. Great, thanks! I will try this with another legacy code which might have more issues.

By the way, if you don't mind, do you know any example with calling an f90wrap-ed module with OpenMP? I saw there is a -lgomp in the link you replied to me.

jameskermode commented 4 years ago

Yes. that's all you need - just compile your code with OpenMP as usual (-fopenmp or similar) and link to the required libraries. Then if you set OMP_NUM_THREADS to something other than 1 your code will run in parallel within Fortran calls.

zhucaoxiang commented 4 years ago

@jameskermode Thanks for your reply.

For the linked libraries, are they required to be compiled with -fPIC? I got the following error with ScaLAPACK, which suggests recompiling with -fPIC. But previously there is no error with linking BLAS in the other code.

/usr/bin/ld: /usr/gcc/8.1-pkgs/openmpi-3.0.0-pkgs/scalapack-2.0.2/lib/libscalapack.a(igesd2d_.o): relocation R_X86_64_32 against `BI_AuxBuff' can not be used when making a shared object; recompile with -fPIC
/usr//gcc/8.1-pkgs/openmpi-3.0.0-pkgs/scalapack-2.0.2/lib/libscalapack.a: could not read symbols: Bad value
collect2: error: ld returned 1 exit status
jameskermode commented 4 years ago

Ah yes, -fPIC is required. If you link against dynamic (.so) rather than static (.a) libraries this will not be an issue.