Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
Message from discussion Error compiling cython with numpy arrays
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Robert Bradshaw  
View profile  
 More options Apr 3 2012, 3:46 am
From: Robert Bradshaw <rober...@gmail.com>
Date: Tue, 3 Apr 2012 00:46:14 -0700
Local: Tues, Apr 3 2012 3:46 am
Subject: Re: [cython-users] Re: Error compiling cython with numpy arrays

On Mon, Apr 2, 2012 at 10:57 PM, J Diviney <justd...@gmail.com> wrote:
> I have a series of arrays each with at least 1000 elements and each of which
> needs to be accessed by about 10 different functions for different purposes.
> I've declared them as global to make the code simpler and to avoid passing
> them as arguments when it's much simpler to pass indices.

Perhaps this would be more naturally written as a class with methods?
(Or maybe not, it all depends and I don't have enough information to
judge.)

> In any case, I've tried these things, ndarray declarations with buffer types
> just aren't working for me, regardless of where I make them.

Could you give a short, complete example of something that doesn't
work for you? If there's a bug here, we'd like to know.
> On 3 April 2012 06:53, Robert Bradshaw <rober...@gmail.com> wrote:

>> On Mon, Apr 2, 2012 at 10:38 PM, J Diviney <justd...@gmail.com> wrote:
>> > For this particular example, I don't need the variable as a global.
>> > However,
>> > it is needed for the project I'm doing.

>> If you really need this, you can assign to a local variable within
>> your function (and then re-assign on exiting if you didn't just change
>> it inplace).

>> > I realise that the ndarray declarations you've used are faster, but
>> > that's
>> > exactly my problem, they refuse to compile and I usually get a message
>> > saying:

>> > 'ndarray' is not a type identifier

>> Could you please send the smallest, complete example you can create
>> where you get this error?

>> > On 2 April 2012 16:32, Aronne Merrelli <aronne.merre...@gmail.com>
>> > wrote:

>> >> On Sun, Apr 1, 2012 at 3:49 PM, J Diviney <justd...@gmail.com> wrote:
>> >> > This is just a simple example of the imports I'm doing. Like I said
>> >> > the
>> >> > code
>> >> > is useless, I just wanted to show what I was having trouble with
>> >> > instead
>> >> > of
>> >> > uploading 500 lines of code and the files that go with it.
>> >> > That's odd, I just checked again, the importing of sqrt from the c
>> >> > library
>> >> > works fine for me. In any case, you can just replace that with:
>> >> > from math import sqrt.
>> >> > Once again, apologies if my description is lacking, haven't posted
>> >> > here
>> >> > before.

>> >> Correct - if you do:

>> >> from libc.math cimport sqrt

>> >> Then you are "done" - all later calls to sqrt(x) in your cdef function
>> >> will directly use the C function.

>> >> In your example code you - do you really need to pull in the variable
>> >> as a global? You are defining the variable outside of any cdef
>> >> function, which isn't really helpful IMO. I do not think you will be
>> >> able to access "start" from python, since python cannot directly
>> >> access cdef variables (only cdef functions). Plus, the first version
>> >> with the declared dimensions would be the fastest; you can do this
>> >> easily if you just cdef an additional input to the test function.
>> >> Specifically, replace this:

>> >> start=nu.zeros(10000,dtype=nu.float)
>> >> def run_test(int n_loops):
>> >>   global start
>> >>   ...

>> >> With the following:

>> >> def run_test2(int n_loops, nu.ndarray[nu.float_t, ndim=1] start):
>> >>   ...

>> >> If you also typedef the arr variable same way - specifically, replace:

>> >>    cdef nu.ndarray arr=nu.zeros(len(start),dtype=nu.float)

>> >> With this (I like to make this 2 steps, it is clearer to me):

>> >>    cdef nu.ndarray[nu.float_t, ndim=1] arr
>> >>    arr=nu.zeros(len(start),dtype=nu.float)

>> >> The cython version is then substantially faster - here is a timing
>> >> result on my machine (I put those functions into dummy.pyx):

>> >> In [14]: %timeit dummy.run_test(10)
>> >> 10 loops, best of 3: 193 ms per loop

>> >> In [15]: %timeit dummy.run_test2(10,np.zeros(10000))
>> >> 1000 loops, best of 3: 996 us per loop

>> >> Hope that helps,
>> >> Aronne


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.