More C++ Wrapper bugs

15 views
Skip to first unread message

SevenThunders

unread,
Apr 7, 2010, 11:21:09 PM4/7/10
to cython-users
I think I've found some more C++ wrapper bugs, or at the very least
a failure to generate a cython exception prior to launching the
compiler.

The toy C++ header
----------badmat.h------------------------
typedef struct {
unsigned int x,y,z ;
} dim3 ;

typedef enum {
Realmatrix = 0,
Complexmatrix=1 ,
Cuppermatrix = 2,
} MatrixType ;


class badmat {
public:
int nrows ;
int ncols ;
int ld ;
MatrixType mtype ;
float *val ;
dim3 block ;
dim3 grid ;
dim3 residual ;
badmat(unsigned i, unsigned j, unsigned nld, MatrixType mt, float
*fptr) ;

badmat() {}
void cgauss() ;


} ;
--------------------------------------------
the .pyx source
----------badmat.pyx------------------------
import numpy as np
cimport numpy as np
import sys

cdef extern from "badmat.h" :
ctypedef struct dim3:
int x, y, z
ctypedef enum MatrixType :
Realmatrix = 0
Complexmatrix=1
Cuppermatrix = 2
cdef cppclass badmat:
int nrows
int ncols
int ld
MatrixType mtype
float *val
dim3 block
dim3 grid
dim3 residual
badmat()
void cgauss()
badmat(unsigned i, unsigned j, unsigned nld, MatrixType mt,
float *fptr)

# Python wrapper for a float pointer
cdef class FloatPtr :
cdef float *ptr
cdef setf(FloatPtr self, float *fp) :
self.ptr = fp
cdef float *getf(FloatPtr self) :
return self.ptr

cdef class Gmat:
cdef badmat *gptr # C++ pointer instance
def __cinit__(self, int r, int c, int ld, int mt, FloatPtr f):
if (f.ptr == NULL) :
self.gptr = new badmat(r,c, ld, <MatrixType> mt)
else:
self.gptr = new badmat(r,c,ld, <MatrixType> mt,f.ptr)
def __dealloc__(self) :
del self.gptr


cpdef Gmat cgauss(int r, int c) :
# cdef MatrixType mtype = Complexmatrix
cdef FloatPtr f
f.setf(NULL)
cdef Gmat g = Gmat(r,c,r,Complexmatrix, f)
g.gptr[0].cgauss()
return(g)
--------------------------------------------------------
The compiler error on linux
[matt@myhost cudamex]$ python setup.py build_ext --inplace
running build_ext
cythoning badmat.pyx to badmat.cpp
building 'gpumat' extension
gcc -pthread -fno-strict-aliasing -DNDEBUG -march=x86-64 -
mtune=generic -O2 -pipe -fPIC -I. -I/usr/include/python2.6 -I/usr/lib/
python2.6/site-packages/numpy/core/include -I/usr/local/cuda/include -
I/usr/local/cudasdk/common/inc -I/usr/include/python2.6 -c badmat.cpp -
o build/temp.linux-x86_64-2.6/badmat.o
badmat.cpp: In function ‘int
__pyx_pf_6gpumat_4Gmat___cinit__(PyObject*, PyObject*, PyObject*)’:
badmat.cpp:873: error: expected primary-expression before ‘<’ token
badmat.cpp:873: error: ‘error’ was not declared in this scope
badmat.cpp:873: error: expected primary-expression before ‘;’ token
error: command 'gcc' failed with exit status 1


The bizarre code in badmat.cpp that generated the error:
------------------------
/ * def __cinit__(self, int r, int c, int ld, int mt, FloatPtr f):
* if (f.ptr == NULL) :
* self.gptr = new badmat(r,c, ld, <MatrixType>
mt) # <<<<<<<<<<<<<<
* else:
* self.gptr = new badmat(r,c,ld, <MatrixType> mt,f.ptr)
*/
((struct __pyx_obj_6gpumat_Gmat *)__pyx_v_self)->gptr = <error>;
goto __pyx_L6;
}
/*else*/
-------------------------------------

I've got a more complex version of this that generates a similar but
more subtle error. Hopefully fixing this one will repair the other
case as well.

Reply all
Reply to author
Forward
0 new messages