擅长:python、mysql、java
<p>我相信<code>searchsorted</code>实现了GIL本身(参见<a href="https://github.com/numpy/numpy/blob/e2805398f9a63b825f4a2aab22e9f169ff65aae9/numpy/core/src/multiarray/item_selection.c" rel="nofollow">https://github.com/numpy/numpy/blob/e2805398f9a63b825f4a2aab22e9f169ff65aae9/numpy/core/src/multiarray/item_selection.c</a>,第1664行“<code>NPY_BEGIN_THREADS_DEF</code>”)。在</p>
<p>所以,你可以</p>
<pre><code>for j in prange(0,N, nogil=True):
with gil:
C[j,:,:] = np.searchsorted(A1[j,:], A2, side='left' )
</code></pre>
<p>这暂时要求GIL在Python对象上做必要的工作(希望速度很快),然后它应该在<code>searchsorted</code>内再次发布,允许大量并行运行。在</p>
<hr/>
<p>为了更新,我做了一个快速的测试(<code>A1.shape==(105,100)</code>,<code>A2.shape==(302,302)</code>,数字是任意选择的)。对于10次重复,串行版本需要4.5秒,并行版本需要1.4秒(测试在4核CPU上运行)。你不能得到4倍全速,但你接近了。在</p>
<p>它被编译为<a href="http://docs.cython.org/src/userguide/parallelism.html#compiling" rel="nofollow">described in the documentation</a>。我怀疑如果你没有看到加速,那么它可能是:1)你的数组足够小,以至于函数调用/numpy检查类型和大小的开销占主导地位;2)你没有在启用OpenMP的情况下编译它;或者3)你的编译器不支持OpenMP。在</p>