[Numpy-discussion] f2py complications

Pearu Peterson pearu.peterson at gmail.com
Thu May 12 12:37:19 EDT 2011


On Thu, May 12, 2011 at 4:14 PM, Jose Gomez-Dans <jgomezdans at gmail.com>wrote:

> Hi,
>
> We have some fortran code that we'd like to wrap using f2py. The code
> consists of a library that we compile into an .so file, and a file that
> wraps that library, which is then wrapped by f2py to provide a clean
> interface to the library. The process thus includes two steps:
> 1.- Use a makefile to compile the library
> 2.- Use f2py to create the python bindings from a fortran file, and link
> dynamically to the library created in the previous step.
>
> Now, I don't see why just a call to f2py shouldn't suffice (we don't really
> need to have an .so lying around, and it implies that we have to set eg
> $LD_LIBRARY_PATH to search for it).
>

It would be sufficient to just call f2py once.


> I thought about using a pyf file for this, and use the only :: option:
> $ f2py -h my_model.pyf -m my_model  my_wrapper.f90 only: func1 func2 func3
> : all_my_other_files.f even_more_files.f90
>

The above command (with using -h option) will just create the my_model.pyf
file, no extra magic here,


> $ f2py -c  -m my_model  --f90flags="-fdefault-real-8 -O3 -march=native
> -m32" --f90exec=/usr/bin/gfortran  --f77exec=/usr/bin/gfortran --opt=-O3
>  my_model.pyf
>
>
You need to include all .f and .f90 files to the f2py command and -m has no
effect when .pyf is specified:

f2py -c   --f90flags="-fdefault-real-8 -O3 -march=native -m32"
--f90exec=/usr/bin/gfortran  --f77exec=/usr/bin/gfortran --opt=-O3
 my_model.pyf all_my_other_files.f even_more_files.f90

This command (with .pyf file in command line) reads only the my_model.pyf
file and creates wrapper code. It does not scan any Fortran files but only
compiles them (with -c in command line) and links to the extension module.

In fact, IIRC, the two above command lines can be joined to one:

  f2py  -m my_model  my_wrapper.f90 only: func1 func2 func3 :
all_my_other_files.f even_more_files.f90  --f90flags="-fdefault-real-8 -O3
-march=native -m32" --f90exec=/usr/bin/gfortran  --f77exec=/usr/bin/gfortran
--opt=-O3


> This however, doesn't seem to work, with python complaining about missing
> things. If I put all my *.f and *f90 files after the my_model.pyf (which
> doesn't seem to have them in the file), I get undefined symbol errors when
> importing the .so in python.
>
>
Are you sure that you specified all needed Fortran files in the f2py command
line? Where are these symbols defined that are reported to be undefined?

Additionally, it would be great to have this compilation in a
> distutils-friendly package, but how do you specify all these compiler flags?
>
>
It is possible. See numpy/distutils/tests for examples. To use gfortran, run

  python setup.py build --fcompiler=gnu95

HTH,
Pearu
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20110512/1e2668be/attachment.html>


More information about the NumPy-Discussion mailing list