Hi, I am using cython to speed up some computations (btw, thanks a lot to the people who gave me suggestions on a previous thread). I don't understand how to apply the fast C-coded sin/sqrt functions to a numpy array. I can import from math.h like this: cdef extern from "math.h" double sin(double) but then I can't use this function on a numpy ndarray, obviously. Any ideas? It sounds silly to me to write a cycle on ndarray elements. Thanks _______________________________________________ SciPy-User mailing list [hidden email] http://mail.scipy.org/mailman/listinfo/scipy-user |
On Wed, 12 Jan 2011 13:05:25 +0100 (CET)
<[hidden email]> wrote: > > Hi, > > I am using cython to speed up some computations (btw, thanks a lot to the people who gave me suggestions on a previous thread). > I don't understand how to apply the fast C-coded sin/sqrt functions to a numpy array. > I can import from math.h like this: > > cdef extern from "math.h" > double sin(double) > > but then I can't use this function on a numpy ndarray, obviously. > > Any ideas? It sounds silly to me to write a cycle on ndarray elements. it is not ... and you will gain a lot in speed. -- Jérôme Kieffer On-Line Data analysis / Software Group ISDD / ESRF tel +33 476 882 445 _______________________________________________ SciPy-User mailing list [hidden email] http://mail.scipy.org/mailman/listinfo/scipy-user |
On Wed, Jan 12, 2011 at 7:40 AM, Jerome Kieffer <[hidden email]> wrote:
> On Wed, 12 Jan 2011 13:05:25 +0100 (CET) > <[hidden email]> wrote: > >> >> Hi, >> >> I am using cython to speed up some computations (btw, thanks a lot to the people who gave me suggestions on a previous thread). >> I don't understand how to apply the fast C-coded sin/sqrt functions to a numpy array. >> I can import from math.h like this: >> >> cdef extern from "math.h" >> double sin(double) >> >> but then I can't use this function on a numpy ndarray, obviously. >> >> Any ideas? It sounds silly to me to write a cycle on ndarray elements. > > it is not ... and you will gain a lot in speed. > > -- > Jérôme Kieffer > On-Line Data analysis / Software Group > ISDD / ESRF > tel +33 476 882 445 > _______________________________________________ > SciPy-User mailing list > [hidden email] > http://mail.scipy.org/mailman/listinfo/scipy-user > http://docs.cython.org/src/userguide/numpy_tutorial.html _______________________________________________ SciPy-User mailing list [hidden email] http://mail.scipy.org/mailman/listinfo/scipy-user |
In reply to this post by Giovanni Plantageneto
On Wed, Jan 12, 2011 at 01:05:25PM +0100, [hidden email] wrote:
> > Hi, > > I am using cython to speed up some computations (btw, thanks a lot to the people who gave me suggestions on a previous thread). > I don't understand how to apply the fast C-coded sin/sqrt functions to a numpy array. > I can import from math.h like this: > > cdef extern from "math.h" > double sin(double) > > but then I can't use this function on a numpy ndarray, obviously. > > Any ideas? It sounds silly to me to write a cycle on ndarray elements. Hi. When I started using Cython I also thought that was strange, but that is really how it works. It gets faster than using Numpy because numpy makes a new loop for each operation, and also stores intermediate results in new arrays. With Cython (and other tools) you make a single loop where you calculate complex expressions completely at each iteration without storing intermediate results in memory. When you implement a function in Cython you must create this loop over the array values, and also make sure you use "cdef" in the necessary variables. You must take care because it is very easy to forget to declare a variable properly, and although the program will work correctly, it will be slower. You must also use @cython.boundscheck(False) @cython.wraparound(False) to speed up the array accesses. You can even use C pointers to the arrays, but that doesn't add much from just switching off these checks. Regarding the sin and sqrt, you should not that simply using sin inside Cython shouldn't accelerate much from e.g. doing a sin(x) on an array from Python. The speedup is more related to the memory accesses and number of loops. But you can try to substitute the sin calculations using memoization, for example, or polynomials. And as I mentioned previously, you can use the rsqrt instruction in the case of sqrt calculations. I wrote about that in my blog a few months ago: http://xor0110.wordpress.com/2010/09/16/using-the-sse-rsqrt-from-python-via-cython/ ++nic -- Nicolau Werneck <[hidden email]> C3CF E29F 5350 5DAA 3705 http://www.lti.pcs.usp.br/~nwerneck 7B9E D6C4 37BB DA64 6F15 Linux user #460716 "We should continually be striving to transform every art into a science: in the process, we advance the art." -- Donald Knuth _______________________________________________ SciPy-User mailing list [hidden email] http://mail.scipy.org/mailman/listinfo/scipy-user |
In reply to this post by Giovanni Plantageneto
Am 12.1.2011 um 13:05 schrieb <[hidden email]> <[hidden email]>: > > Hi, > > I am using cython to speed up some computations (btw, thanks a lot to the people who gave me suggestions on a previous thread). > I don't understand how to apply the fast C-coded sin/sqrt functions to a numpy array. > I can import from math.h like this: > > cdef extern from "math.h" > double sin(double) > > but then I can't use this function on a numpy ndarray, obviously. > > Any ideas? It sounds silly to me to write a cycle on ndarray elements. > > Thanks For sin/sqrt computations on large arrays, numexpr + VML might give you better results than a cython implementation, and with less effort. numexpr with VML uses Intels VML library which gives much better performance than a C loop, at least for sin, for sqrt the performance is similar. In addition, numexpr-VML makes use of all your CPU cores. Gregor _______________________________________________ SciPy-User mailing list [hidden email] http://mail.scipy.org/mailman/listinfo/scipy-user |
Free forum by Nabble | Edit this page |