Search code examples
pythonparallel-processingmalloccythonpython-multithreading

cython shared memory in cython.parallel.prange - block


I have a function foo that takes a pointer to memory as argument and both writes and reads to that memory:

cdef void foo (double *data):
   data[some_index_int] = some_value_double
   do_something_dependent_on (data)

I'm allocating to data like so:

cdef int N = some_int
cdef double *data = <double*> malloc (N * sizeof (double))

cdef int i
for i in cython.parallel.prange (N, nogil=True):
    foo (data)

readout (data)

My question is now: how do the different threads treat this? My guess is that the memory pointed to by data will be shared by all threads and 'simultaneously' read from or written to while inside the function foo. This would then mess up all results as one can't rely on a previously set datavalue (within foo)? Is my guessing correct or is there some magic safety-belt implemented in the cython-compiler?

Thank you very much in advance.


Solution

  • I assume that without read or write synchronization locks to data threads will read/write to the memory location and overwrite each other's changes. You will not get consistent results without some kind of synchronization.

    Although the docs (http://docs.cython.org/src/userguide/parallelism.html) seem to suggest that OpenMP (the default backend) automatically creates thread locals.