[SciPy-Dev] f2py, the fortran integer type, and npy_intp

Sturla Molden sturla@molden...
Sat Jul 10 18:33:12 CDT 2010


Charles R Harris skrev:
>
> I don't think the bindings don't help, we need to have the default 
> integers in the existing pre-FORTRAN77 code in scipy compiled as 
> Py_ssize_d, and then f2py needs to be modified to generate appropriate 
> Python bindings. That's a lot of work. Even with the bindings I think 
> one would need to have a script to rewrite the FORTRAN code since the 
> c-type corresponding to Py_ssize_d isn't fixed.
We should also beware that the problem applies to real numbers as well 
as integer. You cannot rely on standard mappings from C float to REAL 
and C double to DOUBLE PRECISION.

With pre-Fortran 90, there is no way of controlling this portably. With 
Fortran 90 and later, we have the standard methods selected_real_kind 
and selected_int_kind that returns (compiler dependent) "kind" numbers, 
which can be used to declare real and integers with specific precitions.

  integer, parameter :: single = selected_real_kind(p=6, r=37)
  integer, parameter :: double = selected_real_kind(p=13)
 
  integer, parameter :: npy_int8 = selected_int_kind(2)
  integer, parameter :: npy_int16 = selected_int_kind(4)
  integer, parameter :: npy_int32 = selected_int_kind(9)
  integer, parameter :: npy_int64 = selected_int_kind(18)

Now we can declare an npy_int32 like this:

  integer(kind=npy_int32) :: i

Still we have no ide what npy_intp would map to. We can do this in 
Fortran 2003:
 
  use, intrinsic ::  iso_c_binding
  integer, parameter :: npy_intp = c_intptr_t

  integer(kind=npy_intp) ::  i

Real numers and other integer sre also easier:
 
  integer, parameter :: float = c_float
  integer, parameter :: double = c_double
  integer, parameter :: npy_int32 = c_int32_t

Such declarations can be put in a module, and subsequently imported to 
Fortran 90.

It might be that f2c is the only cure for old Fortran code. The other 
option is to write a Fortran 2003 wrapper to interface C. Then this 
wrapper will call the old Fortran code. We then need to declare the old 
Fortran routine (as an interface block) to Fortran 2003. The Fortran 
compiler is then smart enough to do the correct conversions, including 
making a local copy of an array if that is needed.

Wasn't Kurt Smith working on this for a GSOC project?



Sturla




 





























More information about the SciPy-Dev mailing list