英特尔Intrinsics代码优化

时间:2018-02-23 11:04:23

标签: c optimization intel intrinsics

所以我试图将一个常数与short int a [101]与intel内在函数相乘。我已经做了添加,但我似乎无法弄清楚为什么它不会与乘法工作。在我们使用32位的整数之前,现在我们使用16位短路,所以我们可以在内在函数中有两倍的值来填充128位据我所知?

我试图做的天真的例子:

int main(int argc, char **argv){
    short int a[101];
    int len = sizeof(a)/sizeof(short);

    /*Populating array a with values 1 to 101*/

    mult(len, a);

    return 0;
}

int mult(int len, short int *a){
    int result = 0;
    for(int i=0; i<len; i++){
        result += a[i]*20;  
    }
    return result;
}

我的代码试图在内在函数中做同样的事情

/*Same main as before with a short int a[101] containing values 1 to 101*/

int SIMD(int len, short int *a){
    int res;
    int val[4];

    /*Setting constant value to mulitply with*/
    __m128i sum = _mm_set1_epi16(20);
    __m128i s = _mm_setzero_si128( );

    for(int i=0; i<len/4*4; i += 4){
        __m128i vec = _mm_loadu_si128((__m128i *)(a+i));
        s += _mm_mul_epu32(vec,sum);
    }

    _mm_storeu_si128((__m128i*) val, s);
    res += val[0] + val[1] + val[2] + val[3];

    /*Haldeling tail*/
    for(int i=len/4*4; i<len; i++){
        res += a[i];
    }
    return res;
}

所以我得到一个数字作为结果,但数字与天真的方法不匹配,我尝试了其他内在函数和更改数字,看看它是否有任何明显的差异,但没有什么接近我期望的输出。计算时间与此时的幼稚几乎相同。

1 个答案:

答案 0 :(得分:1)

一个short中有8个__m128i。所以:

for(int i=0; i<len/4*4; i += 4)

应该是

for(int i=0; i<len/8*8; i += 8)`

res += val[0] + val[1] + val[2] + val[3];

应该是:

res += val[0] + val[1] + val[2] + val[3] + val[4] + val[5] + val[6] + val[7];

for(int i=len/4*4; i<len; i++)

应该是:

for(int i=len/8*8; i<len; i++)

在:

s += _mm_mul_epu32(vec,sum);

_mm_mul_epu32对32位元素进行操作。它应该是:

s += _mm_mullo_epi16(vec, sum);

对象res未初始化;它应该是:

int res = 0;

这是工作代码:

#include <stdio.h>
#include <stdlib.h>

#include <immintrin.h>

//  Number of elements in an array.
#define NumberOf(x) (sizeof (x) / sizeof *(x))


//  Compute the result with scalar arithmetic.
static int mult(int len, short int *a)
{
    int result = 0;
    for (size_t i=0; i<len; i++)
    {
        result += a[i]*20;  
    }
    return result;
}


//  Compute the result with SIMD arithmetic.
static int SIMD(int len, short int *a)
{
    //  Initialize the multiplier and the sum.
    __m128i multiplier = _mm_set1_epi16(20);
    __m128i s = _mm_setzero_si128( );

    //  Process blocks of 8 short.
    for (int i=0; i<len/8*8; i += 8)
    {
        __m128i vec = _mm_loadu_si128((__m128i *)(a+i));

        //  Multtiply by multiplier and add to sum.
        s = _mm_add_epi16(s, _mm_mullo_epi16(vec, multiplier));
    }

    //  Store the sum so far so its individual elements can be manipulated.
    short val[8];
    _mm_storeu_si128((__m128i*) val, s);

    //  Add the individual elements.
    int res = 0;
    for (size_t i = 0; i < 8; ++i)
        res += val[i];

    //  Add the elements in the tail.
    for (size_t i = len/8*8; i < len; ++i)
    {
        res += a[i];
    }

    return res;
}



int main(int argc, char **argv)
{
    short int a[96];
    int len = NumberOf(a);

    //  Initiailize a.
    for (size_t i = 0; i < len; ++i)
        a[i] = i+1;

    printf("sum by scalar arithmetic is %d.\n", mult(len, a));
    printf("sum by SIMD arithmetic is %d.\n", SIMD(len, a));

    return 0;
}