[SciPy-dev] Just saw this

eric jones eric at enthought.com
Fri Jan 6 02:27:18 EST 2006


Hey Travis,

Hadn't seen this -- Thanks.  The neat thing about Instant is that it 
uses SWIGs parser and type pattern matching to automatically generate 
the functions and arguments from the C code.  Weave doesn't do that.  
The 2nd nice thing is that it is pure C (I think).  One of the drawbacks 
is that it requires SWIG.  It didn't find the installation on my windows 
machine (but I'm sure that can be remedied).

But, are the examples below using weave so bad?  For this case line 
count is about the same, and I might even argue that this is simpler, 
requiring the user to know less about include files, headers, init_code, 
etc.  In weave, you do have to declare the functions explicitly in 
python.  I've noticed that this has been a pain for large libraries (but 
not for things like these examples) and have thought about writing an 
auto-generator for them in weave. This wouldn't be so hard -- you just 
need something to parse C...  Oh wait, that's hard. (its left as an 
exercise for the reader.)

Dave Beazley and I have talked a couple of times about exposing the SWIG 
C++ parser as a python library so that other tools could parse C++ 
code.  Weave could use this to autogenerate the function names.   Dave 
raised an eye-brow though muttering something like " parser leaks like a 
sieve" -- not good for a scripting environment where it can be called 
over and over.  Still, its a good idea.  gcc-xml is another candidate 
here. 

Another comment.  weave has a fairly nifty extension mechanism.  The 
notion of "arrays" is not hard coded into the argument list of the 
extension creation method as in Instant.  Instead it is a type spec that 
is registered with weave (array type specs are automatically 
registered).   These type specs are fairly useful carrying around extra 
code, include information, etc. needed when a particular variable type 
is needed (arrays!).  I prefer this  because adding new type conversions 
doesn't require changing the libraries api.  I think that supporting VTK 
objects might require editing the underlying library in Instant.  Prabhu 
added vtk_spec.py and didn't have to touch any of ext_tools.  Instant 
could probably benefit from a similar approach, but I haven't thought 
hard enough about whether it would work with SWIG that well.

eric

# example.py
from Numeric import array, arange, sin, cos
from weave.ext_tools import ext_function, ext_module

a=b=c=array((1.0)) # type declaration of a,b, and c to double arrays
mod = ext_module("test_ext")
add = ext_function("add",
                   """
                   for (int i=0; i<Na[0]; i++) { 
                       c[i] = a[i] + b[i];
                   }
                   """, 
                   ("a","b","c"))
mod.add_function(func)
mod.compile(compiler="gcc") # compiler="gcc" is only needed on windows 
without MSVC

# test the module
from test_ext import add
a = arange(10000.0)
b = arange(10000.0)
c = arange(10000.0)

add(a,b,c)
print c[:5]
# end

This approach is a little closer to how Instant does it, but I noticed a 
few things that need to be fixed in weave to do it a little better (name 
mangling on function names, and a missing instance method).

# example2.py
from Numeric import array, arange, sin, cos
from weave.ext_tools import ext_function, ext_module

c_code = """
/* add function for vectors with all safety checks removed ..*/
void _add(int n1, double* array1, int n2, double* array2, int n3, 
double* array3){
  for (int i=0; i<n1; i++) { 
     array3[i] = array1[i] + array2[i];
  }
}
"""

mod = ext_module("test_ext2")
mod.customize._support_code.append(c_code) # hack! -- shold have 
add_support_code method

a=b=c=array((1.0)) # type declaration of a,b, and c to double arrays
add = ext_function("add", "_add(Na[0], a, Nb[0], b, Nc[0], c);", 
["a","b","c"])
mod.add_function(add)
mod.compile(compiler="gcc") # compiler="gcc" is only needed on windows 
without MSVC

# test the module
from test_ext2 import add
a = arange(10000.0)
b = arange(10000.0)
c = arange(10000.0)

add(a,b,c)
print c[:5]
# end

Travis Oliphant wrote:

>http://heim.ifi.uio.no/~kent-and/software/Instant/doc/Instant.html
>
>I'd like to fix weave so it can be used *more easily* in this way 
>(creating an extension module on-the-fly).
>
>-Travis
>
>_______________________________________________
>Scipy-dev mailing list
>Scipy-dev at scipy.net
>http://www.scipy.net/mailman/listinfo/scipy-dev
>




More information about the SciPy-Dev mailing list