首页 > 解决方案 > Cython 和 SIMD 内在函数:防止 SIMD 内在函数的参数转换为 python 对象

问题描述

我在通过 cython 尝试 SIMD 内在函数方面取得了一些成功。现在我正在努力让 AVX 中的比较函数工作,因为比较函数需要一个不应转换为 python 对象的参数。

cdef extern from "immintrin.h" nogil:  # in this example, we use SSE2
    ctypedef float  __m256
    const int _CMP_GT_OS

    __m256 _mm256_loadu_ps  (float *__P) nogil  
    void   _mm256_storeu_ps (float *__P, __m256 __A) nogil
    __m256 _mm256_set1_ps   (__m256 __A) nogil
    __m256 _mm256_cmp_ps    (__m256 __A, __m256 __B, _CMP_GT_OS) nogil


@cython.boundscheck(False)      # turn off bounds-checking for entire function
@cython.wraparound (False)      # turn off negative index wrapping for entire function
@cython.cdivision  (True )        
cdef void Example_v4 (float *A, float *B, float delx) :
    ### this example for A & B having exactly 8 elements

    cdef:
        __m256 mA, mB, mdelx, mOut
        float *out = <float*> malloc( 8 * sizeof(float)) 
        int i

    with nogil:
        mdelx = _mm256_set1_ps( delx )
        mA    = _mm256_loadu_ps( &A[0] )
        mB    = _mm256_loadu_ps( &B[0] )        

        mOut = _mm256_cmp_ps  ( mA, mB, _CMP_GT_OS )        
        _mm256_storeu_ps( &out[0], mOut )

    print ( " i     out  " )
    for i in range(8):
        print ( i, out[i] )
    return

问题是当我编译 cython 代码时,我将此部分突出显示为问题。

        mOut = _mm256_cmp_ps  ( mA, mB, _CMP_GT_OS ) 

with  ^ symbol pointing at _CMP_GT_OS

和留言

Converting to Python object not allowed without gil

我相信问题不在于 gil,内在函数在英特尔官方文档中定义为

__m256 _mm256_cmp_ps (__m256 __A, __m256 __B, const int imm8)

imm8 可以是多种操作类型,_CMP_GT_OS 就是其中之一。我不知道如何处理第三个参数并阻止它转换为 python,因为内在函数只识别 C/C++ const int。知道如何解决这个问题吗?

标签: pythoncythonsimdintrinsicsavx

解决方案


我做了两个更改:在 cdef extern 部分,我为其添加了一个值。我仍然不能 100% 确定如何使用此功能。所以这都是反复试验,但至少这样做,我可以继续检查并做更多的反复试验。

cdef extern from "immintrin.h" nogil:  # in this example, we use SSE2
    ctypedef float  __m256
    const int _CMP_GT_OS = 14

    ## other definition like in the question
    __m256 _mm256_cmp_ps    (__m256 __A, __m256 __B, const int _CMP_GT_OS) nogil

仅更改 _mm256_cmp_ps 部分中的声明不起作用。我必须先给它赋值。

现在它可以无错误地编译,并且可以在python端调用和使用。至于结果,还是很奇怪,不知道这样对不对。当我了解更多时,我会报告。在此期间,请随时插话。谢谢。


推荐阅读