Basically, I keep getting the following error message:
Error compiling Cython file:
------------------------------------------------------------
...
global cl_positions, cl_radii, cl_elements, positions,\
radii, cl_masses, masses, cl_wpos, cl_wvel,\
cl_velocities
cdef:
int prox, el, temp
FLOAT_t score, d_big, l=0.0, m_dir
nu.ndarray[ndim=1,dtype=FLOAT] v_big, v_rel, dist
^
------------------------------------------------------------
ast_builder.pyx:406:8: 'ndarray' is not a type identifier
I would provide my code, but it consists of a few hundred
lines and isn't completed. However, every single instance
of ndarray is getting this error message.
Dropping the variable typing here is an option, but the
code references arrays a huge number of times with each
iteration, so I would really rather not do so.
Does anyone have any idea why this might be happening?
(By the way, I'm using Python 2.7.2 with Numpy 1.6.1 and
Cython 0.14.1 on a Windows 7 machine).
Thanks in advance for any help
At a few hundred lines, a link to the full code could be helpful.
> Dropping the variable typing here is an option, but the
> code references arrays a huge number of times with each
> iteration, so I would really rather not do so.
>
> Does anyone have any idea why this might be happening?
>
> (By the way, I'm using Python 2.7.2 with Numpy 1.6.1 and
> Cython 0.14.1 on a Windows 7 machine).
>
> Thanks in advance for any help
What is nu? Is it supposed to be np (e.g. if you did a cimport numpy as np)?
- Robert
What is this even supposed to do? When I compile it, I get a compile error:
Error compiling Cython file:
------------------------------------------------------------
...
from libc.math cimport sqrt
cdef double sqrt(double x):
^
------------------------------------------------------------
sq.pyx:2:5: Function signature does not match previous declaration
(and I'm quite happy to get one at all, even if it's not exactly the one
I'd like to get)
Stefan
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
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?
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.