I am struggling to initialise thread-local ndarrays with cython.parallel
:
Pseudo-code:
cdef:
ndarray buffer
with nogil, parallel():
buffer = np.empty(...)
for i in prange(n):
with gil:
print "Thread %d: data address: 0x%x" % (threadid(), <uintptr_t>buffer.data)
some_func(buffer.data) # use thread-local buffer
cdef void some_func(char * buffer_ptr) nogil:
(... works on buffer contents...)
My problem is that in all threads buffer.data
points to the same address. Namely the address of the thread that last assigned buffer
.
Despite buffer
being assigned within the parallel()
(or alternatively prange
) block, cython does not make buffer
a private
or thread-local variable but keeps it as a shared
variable.
As a result, buffer.data
points to the same memory region wreaking havoc on my algorithm.
This is not a problem exclusively with ndarray objects but seemingly with all cdef class
defined objects.
How do I solve this problem?