Pyrex - Numeric demo doesn't build in 0.7.2

Fernando Perez fperez528 at yahoo.com
Thu May 15 13:53:18 EDT 2003


Greg Ewing (using news.cis.dfn.de) wrote:

> Fernando Perez wrote:
>>             mat[k,l] = sqrt_N_inv * \
>>                        exp(I*(alpha*(k*k-k*l+l*l) + kap_al*sin(alpha*l)))
> 
> The array indexing and the calls to exp and sin are all
> being done as Python operations.
> 
> For maximum speed, you need to remove all operations
> involving Python objects from the inner loop. This will
> involve declaring appropriate functions from the
> C math library (e.g. sin) so they will be called directly,
> and using the techniques from the Numeric demo to poke
> data directly into the Numeric array.
> 
> You'll also want to avoid dealing with Python complex
> number objects inside the loop. This will mean using
> some suitable C library for doing complex exp() etc.,
> or handling the real and imaginary components yourself.

[snip]

Thanks a lot for your tips.  I was trying to probe pyrex a bit for the kind of
things I typically do with weave.inline (C/C++) or f2py (Fortran).  It seems
that for _this_ case, pyrex may actually not be the easiest tool to use.  A
simple inlining with weave of the critical loop using:

    # This version instantiates a Complex number on each element
    codes['C++ std'] = """
    double phi_kl;
    for (int k=0;k<N;++k)
      for (int l=0;l<N;++l) {
        phi_kl = alpha*(k*k-k*l+l*l) + kap_al*sin(alpha*l);
        mat(k,l) = sqrt_N_inv*std::complex<double>(cos(phi_kl),sin(phi_kl));
        }
    """
    # Call weave to execute the C++ code
    inline(codes[code_tag],['N','alpha','kap_al','mat','sqrt_N_inv','phi'],
           type_converters = converters.blitz,
           support_code = support,
           libraries = ['m'],
           )

is enough to get a factor of 50 speedup over plain python.  So for this kind of
situation, inlined C/C++ is really a very low pain/high gain approach.  But
I'll keep on exploring pyrex for other situations, as it seems to be a very
interesting piece of work.

Best regards,

Fernando.





More information about the Python-list mailing list