22Diciembre2017numpyW

Como nota de laboratorio, básicamente p2py es una herramienta que permite utilizar código Fortran en scripts de python, aunque tal vez Fortran no es un lenguaje que hoy en día marque una tendencia tecnológica, es un hecho que existen investigadores que tienen desarrolladas rutinas computacionales estables sobre dicho lenguaje, teniendo sentido no reescribir la rueda y continuar su uso desde un entorno python.

Su instalación sobre un equipo que utiliza una versión de Debian (8.9):

Para proveer p2py es necesario tener numpy instalado:

apt install python-numpy

El tutorial que estamos siguiendo es https://docs.scipy.org/doc/numpy-dev/f2py/getting-started.html. Básicamente intentan correr un código en Fortran que calcula la serie fibonacci. No voy a copiar el código Fortran que ellos usan, lo que si menciono es que localmente en este ejemplo lo hemos guardado como en el archivo "fibonnacci.f" (no olvidar la extensión), intentamos pues correrlo y crear el archivo importable posteriormente desde Python:

f2py -c fibonacci.f -m fibonacciComp

Al final de todo el proceso nos genera un error:

running build
running config_cc
unifing config_cc, config, build_clib, build_ext, build commands --compiler options
running config_fc
unifing config_fc, config, build_clib, build_ext, build commands --fcompiler options
running build_src
build_src
building extension "fibonacciComp" sources
f2py options: [ ]
f2py:> /tmp/tmpHgFRJC/src.linux-x86_64-2.7/fibonacciCompmodule.c
creating /tmp/tmpHgFRJC/src.linux-x86_64-2.7
Reading fortran codes...
Reading file 'fibonacci.f' (format:fix,strict)
Post-processing...
Block: fibonacciComp
Block: fib
Post-processing (stage 2)...
Building modules...
Building module "fibonacciComp"...
Constructing wrapper function "fib"...
fib(a,[n])
Wrote C/API module "fibonacciComp" to file "/tmp/tmpHgFRJC/src.linux-x86_64-2.7/fibonacciCompmodule.c"
adding '/tmp/tmpHgFRJC/src.linux-x86_64-2.7/fortranobject.c' to sources.
adding '/tmp/tmpHgFRJC/src.linux-x86_64-2.7' to include_dirs.
copying /usr/lib/python2.7/dist-packages/numpy/f2py/src/fortranobject.c -> /tmp/tmpHgFRJC/src.linux-x86_64-2.7
copying /usr/lib/python2.7/dist-packages/numpy/f2py/src/fortranobject.h -> /tmp/tmpHgFRJC/src.linux-x86_64-2.7
build_src: building npy-pkg config files
running build_ext
customize UnixCCompiler
customize UnixCCompiler using build_ext
customize Gnu95FCompiler
Could not locate executable gfortran
Could not locate executable f95
customize IntelFCompiler
Could not locate executable ifort
Could not locate executable ifc
customize LaheyFCompiler
Could not locate executable lf95
customize PGroupFCompiler
Could not locate executable pgfortran
customize AbsoftFCompiler
Could not locate executable f90
Could not locate executable f77
customize NAGFCompiler
customize VastFCompiler
customize CompaqFCompiler
Could not locate executable fort
customize IntelItaniumFCompiler
Could not locate executable efort
Could not locate executable efc
customize IntelEM64TFCompiler
customize GnuFCompiler
Could not locate executable g77
customize G95FCompiler
Could not locate executable g95
customize PathScaleFCompiler
Could not locate executable pathf95
don't know how to compile Fortran code on platform 'posix'
warning: build_ext: f77_compiler=None is not available.

building 'fibonacciComp' extension
error: extension 'fibonacciComp' has Fortran sources but no Fortran compiler found

Tiene todo el sentido del mundo, si vamos a compilar código Fortran, se necesita un compilador de Fortran ...instalamos uno:

apt install gfortran

Intentamos de nuevo:

f2py -c fibonacci.f -m fibonacciComp

y se obtiene:

running build
running config_cc
unifing config_cc, config, build_clib, build_ext, build commands --compiler options
running config_fc
unifing config_fc, config, build_clib, build_ext, build commands --fcompiler options
running build_src
build_src
building extension "fibonacciComp" sources
f2py options: [ ]
f2py:> /tmp/tmpTVbfs7/src.linux-x86_64-2.7/fibonacciCompmodule.c
creating /tmp/tmpTVbfs7/src.linux-x86_64-2.7
Reading fortran codes...
Reading file 'fibonacci.f' (format:fix,strict)
Post-processing...
Block: fibonacciComp
Block: fib
Post-processing (stage 2)...
Building modules...
Building module "fibonacciComp"...
Constructing wrapper function "fib"...
fib(a,[n])
Wrote C/API module "fibonacciComp" to file "/tmp/tmpTVbfs7/src.linux-x86_64-2.7/fibonacciCompmodule.c"
adding '/tmp/tmpTVbfs7/src.linux-x86_64-2.7/fortranobject.c' to sources.
adding '/tmp/tmpTVbfs7/src.linux-x86_64-2.7' to include_dirs.
copying /usr/lib/python2.7/dist-packages/numpy/f2py/src/fortranobject.c -> /tmp/tmpTVbfs7/src.linux-x86_64-2.7
copying /usr/lib/python2.7/dist-packages/numpy/f2py/src/fortranobject.h -> /tmp/tmpTVbfs7/src.linux-x86_64-2.7
build_src: building npy-pkg config files
running build_ext
customize UnixCCompiler
customize UnixCCompiler using build_ext
customize Gnu95FCompiler
Found executable /usr/bin/gfortran
customize Gnu95FCompiler
customize Gnu95FCompiler using build_ext
building 'fibonacciComp' extension
compiling C sources
C compiler: x86_64-linux-gnu-gcc -pthread -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict-prototypes -fno-strict-aliasing -D_FORTIFY_SOURCE=2 -g -fstack-protector-strong -Wformat -Werror=format-security -fPIC

creating /tmp/tmpTVbfs7/tmp
creating /tmp/tmpTVbfs7/tmp/tmpTVbfs7
creating /tmp/tmpTVbfs7/tmp/tmpTVbfs7/src.linux-x86_64-2.7
compile options: '-I/tmp/tmpTVbfs7/src.linux-x86_64-2.7 -I/usr/lib/python2.7/dist-packages/numpy/core/include -I/usr/include/python2.7 -c'
x86_64-linux-gnu-gcc: /tmp/tmpTVbfs7/src.linux-x86_64-2.7/fibonacciCompmodule.c
In file included from /usr/lib/python2.7/dist-packages/numpy/core/include/numpy/ndarraytypes.h:1761:0,
from /usr/lib/python2.7/dist-packages/numpy/core/include/numpy/ndarrayobject.h:17,
from /usr/lib/python2.7/dist-packages/numpy/core/include/numpy/arrayobject.h:4,
from /tmp/tmpTVbfs7/src.linux-x86_64-2.7/fortranobject.h:13,
from /tmp/tmpTVbfs7/src.linux-x86_64-2.7/fibonacciCompmodule.c:18:
/usr/lib/python2.7/dist-packages/numpy/core/include/numpy/npy_1_7_deprecated_api.h:15:2: warning: #warning "Using deprecated NumPy API, disable it by " "#defining NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION" [-Wcpp]
#warning "Using deprecated NumPy API, disable it by " \
^
/tmp/tmpTVbfs7/src.linux-x86_64-2.7/fibonacciCompmodule.c:111:12: warning: ‘f2py_size’ defined but not used [-Wunused-function]
static int f2py_size(PyArrayObject* var, ...)
^
x86_64-linux-gnu-gcc: /tmp/tmpTVbfs7/src.linux-x86_64-2.7/fortranobject.c
In file included from /usr/lib/python2.7/dist-packages/numpy/core/include/numpy/ndarraytypes.h:1761:0,
from /usr/lib/python2.7/dist-packages/numpy/core/include/numpy/ndarrayobject.h:17,
from /usr/lib/python2.7/dist-packages/numpy/core/include/numpy/arrayobject.h:4,
from /tmp/tmpTVbfs7/src.linux-x86_64-2.7/fortranobject.h:13,
from /tmp/tmpTVbfs7/src.linux-x86_64-2.7/fortranobject.c:2:
/usr/lib/python2.7/dist-packages/numpy/core/include/numpy/npy_1_7_deprecated_api.h:15:2: warning: #warning "Using deprecated NumPy API, disable it by " "#defining NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION" [-Wcpp]
#warning "Using deprecated NumPy API, disable it by " \
^
compiling Fortran sources
Fortran f77 compiler: /usr/bin/gfortran -Wall -ffixed-form -fno-second-underscore -fPIC -O3 -funroll-loops
Fortran f90 compiler: /usr/bin/gfortran -Wall -fno-second-underscore -fPIC -O3 -funroll-loops
Fortran fix compiler: /usr/bin/gfortran -Wall -ffixed-form -fno-second-underscore -Wall -fno-second-underscore -fPIC -O3 -funroll-loops
compile options: '-I/tmp/tmpTVbfs7/src.linux-x86_64-2.7 -I/usr/lib/python2.7/dist-packages/numpy/core/include -I/usr/include/python2.7 -c'
gfortran:f77: fibonacci.f
/usr/bin/gfortran -Wall -Wall -shared /tmp/tmpTVbfs7/tmp/tmpTVbfs7/src.linux-x86_64-2.7/fibonacciCompmodule.o /tmp/tmpTVbfs7/tmp/tmpTVbfs7/src.linux-x86_64-2.7/fortranobject.o /tmp/tmpTVbfs7/fibonacci.o -lgfortran -o ./fibonacciComp.so
Removing build directory /tmp/tmpTVbfs7

La compilación es exitosa y tenemos un nuevo archivo fibonacciComp.so. Ahora desde la consola de Python el tutorial ya nos debería funcionar:

Python 2.7.9 (default, Jun 29 2016, 13:08:31)
[GCC 4.9.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import numpy
>>> import fibonacciComp
>>> print fibonacciComp.fib.__doc__
fib(a,[n])

Wrapper for ``fib``.

Parameters
----------
a : input rank-1 array('d') with bounds (n)

Other Parameters
----------------
n : input int, optional
Default: len(a)

>>> a = numpy.zeros(8,'d')
>>> fibonacciComp.fib(a)
>>> print a
[ 0. 1. 1. 2. 3. 5. 8. 13.]

Recordemos que en nuestro caso el archivo resultante no se llama fib1.so sino fibonacciComp.so y así mismo se importó en el ejemplo de arriba (sin la extensión ".so").

Para otras distribuciones habría que ajustar la paqueteria necesaria, en otra nota de laboratorio registraremos el proceso para Fedora.

.