这种处理带有SSE的数组尾部的方法是否过度了?



我正在使用SSE,试图编写一个将单精度浮点数组的所有值相加的函数。我希望它适用于数组的所有长度,而不仅仅是4的倍数,就像在网络上几乎所有的例子中所假设的那样。我想到了这样的东西:

float sse_sum(const float *x, const size_t n)
{
    const size_t
        steps = n / 4,
        rem = n % 4,
        limit = steps * 4;
    __m128 
        v, // vector of current values of x
        sum = _mm_setzero_ps(0.0f); // sum accumulator
    // perform the main part of the addition
    size_t i;
    for (i = 0; i < limit; i+=4)
    {
        v = _mm_load_ps(&x[i]);
        sum = _mm_add_ps(sum, v);
    }
    // add the last 1 - 3 odd items if necessary, based on the remainder value
    switch(rem)
    {
        case 0: 
            // nothing to do if no elements left
            break;
        case 1: 
            // put 1 remaining value into v, initialize remaining 3 to 0.0
            v = _mm_load_ss(&x[i]);
            sum = _mm_add_ps(sum, v);
            break;
        case 2: 
            // set all 4 to zero
            v = _mm_setzero_ps();
            // load remaining 2 values into lower part of v
            v = _mm_loadl_pi(v, (const __m64 *)(&x[i]));
            sum = _mm_add_ps(sum, v);
            break;
        case 3: 
            // put the last one of the remaining 3 values into v, initialize rest to 0.0
            v = _mm_load_ss(&x[i+2]);
            // copy lower part containing one 0.0 and the value into the higher part
            v = _mm_movelh_ps(v,v);
            // load remaining 2 of the 3 values into lower part, overwriting 
            // old contents                         
            v = _mm_loadl_pi(v, (const __m64*)(&x[i]));     
            sum = _mm_add_ps(sum, v);
            break;
    }
    // add up partial results
    sum = _mm_hadd_ps(sum, sum);
    sum = _mm_hadd_ps(sum, sum);
    __declspec(align(16)) float ret;
    /// and store the final value in a float variable
    _mm_store_ss(&ret, sum);
    return ret; 
}

然后我开始怀疑这是不是过分了。我的意思是,我被困在SIMD模式中,只能用SSE来处理尾部。这很有趣,但是将尾部相加并使用常规浮点运算计算结果不是同样好(而且更简单)吗?我在上交所这样做有什么收获吗?

我会看看Agner Fog的vectorclass。参见VectorClass.pdf的一节"当数据大小不是向量大小的倍数时"。他列出了五种不同的方法,并讨论了每种方法的优缺点。http://www.agner.org/optimize/vectorclass

一般来说,我从下面的链接得到了这样做的方法。http://fastcpp.blogspot.no/2011/04/how-to-unroll-loop-in-c.html

#define ROUND_DOWN(x, s) ((x) & ~((s)-1))
 void add(float* result, const float* a, const float* b, int N) {
 int i = 0;
 for(; i < ROUND_DOWN(N, 4); i+=4) {
    __m128 a4 = _mm_loadu_ps(a + i);
    __m128 b4 = _mm_loadu_ps(b + i);
    __m128 sum = _mm_add_ps(a4, b4);
    _mm_storeu_ps(result + i, sum);
  }
  for(; i < N; i++) {
      result[i] = a[i] + b[i];
  }
}

按照承诺,我做了一些基准测试。为此,我_aligned_malloc创建了一个大小为100k的浮点数组,用1.123f的单个值填充它,并针对该值测试函数。我写了一个简单的求和函数,它只是在循环中累积结果。接下来,我制作了一个简化的SSE求和函数变体,其中水平和尾部添加了常规浮点数:

float sseSimpleSum(const float *x, const size_t n)
{
    /* ... Everything as before, but instead of hadd: */
    // do the horizontal sum on "sum", which is a __m128 by hand
    const float *sumf = (const float*)(&sum);
    float ret = sumf[0] + sumf[1] + sumf[2] + sumf[3];
    // add up the tail
    for (; i < n; ++i)
    {
        ret += x[i];
    }
    return ret; 
}

我没有得到任何性能影响,有时甚至看起来稍微快了一点,但我发现计时器相当不可靠,所以让我们假设简化的版本等同于复杂的版本。然而,令人惊讶的是,从SSE和朴素浮点求和函数获得的值存在相当大的差异。我怀疑这是由于舍入导致的误差累积,所以我基于Kahan算法编写了一个函数,它给出了正确的结果,尽管比单纯的浮点加法慢得多。为了完整起见,我创建了一个基于SSE kahan的函数,如下所示:

float SubsetTest::sseKahanSum(const float *x, const size_t n)
{
    /* ... init as before... */
    __m128 
        sum = _mm_setzero_ps(), // sum accumulator
        c   = _mm_setzero_ps(), // correction accumulator
        y, t;
    // perform the main part of the addition
    size_t i;
    for (i = 0; i < limit; i+=4)
    {
        y = _mm_sub_ps(_mm_load_ps(&x[i]), c);
        t = _mm_add_ps(sum, y);
        c = _mm_sub_ps(_mm_sub_ps(t, sum), y);
        sum = t;
    }
    /* ... horizontal and tail sum as before... */
}

下面是在vc++ 2010 Release模式下获得的基准测试结果,显示了获得的和的值,计算所需的时间以及与正确值相关的误差量:

Kahan: value = 112300, time = 1155, error = 0
Float: value = 112328.78125, time = 323, error = 28.78125
上证指数:值= 112304.476563,时间= 46,误差= 4.4765625
简单SSE:值= 112304.476563,时间= 45,误差= 4.4765625
Kahan SSE: value = 112300, time = 167, error = 0

简单浮点数加法的错误量是巨大的!我怀疑非kahan SSE函数更准确,因为它们相当于两两求和,可以比直接方法提高精度。Kahan SSE是准确的,但只比单纯的float加法快两倍左右。

在这种情况下,它可能是多余的,除非您可以指出一些真正的性能增益。如果你正在使用gcc,那么这篇关于gcc 4.7的自动向量化的指南可能是一个不错的选择,尽管它显然是gcc特定的,但它不像intrinsic那么难看。

相关内容

最新更新