Re: [Numpy-discussion] Compile extension modules with Visual Studio 2005

Geoffrey Zhu wrote:
Hi,
I am about to write a C extension module. C functions in the module will take and return numpy arrays. I found a tutorial online, but I am not sure about the following:
I agree with others that ctypes might be your best path. The codeGenerator is magic, if you ask me: http://starship.python.net/crew/theller/ctypes/old/codegen.html But, if the function is simple, why not weave.inline? What I have done is run the function once, hunt down the long-named library, copy it to the local directory, then include it explicitly and call its function. This eliminates some overhead time for the call. I use it to convert packed IEEE data from an ADC data read function, and it's faster than the manufacturer's own function version that returns scaled integers! Ray

On Wed, Jul 25, 2007 at 06:38:55AM -0700, Ray Schumacher wrote:
The codeGenerator is magic, if you ask me: http://starship.python.net/crew/theller/ctypes/old/codegen.html
Can it wrap code passing around arrays ? If so it really does magic that I don't understand. Gaël

On Wed, Jul 25, 2007 at 03:41:37PM +0200, Gael Varoquaux wrote:
On Wed, Jul 25, 2007 at 06:38:55AM -0700, Ray Schumacher wrote:
The codeGenerator is magic, if you ask me: http://starship.python.net/crew/theller/ctypes/old/codegen.html
Can it wrap code passing around arrays ? If so it really does magic that I don't understand.
If your array is contiguous, it really is only a matter of passing along a pointer and dimensions. By writing your C-functions in the form void func(double* data, int rows, int cols, double* out) { } wrapping becomes trivial. Cheers Stéfan

On Wed, Jul 25, 2007 at 04:44:08PM +0200, Stefan van der Walt wrote:
On Wed, Jul 25, 2007 at 03:41:37PM +0200, Gael Varoquaux wrote:
On Wed, Jul 25, 2007 at 06:38:55AM -0700, Ray Schumacher wrote:
The codeGenerator is magic, if you ask me: http://starship.python.net/crew/theller/ctypes/old/codegen.html
Can it wrap code passing around arrays ? If so it really does magic that I don't understand.
If your array is contiguous, it really is only a matter of passing along a pointer and dimensions.
By writing your C-functions in the form
void func(double* data, int rows, int cols, double* out) { }
wrapping becomes trivial.
Yes, I have done this many times. It is trivial and very convenient. I was just wondering if the code generator could detect this pattern. Gaël

Gael Varoquaux wrote:
On Wed, Jul 25, 2007 at 04:44:08PM +0200, Stefan van der Walt wrote:
On Wed, Jul 25, 2007 at 03:41:37PM +0200, Gael Varoquaux wrote:
On Wed, Jul 25, 2007 at 06:38:55AM -0700, Ray Schumacher wrote:
The codeGenerator is magic, if you ask me: http://starship.python.net/crew/theller/ctypes/old/codegen.html
Can it wrap code passing around arrays ? If so it really does magic that I don't understand.
If your array is contiguous, it really is only a matter of passing along a pointer and dimensions.
By writing your C-functions in the form
void func(double* data, int rows, int cols, double* out) { }
wrapping becomes trivial.
Yes, I have done this many times. It is trivial and very convenient. I was just wondering if the code generator could detect this pattern. I don't see either how to magically generate those functions, since C has no concept of arrays (I mean outside a serie of contiguous bytes): if you see the declaration int f(double* in, int rows, int cols), the compiler does not know that it means a double array of size rows * cols, and that the in(i, j) is given by in[i*rows+j]. Actually, you don't know either without reading the source code or code conventions :).
Now, if you have always the same convention, I think it is conceptually possible to automatically generate the wrappers, a bit like swig does with typemaps for example (maybe f2py can do it for Fortran code ? I have never used f2py, but I think Fortran has a concept of arrays and matrices ?). David

Ray Schumacher wrote:
I agree with others that ctypes might be your best path.
Pyrex is a good bet too: http://www.scipy.org/Cookbook/Pyrex_and_NumPy The advantage with pyrex is that you don't have to write any C at all. You will have to use a compiler that is compatible with your Python build. I found MingGW very easy to use with the python.org python2.5 on Windows-- distutils has built-in support for it. http://boodebr.org/main/python/build-windows-extensions -Chris -- Christopher Barker, Ph.D. Oceanographer Emergency Response Division NOAA/NOS/OR&R (206) 526-6959 voice 7600 Sand Point Way NE (206) 526-6329 fax Seattle, WA 98115 (206) 526-6317 main reception Chris.Barker@noaa.gov
participants (5)
-
Chris Barker
-
David Cournapeau
-
Gael Varoquaux
-
Ray Schumacher
-
Stefan van der Walt