Cython:如何在不使用GIL的情况下从numpy内存视图转到vector [pair [double,double]]? [英] Cython: how to go from numpy memory view to vector[pair[double,double]] without needing the GIL?
问题描述
我试图将我的Cython代码中的所有Python调用都转换为纯C语言,以便能够释放GIL并进行并行化。
I am trying to convert all Python calls in my Cython code to pure C to be able to release the GIL and do parallelisation.
我曾经使用过size-list-2的列表是从2D numpy数组初始化的,所以我做了类似的事情:
I used to work with a list of lists-of-size-2 initialized from a 2D numpy array so I did something like that:
cdef double[:,:,:] init=np.random.uniform((10,4,2),dtype=np.float32)
cdef int i
cdef int N=init.shape[0]
for i in range(N):
a=init[i].tolist()
#I then get this list of list
#a=[[1.,1.],[1.,1.],[1.,1.]]
#f acting on list of list
f(a)
我需要在循环内释放GIL,因此需要删除对Python的所有调用。通过使用vector [pair [double,double]]而不是列表并相应地修改f,我现在有:
I need to release the GIL inside the loop so I need to remove all the calls to Python. By using vector[pair[double,double]] instead of lists and modifying f accordingly I now have:
cdef vector[pair[double,double]] a
cdef double[:,:,:] init=np.ones((10,4,2),dtype=np.float32_t)
cdef int i
cdef int N=init.shape[0]
for i in prange(N):
#I need to get a vector[pair[double,double]] from the numpy init[i]
#with f now cdef acting on vector[pair[double,double]]
a=np.asarray(init[i]) #actually works but it goes through Python !
f(a)
如何转换init [i](因此是double [:, :]类型)到向量[pair [double,double]]而不通过python吗?
How to convert init[i] (thus a double[:,:] type) to a vector[pair[double,double]] without going through python ?
推荐答案
我不认为是不使用GIL而从memoryview甚至numpy转到 std :: vector
的任何解决方案。我发现的唯一解决方案是先将大数组转换为 vector [vector [pair [pair [double,double]]]
,然后在没有GIL的情况下访问它第二个for循环,您在其中执行所有密集计算。
I do not think there is any solution to go from memoryview or even numpy to std::vector
without using the GIL. The only solution I found was to first convert the big array to a vector[vector[pair[double,double]]]
and then you can access it without the GIL in a second for loop where you are doing all the intensive computations.
这篇关于Cython:如何在不使用GIL的情况下从numpy内存视图转到vector [pair [double,double]]?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!