cython.parallel.prange 中的 cython 共享内存 - 块 [英] cython shared memory in cython.parallel.prange - block
问题描述
我有一个函数 foo
,它接受一个指向内存的指针作为参数,同时写入和读取该内存:
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)
我像这样分配data
:
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)
我现在的问题是:不同的线程如何处理这个?我的猜测是 data
指向的内存将被所有线程共享,并在函数 foo
内部同时"读取或写入.这会弄乱所有结果,因为人们不能依赖先前设置的数据值(在 foo
内)?我的猜测是正确的还是在 cython 编译器中实现了一些神奇的安全带?
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?
非常感谢您.
推荐答案
我假设没有读或写同步锁 data
线程将读/写内存位置并覆盖彼此的更改.如果没有某种同步,您将无法获得一致的结果.
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.
虽然文档(http://docs.cython.org/src/userguide/parallelism.html) 似乎表明 OpenMP(默认后端)会自动创建线程局部变量.
Although the docs (http://docs.cython.org/src/userguide/parallelism.html) seem to suggest that OpenMP (the default backend) automatically creates thread locals.
这篇关于cython.parallel.prange 中的 cython 共享内存 - 块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!