Jon Olav Vik <jono...@...> writes:
> Thank you so much! This seems to work well [1] after some adjustments, and 
> I've even been able to print and modify the contents of an N_Vector.

Now I've had a go at another subtask:

> * Handle the NVector from Python: exchange data with a Numpy array,
> and pass the NVector to a C or Cython function.

The functions arr2nv() and nv2arr() work [1], at least within Cython. It can 
probably be done faster, but I don't expect frequent copying to/from N_Vectors.

I did try "cdef N_Vector arr2nv(np.ndarray[np.int_t] x):", based on
http://wiki.cython.org/tutorials/numpy#Efficientindexing
but I couldn't "cimport numpy as np" as required to get at np.int_t. Any ideas 
why I'd get "error: numpy/arrayobject.h: No such file or directory"? [2]

Regards,
Jon Olav


[1]
== compilation, running, output ==
bash-3.2$ rm -rf build hello.so hello.c && python setup.py build_ext --inplace 
&& python -c "import hello"
running build_ext
cythoning hello.pyx to hello.c
building 'hello' extension
creating build
creating build/temp.linux-x86_64-2.5
gcc -pthread -fno-strict-aliasing -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-
prototypes -fPIC -I/site/VERSIONS/sundials-2.3.0/include -I/site/VERSIONS/
compython-2.5/Linux/include/python2.5 -c hello.c -o build/temp.linux-x86_64-2.5/
hello.o
gcc -pthread -shared build/temp.linux-x86_64-2.5/hello.o -L/site/VERSIONS/
sundials-2.3.0/lib -L/site/VERSIONS/compython-2.5/Linux/lib -lsundials_cvode -
lsundials_nvecserial -lpython2.5 -o hello.so
1.1297001e-312
4.4472177e-316
1.5810101e-322

          0
          1
          2

[ 0.  1.  2.]
          0
          1
          2

== hello.pyx ==
"""Trying to modify an NVector"""

cdef extern from "sundials/sundials_types.h":
    ctypedef double realtype

cdef extern from "sundials/sundials_nvector.h":
    cdef struct _generic_N_Vector:
        void* content
    ctypedef _generic_N_Vector* N_Vector
    N_Vector N_VNew_Serial(long int vec_length)
    void N_VDestroy_Serial(N_Vector v)
    void N_VPrint_Serial(N_Vector v)

cdef extern from "nvector/nvector_serial.h":
    cdef struct _N_VectorContent_Serial:
        long int length
        realtype* data
    ctypedef _N_VectorContent_Serial* N_VectorContent_Serial

import numpy as np
cdef nv2arr(N_Vector v):
    cdef long int n = (<N_VectorContent_Serial>v.content).length
    cdef realtype* v_data = (<N_VectorContent_Serial>v.content).data
    cdef long int i
    x = np.empty(n)
    for i in range(n):
        x[i] = v_data[i]
    return x

# cimport numpy as np
# cdef N_Vector arr2nv(np.ndarray[np.int_t] x):
cdef N_Vector arr2nv(x):
    cdef long int n = len(x)
    cdef N_Vector v = N_VNew_Serial(n)
    cdef realtype* v_data = (<N_VectorContent_Serial>v.content).data
    cdef long int i
    for i in range(n):
        v_data[i] = x[i]
    return v

cdef N_Vector v = N_VNew_Serial(3)
cdef realtype* v_data = (<N_VectorContent_Serial>v.content).data
N_VPrint_Serial(v) # prints uninitialized floats
for i in range(3):
    v_data[i] = i
N_VPrint_Serial(v)
x = nv2arr(v)
print x
N_VDestroy_Serial(v)
v = arr2nv(x)
N_VPrint_Serial(v)
N_VDestroy_Serial(v)

[2]
"cimport numpy" fails inside my hello.pyx too; here follows a minimal example.

bash-3.2$ cat > cimp.pyx
cimport numpy
bash-3.2$ python -c "import pyximport; pyximport.install(build_dir='.'); import 
cimp"
./temp.linux-x86_64-2.5/pyrex/cimp.c:136:31: error: numpy/arrayobject.h: No 
such file or directory
./temp.linux-x86_64-2.5/pyrex/cimp.c:595: error: expected '=', ',', ';', 'asm' 
or '__attribute__' before '__pyx_t_5numpy_int8_t'
...
./temp.linux-x86_64-2.5/pyrex/cimp.c:639: error: expected '=', ',', ';', 'asm' 
or '__attribute__' before '__pyx_t_5numpy_complex_t'
./temp.linux-x86_64-2.5/pyrex/cimp.c:650: error: expected ')' before '*' token
./temp.linux-x86_64-2.5/pyrex/cimp.c: In function 
'__pyx_pf_5numpy_7ndarray___getbuffer__':
./temp.linux-x86_64-2.5/pyrex/cimp.c:737: error: 'PyArray_Descr' undeclared 
(first use in this function)
./temp.linux-x86_64-2.5/pyrex/cimp.c:737: error: (Each undeclared identifier is 
reported only once
./temp.linux-x86_64-2.5/pyrex/cimp.c:737: error: for each function it appears 
in.)
...
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/usit/titan/u1/jonvi/usr//lib/python2.5/site-packages/pyximport/
pyximport.py", line 288, in load_module
    self.pyxbuild_dir)
  File "/usit/titan/u1/jonvi/usr//lib/python2.5/site-packages/pyximport/
pyximport.py", line 154, in load_module
    raise ImportError("Building module failed: %s" % e)
ImportError: Building module failed: CompileError(DistutilsExecError("command 
'gcc' failed with exit status 1",),)


_______________________________________________
Cython-dev mailing list
[email protected]
http://codespeak.net/mailman/listinfo/cython-dev

Reply via email to