[SciPy-user] Wrapping C libraries for Numeric integration

Robert Hetland hetland at tamu.edu
Tue Feb 7 12:23:13 CST 2006


Here is the relevant information:

Mac OS X 10.4.4, gcc, g77,  numpy.__version__ =  '0.9.5.2044'

$ ls
foo.c  m.pyf  setup.py
$ python setup.py build_src build_ext --inplace > build.out
$ cat build.out
running build_src
building extension "m" sources
f2py options: []
f2py: ./m.pyf
Reading fortran codes...
         Reading file './m.pyf' (format:free)
{'this': 'intent', 'after': '(c)                      ', 'before': ''}
Line #5 in ./m.pyf:"     intent(c)                      "
         analyzeline: no name pattern found in intent statement for  
''. Skipping.
Post-processing...
         Block: m
                         Block: foo
Post-processing (stage 2)...
Building modules...
         Building module "m"...
                 Constructing wrapper function "foo"...
                   y = foo(x)
         Wrote C/API module "m" to file "./mmodule.c"
   adding 'build/src/fortranobject.c' to sources.
   adding 'build/src' to include_dirs.
creating build
creating build/src
copying /Library/Frameworks/Python.framework/Versions/2.4/lib/ 
python2.4/site-packages/
numpy/f2py/src/fortranobject.c -> build/src
copying /Library/Frameworks/Python.framework/Versions/2.4/lib/ 
python2.4/site-packages/
numpy/f2py/src/fortranobject.h -> build/src
running build_ext
customize UnixCCompiler
customize UnixCCompiler using build_ext
building 'm' extension
compiling C sources
gcc options: '-fno-strict-aliasing -Wno-long-double -no-cpp-precomp - 
mno-fused-madd -f
no-common -dynamic -DNDEBUG -g -O3 -Wall -Wstrict-prototypes'
creating build/temp.darwin-8.4.0-Power_Macintosh-2.4
creating build/temp.darwin-8.4.0-Power_Macintosh-2.4/build
creating build/temp.darwin-8.4.0-Power_Macintosh-2.4/build/src
compile options: '-Ibuild/src -I/Library/Frameworks/Python.framework/ 
Versions/2.4/lib/
python2.4/site-packages/numpy/core/include -I/Library/Frameworks/ 
Python.framework/Vers
ions/2.4/include/python2.4 -c'
gcc: build/src/fortranobject.c
In file included from build/src/fortranobject.h:13,
                  from build/src/fortranobject.c:2:
/Library/Frameworks/Python.framework/Versions/2.4/lib/python2.4/site- 
packages/numpy/co
re/include/numpy/arrayobject.h:134: warning: redefinition of `ushort'
/usr/include/sys/types.h:85: warning: `ushort' previously declared here
/Library/Frameworks/Python.framework/Versions/2.4/lib/python2.4/site- 
packages/numpy/co
re/include/numpy/arrayobject.h:135: warning: redefinition of `uint'
/usr/include/sys/types.h:86: warning: `uint' previously declared here
gcc: ./foo.c
gcc: ./mmodule.c
In file included from build/src/fortranobject.h:13,
                  from mmodule.c:17:
/Library/Frameworks/Python.framework/Versions/2.4/lib/python2.4/site- 
packages/numpy/co
re/include/numpy/arrayobject.h:134: warning: redefinition of `ushort'
/usr/include/sys/types.h:85: warning: `ushort' previously declared here
/Library/Frameworks/Python.framework/Versions/2.4/lib/python2.4/site- 
packages/numpy/co
re/include/numpy/arrayobject.h:135: warning: redefinition of `uint'
/usr/include/sys/types.h:86: warning: `uint' previously declared here
gcc -Wl,-x -bundle -undefined dynamic_lookup build/temp.darwin-8.4.0- 
Power_Macintosh-2
.4/mmodule.o build/temp.darwin-8.4.0-Power_Macintosh-2.4/foo.o build/ 
temp.darwin-8.4.0
-Power_Macintosh-2.4/build/src/fortranobject.o -o m.so

$ ipython
[...snip...]
 >>> import m
 >>> m.foo([1.,2.,3])
array([ 0.,  0.,  0.])


On Feb 7, 2006, at 11:02 AM, Pearu Peterson wrote:

>
>
> On Tue, 7 Feb 2006, Robert Hetland wrote:
>
>>
>> This is an excellent example -- I have just been wondering how to do
>> such things, and I would prefer f2py over swig, since it is more
>> familiar to me.
>>
>> However, I can't seem to get the example to work.  It complies fine,
>> but I get an output array of all zeros (the same size as the input
>> array).  Any suggestions?
>
> What numpy version are you using? I am using numpy from svn repo.
>
> Did you follow exactly the instructions below or did you try something
> different? Could you send the full output of setup.py build  
> command, may
> be offlist? Remember to do `rm -rf build *.so` before rebuilding.
> What platform/compilers are you using?
>
> Pearu
>
>> On Feb 7, 2006, at 5:23 AM, Pearu Peterson wrote:
>>
>>>
>>>
>>> On Tue, 7 Feb 2006, Bryan Cole wrote:
>>>
>>>> What's the best approach for wrapping C libraries where the
>>>> inputs/outputs are C arrays (which I want to pass to/from Numeric
>>>> arrays)?
>>>>
>>>> I'm a regular SWIG user, but I don't have any typemaps to handle
>>>> C-array-to-Numeric conversion and I can't see any distributed with
>>>> SWIG.
>>>> What approach is used by SciPy? (I know scipy is mostly fortran
>>>> routines
>>>> but surely you have a few C ones?).
>>>
>>> f2py can be easily be used also wrapping C libraries if writing some
>>> Fortran-like syntax (see f2py usersguide) does not scare you.  
>>> Here's a
>>> simple example:
>>>
>>> /* File foo.c */
>>> void foo(int n, double *x, double *y) {
>>>    int i;
>>>    for (i=0;i<n;i++) {
>>>      y[i] = x[i] + i;
>>>    }
>>> }
>>>
>>> ! File m.pyf
>>> python module m
>>> interface
>>>    subroutine foo(n,x,y)
>>>      intent(c) foo                 ! foo is a C function
>>>      intent(c)                     ! all foo arguments are
>>> considered as C based
>>>      integer intent(hide), depend(x) :: n=len(x)  ! n is the lenght
>>> of input array x
>>>      double precision intent(in) :: x(n)   ! x is input array (or
>>> arbitrary sequence)
>>>      double precision intent(out) :: y(n)  ! y is output array, see
>>> code in foo.c
>>>    end subroutine foo
>>> end interface
>>> end python module m
>>>
>>> # File setup.py
>>> def configuration(parent_package='',top_path=None):
>>>      from numpy.distutils.misc_util import Configuration
>>>      config = Configuration('',parent_package,top_path)
>>>
>>>      config.add_extension('m',
>>>                           sources = ['m.pyf','foo.c'])
>>>      return config
>>> if __name__ == "__main__":
>>>      from numpy.distutils.core import setup
>>>      setup(**configuration(top_path='').todict())
>>>
>>> Building and testing module m thats function foo calls C function:
>>>
>>>    python setup.py build_src build_ext --inplace
>>>
>>>    python
>>>>>> import m
>>>>>> print m.foo.__doc__
>>> foo - Function signature:
>>>    y = foo(x)
>>> Required arguments:
>>>    x : input rank-1 array('d') with bounds (n)
>>> Return objects:
>>>    y : rank-1 array('d') with bounds (n)
>>>
>>>>>> m.foo([1,2,3,4,5])
>>> array([ 1.,  3.,  5.,  7.,  9.])
>>>>>>
>
> _______________________________________________
> SciPy-user mailing list
> SciPy-user at scipy.net
> http://www.scipy.net/mailman/listinfo/scipy-user

-----
Rob Hetland, Assistant Professor
Dept of Oceanography, Texas A&M University
p: 979-458-0096, f: 979-845-6331
e: hetland at tamu.edu, w: http://pong.tamu.edu



More information about the SciPy-user mailing list