Cython和SIMD内部函数:阻止将SIMD内部函数的参数转换为python对象

问题描述 投票:0回答:1

我在通过cython尝试SIMD内在函数方面取得了一些成功。现在,我正在努力使AVX中的compare函数起作用,因为compare函数需要一个不应转换为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。任何想法如何解决此问题吗?

python cython simd intrinsics avx
1个回答
0
投票

我做了两个更改:在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端调用和使用。至于结果,还是很奇怪,所以我不知道这种方式是否正确。当我了解更多信息时,我会报告。同时,请随时鸣叫。谢谢。

© www.soinside.com 2019 - 2024. All rights reserved.