Dag Sverre Seljebotn <da...@...> writes:
> > * Telling Cython where to find cvode.h, nvector.h, sundials.h (see [11])
> >   
> I suspect the problem here is including "cvode", "nvector" etc. directly 
> in include_path. Instead you should likely do
> 
> cdef extern from "cvode/....h":
>     ...
> 
> and in setup.py simly have include_paths=[".../include"] rather than 
> [".../include/cvode", ".../include/nvector", ...]. (This is how 
> libraries are usually set up anyway.)

Thanks for the information; I'm not familiar with C libraries. Having looked in 
the /lib directory, I found that the library is called sundials_cvode, not just 
cvode. I modified setup.py to use "libraries=['sundials_cvode'])" and could 
then recompile without error. (I had to manually remove the build/ directory 
and hello.c, though.)

== Successful compilation ==
bash-3.2$ python setup.py build_ext --inplace
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 -
lpython2.5 -o hello.so

== setup.py ==
"""Run with e.g. python setup.py build_ext --inplace"""
from distutils.core import setup
from distutils.extension import Extension
from Cython.Distutils import build_ext
setup(
    cmdclass = {'build_ext': build_ext},
    ext_modules = [
        Extension("hello",
            ["hello.pyx"],
            include_dirs=['/site/VERSIONS/sundials-2.3.0/include'],
            library_dirs=['/site/VERSIONS/sundials-2.3.0/lib'],
            libraries=['sundials_cvode']),
    ]
)

== hello.pyx ==
cdef extern from "cvode/cvode.h":
   int CVodeSetFdata(void *cvode_mem, void *f_data)

> > * Importing anything from Sundials (see [12]). Currently "python
> > setup.py build_ext --inplace" complains about not finding nvector.h
> > [13]

==> Solved 8-)

I chose CVodeSetFdata for the above example because it didn't depend on any 
user-defined types. Now onto the next challenge...

> > * Allocating an NVector with Sundials (see [3, 7]).
> >   
> Note that the call seems to allocate the ydot NVector for you (whether 
> you still need to allocate other NVector's I didn't bother to check now; 
> but it is likely straightforward).

Yes, ydot is allocated by CVODE (though not initialized to zero, as I learned 
the hard way). I do need to allocate y, however.

As a start, I would like Cython to compile the following line of code:

y0 = N_VNew_Serial(N)

However, I'm unsure how much of the C declarations I need to duplicate (with 
ctypedef?). N_VNew_Serial returns an N_Vector:
include/nvector/nvector_serial.h:N_Vector N_VNew_Serial(long int vec_length);

The generic definition of an N_Vector is here:

== BEGIN include/sundials/sundials_nvector.h (excerpt) ==
#include <sundials/sundials_types.h>

/*
 * -----------------------------------------------------------------
 * Generic definition of N_Vector
 * -----------------------------------------------------------------
 */

/* Forward reference for pointer to N_Vector_Ops object */
typedef struct _generic_N_Vector_Ops *N_Vector_Ops;

/* Forward reference for pointer to N_Vector object */
typedef struct _generic_N_Vector *N_Vector;

/* Define array of N_Vectors */
typedef N_Vector *N_Vector_S;

/* Structure containing function pointers to vector operations  */  
struct _generic_N_Vector_Ops {
  N_Vector    (*nvclone)(N_Vector);
  N_Vector    (*nvcloneempty)(N_Vector);
  void        (*nvdestroy)(N_Vector);
  void        (*nvspace)(N_Vector, long int *, long int *);
  realtype*   (*nvgetarraypointer)(N_Vector);
  void        (*nvsetarraypointer)(realtype *, N_Vector);
  void        (*nvlinearsum)(realtype, N_Vector, realtype, N_Vector, N_Vector); 
  void        (*nvconst)(realtype, N_Vector);
  void        (*nvprod)(N_Vector, N_Vector, N_Vector);
  void        (*nvdiv)(N_Vector, N_Vector, N_Vector);
  void        (*nvscale)(realtype, N_Vector, N_Vector);
  void        (*nvabs)(N_Vector, N_Vector);
  void        (*nvinv)(N_Vector, N_Vector);
  void        (*nvaddconst)(N_Vector, realtype, N_Vector);
  realtype    (*nvdotprod)(N_Vector, N_Vector);
  realtype    (*nvmaxnorm)(N_Vector);
  realtype    (*nvwrmsnorm)(N_Vector, N_Vector);
  realtype    (*nvwrmsnormmask)(N_Vector, N_Vector, N_Vector);
  realtype    (*nvmin)(N_Vector);
  realtype    (*nvwl2norm)(N_Vector, N_Vector);
  realtype    (*nvl1norm)(N_Vector);
  void        (*nvcompare)(realtype, N_Vector, N_Vector);
  booleantype (*nvinvtest)(N_Vector, N_Vector);
  booleantype (*nvconstrmask)(N_Vector, N_Vector, N_Vector);
  realtype    (*nvminquotient)(N_Vector, N_Vector);
};

/*
 * -----------------------------------------------------------------
 * A vector is a structure with an implementation-dependent
 * 'content' field, and a pointer to a structure of vector
 * operations corresponding to that implementation.
 * -----------------------------------------------------------------
 */

struct _generic_N_Vector {
  void *content;
  struct _generic_N_Vector_Ops *ops;
};
== END include/sundials/sundials_nvector.h (excerpt) ==

(Pardon the long quote; unfortunately, I couldn't find a web repository for the 
Sundials source code. An earlier version is in this Ubuntu package, but note 
that the file hierarchy is different than the include/sundials/, include/
cvode/, etc.:
http://www.google.com/codesearch/p?hl=en#jVvm31OatEM/sundials/cvode/include/
cvode.h&q=nvector%20sundials%20cvode

(For all I know, the file hierarchy may be different in the source distribution 
and once it is installed as a library -- I'm really an absolute beginner in the 
C world.)

The abovementioned realtype (and booleantype) is defined in
include/sundials/sundials_types.h
from which I roughly quote some conditional defines:
#if defined(SUNDIALS_SINGLE_PRECISION)
typedef float realtype;
#elif defined(SUNDIALS_DOUBLE_PRECISION)
typedef double realtype;
#elif defined(SUNDIALS_EXTENDED_PRECISION)
typedef long double realtype;

As for the structs, I've tried and failed along these lines:
cdef extern from "sundials/sundials_nvector.h":
    ctypedef struct _generic_N_Vector
    ctypedef struct _generic_N_Vector *N_Vector
                                     ^
------------------------------------------------------------
/xanadu/home/jonvi/svn/trunk/cysundials/hello.pyx:8:38: Syntax error in struct 
or union definition

Do I really have to replicate the entire struct definition?


Thanks a lot for your help!

Regards,
Jon Olav


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

Reply via email to