[Numpy-discussion] [ANN] Pyfort 7.0 Extending Numpy with C

Paul Dubois paul at pfdubois.com
Wed Mar 20 13:14:10 CST 2002


A beta version of Pyfort 7.0 is available at pyfortran.sf.net. The
documentation is not yet upgraded to this version.

Pyfort 7.0 adds the ability to wrap Fortran-like C coding to extend Numpy.

Dramatically illustrating the virtue of open source software, credit for
this improvement goes to:

Michiel de Hoon
Human Genome Center
University of Tokyo

For example, if you have this C code:

double mydot(int n, double* x, double *y) {
    int i;
    double d;
    d = 0.0;
    for (i=0; i < n; ++i) d += x[i] *  y[i];
   return d;
}

Then you can create a Pyfort input file mymod.pyf:
    function mydot (n, x, y)
        integer:: n=size(x)
        doubleprecision x(n), y(n)
        doubleprecision mydot
    end

Compile mydot.c into a library libmydot.a.
Then:

pyfort -c cc -i -L. -l mydot mymod.pyf

builds and installs the module mymod containing function mydot, which you
can use from Python:

import Numeric, mymod
x=Numeric.array([1.,2.3.])
y=Numeric.array([5., -1., -1.])
print mymod.mydot(x,y)

Note that by wrapping mydot in this way, Pyfort takes care of problems like
converting  input arrays of the wrong type, such as integer; making sure
that x and y have the same length; and making sure x and y are contiguous.

I added directory testc that contains an example like this and one where an
array is output.

Mr. de Hoon explained his patch as follows.

"I have modified fortran_compiler.py to add gcc as a
compiler. This enables pyfort to be used for C code
instead of Fortran code only. To use this option, call
pyfort with the -cgcc option to specify gcc as the
compiler. In order to switch off the default TRANSPOSE
and MIRROR options, some small modifications were
needed in generator.py also. [Editor's note: both -c gcc and -c cc will
work]

Before writing this addition to pyfort, I tried to use
swig to generate the wrapper for my C code. However,
pyfort was easier to use in my case because it is
integrated with numpy. I wasn't able to get swig use
numpy arrays. In addition, I am writing extension code
both in fortran and C, so it is easier having to use
only one tool (pyfort) for both.

In a sense, it is easier to extend python with C than
with fortran because you don't have to worry about
transposing the array. I tried to be minimally
instrusive on the existing pyfort code to switch off
transposing arrays; there may be prettier ways to do
this than what I have done. With this modification, I
was able to pass one- and two-dimensional numpy arrays
from Python to C and back without problems, as well as
scalar variables with intent(in) and intent(out). I
have also used the modified Pyfort on some Fortran
routines to make sure I didn't break something in the
fortran part of Pyfort.

I haven't done an extensive test of this addition, but
I haven't found any problems with it so far. I hope
this patch will be useful to other people trying to
extend Python/numpy with C routines."

Michiel de Hoon
Human Genome Center
University of Tokyo
mdehoon at ims.u-tokyo.ac.jp








More information about the Numpy-discussion mailing list