cython openmp 单,屏障

pyt*_*eak 3 openmp cython

我正在尝试在 cython 中使用 openmp 。我需要在 cython 中做两件事:

#pragma omp single{}i)在我的 cython 代码中使用范围。

ii) 使用#pragma omp barrier{}

有谁知道如何在 cython 中做到这一点?

以下是更多详细信息。我有一个 nogil cdef 函数my_fun(),我在 omp for 循环中调用它:

from cython.parallel cimport prange
cimport openmp

cdef int i

with nogil:
    for i in prange(10,schedule='static', num_threads=10):
        my_func(i)
Run Code Online (Sandbox Code Playgroud)

在内部my_func,我需要放置一个屏障来等待所有线程赶上,然后仅在其中一个线程中执行一项耗时的操作并获取 gil,然后释放屏障,以便所有线程同时恢复。

cdef int my_func(...) nogil:

    ...

    # put a barrier until all threads catch up, e.g. #pragma omp barrier

    with gil:
        # execute time consuming operation in one thread only, e.g. pragma omp single{}

    # remove barrier after the above single thread has finished and continue the operation over all threads in parallel, e.g. #pragma omp barrier

    ...


Run Code Online (Sandbox Code Playgroud)

ead*_*ead 7

Cython 对 openmp 有一些支持,但如果广泛使用 openmp-pragmas,用 C 编码并用 Cython 包装生成的代码可能更容易。


作为替代方案,您可以使用逐字 C 代码和定义技巧来为 Cython 带来一些功能,但是在定义中使用编译指示并不简单(_Pragma是一个C99 解决方案,MSVC 一如既往地做自己的事情)__pragma),有一些示例作为 Linux/gcc 的概念证明:

cdef extern from *:
    """
    #define START_OMP_PARALLEL_PRAGMA() _Pragma("omp parallel") {
    #define END_OMP_PRAGMA() }
    #define START_OMP_SINGLE_PRAGMA() _Pragma("omp single") {
    #define START_OMP_CRITICAL_PRAGMA() _Pragma("omp critical") {   
    """
    void START_OMP_PARALLEL_PRAGMA() nogil
    void END_OMP_PRAGMA() nogil
    void START_OMP_SINGLE_PRAGMA() nogil
    void START_OMP_CRITICAL_PRAGMA() nogil
Run Code Online (Sandbox Code Playgroud)

我们让 Cython 相信, thatSTART_OMP_PARALLEL_PRAGMA()和 Co. 是 nogil 函数,因此它将它们放入 C 代码中,从而由预处理器拾取。

我们必须使用语法

#pragma omp single{
   //do_something
}
Run Code Online (Sandbox Code Playgroud)

并不是

#pragma omp single
do_something
Run Code Online (Sandbox Code Playgroud)

因为 Cython 生成 C 代码的方式。

用法如下(我在这里避免这样做,from cython.parallel.parallel因为它对于这个简单的示例来说有太多魔力):

%%cython -c=-fopenmp --link-args=-fopenmp
cdef extern from *:# as listed above
    ...

def test_omp():
    cdef int a=0
    cdef int b=0  
    with nogil:
        START_OMP_PARALLEL_PRAGMA()
        START_OMP_SINGLE_PRAGMA()
        a+=1
        END_OMP_PRAGMA()
        START_OMP_CRITICAL_PRAGMA()
        b+=1
        END_OMP_PRAGMA() # CRITICAL
        END_OMP_PRAGMA() # PARALLEL
    print(a,b)
Run Code Online (Sandbox Code Playgroud)

正如预期的那样,在我的机器上用 2 个线程调用test_omp打印“1 2”(可以使用 更改线程数openmp.omp_set_num_threads(10))。

然而,上面的内容仍然非常脆弱 - Cython 的一些错误检查可能会导致无效代码(Cython 使用 goto 进行控制流,并且不可能跳出 openmp-block)。在你的例子中会发生这样的事情:

cimport numpy as np
import numpy as np
def test_omp2():
    cdef np.int_t[:] a=np.zeros(1,dtype=int)

    START_OMP_SINGLE_PRAGMA()
    a[0]+=1
    END_OMP_PRAGMA()

    print(a)
Run Code Online (Sandbox Code Playgroud)

由于边界检查,Cython 将生成:

START_OMP_SINGLE_PRAGMA();
...
//check bounds:
if (unlikely(__pyx_t_6 != -1)) {
    __Pyx_RaiseBufferIndexError(__pyx_t_6);
    __PYX_ERR(0, 30, __pyx_L1_error)  // HERE WE GO A GOTO!
}
...
END_OMP_PRAGMA();
Run Code Online (Sandbox Code Playgroud)

在这种特殊情况下将boundcheck设置为false,即

cimport cython
@cython.boundscheck(False) 
def test_omp2():
   ...
Run Code Online (Sandbox Code Playgroud)

可以解决上面示例的问题,但可能不能解决一般情况。

再说一次:在 C 中使用 openmp(并使用 Cython 包装功能)是一种更愉快的体验。


附带说明一下:Python 线程(由 GIL 管理的线程)和 openmp 线程是不同的,并且彼此一无所知。上面的示例也可以在不释放 GIL 的情况下正确工作(编译和运行) - openmp 线程不关心 GIL,但由于不涉及任何 Python 对象,所以不会出错。因此,我添加了nogil包装的“函数”,因此它也可以在 nogil 块中使用。

然而,当代码变得更加复杂时,就变得不那么明显了,不同 Python 线程之间共享的变量不会被访问(以上都是因为这些访问可能发生在生成的 C 代码中,并且从 Cython 中并不清楚这一点)代码),在使用 openmp 时不释放 gil 可能是更明智的做法。