Bug when creating vector object in prange loop

143 views
Skip to first unread message

Markus Brachner

unread,
Jul 3, 2015, 10:23:50 AM7/3/15
to cython...@googlegroups.com
I think I found some bug.
I use the current release version of Cython 0.22.1

For me this minimal example leads to a compilation error:
from cython.parallel import prange
from libcpp.vector cimport vector

ctypedef vector[float] pointvector
        
def test_parallel():
    cdef Py_ssize_t j
    cdef pointvector *pv
    
    for j in prange(0,10,nogil=True):
        pv = new pointvector()

The problem is the creation of the pointvector object with the libcpp vector. In a normal range loop the same compiles fine with Cython. It works fine with prange, if I use my own vector wrapper:
cdef extern from "<vector>" namespace "std":
    cpdef cppclass vector[T]:
        cppclass iterator:
            T operator*()
            iterator operator++()
            bint operator==(iterator)
            bint operator!=(iterator)
        vector() nogil
        void push_back(T&) nogil
        T& operator[](int)
        T& at(int)
        iterator begin()
        iterator end()
Can sombebody reproduce this?

Stefan Behnel

unread,
Jul 3, 2015, 11:21:26 AM7/3/15
to cython...@googlegroups.com
You didn't say what the problem is, but my guess is that the error you get
is that Cython refuses to compile code that raises Python exceptions in a
nogil block. In your vector declaration, you are instructing Cython to not
catch C++ exceptions (and convert them to Python exceptions), i.e. you'd
most likely get a crash if allocating the "pointvector" instance fails. So
yes, there is a bug in your code. :)

How to deal with this depends on your actual code, though. You could catch
the exception in a "with gil" block, or you could allocate the C++ objects
outside of the nogil loop, or ...

Stefan

Markus Brachner

unread,
Jul 3, 2015, 2:00:02 PM7/3/15
to cython...@googlegroups.com, stef...@behnel.de
Thanks for your answer Stefan. As I mentioned I get a compilation error. To be more specific: gcc output is
gcc.exe -DMS_WIN64 -mdll -O -Wall -IC:\Anaconda3\envs\py27\lib\site-packages\num
py\core\include -IC:\Anaconda3\envs\py27\include -IC:\Anaconda3\envs\py27\PC -c
c:\tmp\opentest.cpp -o c:\tmp\opentest.o
c:\tmp\opentest.cpp: In function 'PyObject* __pyx_pf_8opentest_test_parallel(PyO
bject*)':
c:\tmp\opentest.cpp:685:68: error: invalid conversion from 'int' to '__pyx_t_8op
entest_pointvector* {aka std::vector<float>*}' [-fpermissive]
             __pyx_t_8opentest_pointvector * __pyx_parallel_temp0 = 1;
                                                                    ^
error: command 'gcc.exe' failed with exit status 1

This is a minimal example, thus I don't do any error handling. It does not even compile. I mentioned also, that wrapping the vector class works by my own works around the problem. In this case it runs without crashing.

Best,
Markus
Reply all
Reply to author
Forward
0 new messages