20

我有四个 64 位浮点值的压缩向量。
我想得到向量元素的总和。

使用 SSE(并使用 32 位浮点数)我可以执行以下操作:

v_sum = _mm_hadd_ps(v_sum, v_sum);
v_sum = _mm_hadd_ps(v_sum, v_sum);

不幸的是,尽管 AVX 具有 _mm256_hadd_pd 指令,但它的结果与 SSE 版本不同。我相信这是因为大多数 AVX 指令分别作为每个低 128 位和高 128 位的 SSE 指令工作,而从未跨越 128 位边界。

理想情况下,我正在寻找的解决方案应遵循以下准则:
1)仅使用 AVX/AVX2 指令。(无 SSE)
2)在不超过 2-3 条指令中完成。

但是,任何有效/优雅的方法(即使不遵循上述准则)总是被广泛接受。

非常感谢您的帮助。

——路易吉·卡斯特利

4

3 回答 3

28

如果您有两个__m256d向量x1并且x2每个向量都包含double要水平求和的四个 s,您可以这样做:

__m256d x1, x2;
// calculate 4 two-element horizontal sums:
// lower 64 bits contain x1[0] + x1[1]
// next 64 bits contain x2[0] + x2[1]
// next 64 bits contain x1[2] + x1[3]
// next 64 bits contain x2[2] + x2[3]
__m256d sum = _mm256_hadd_pd(x1, x2);
// extract upper 128 bits of result
__m128d sum_high = _mm256_extractf128_pd(sum1, 1);
// add upper 128 bits of sum to its lower 128 bits
__m128d result = _mm_add_pd(sum_high, _mm256_castpd256_pd128(sum));
// lower 64 bits of result contain the sum of x1[0], x1[1], x1[2], x1[3]
// upper 64 bits of result contain the sum of x2[0], x2[1], x2[2], x2[3]

因此,看起来 3 条指令将完成您需要的 2 条水平总和。以上内容未经测试,但您应该了解这个概念。

于 2012-03-19T19:24:24.307 回答
4

如果您只想要总和,并且可以接受一些标量代码:

__m256d x;
__m256d s = _mm256_hadd_pd(x,x);
return ((double*)&s)[0] + ((double*)&s)[2];
于 2013-12-05T01:39:08.700 回答
3

假设以下情况,您有一个__m256d包含 4 个压缩双精度的向量,并且您想计算其分量的总和,即a0, a1, a2, a3您想要的每个双精度分量,a0 + a1 + a2 + a3然后是另一个 AVX 解决方案:

// goal to calculate a0 + a1 + a2 + a3
__m256d values = _mm256_set_pd(23211.24, -123.421, 1224.123, 413.231);

// assuming _mm256_hadd_pd(a, b) == a0 + a1, b0 + b1, a2 + a3, b2 + b3 (5 cycles) ...
values = _mm256_hadd_pd(values, _mm256_permute2f128_pd(values, values, 1));
// ^^^^^^^^^^^^^^^^^^^^ a0 + a1, a2 + a3, a2 + a3, a0 + a1

values = _mm256_hadd_pd(values, values);
// ^^^^^^^^^^^^^^^^^^^^ (a0 + a1 + a2 + a3), (a0 + a1 + a2 + a3), (a2 + a3 + a0 + a1), (a2 + a3 + a0 + a1)

// Being that addition is associative then each component of values contains the sum of all its initial components (11 cycles) to calculate, (1-2 cycles) to extract, total (12-13 cycles)
double got = _mm_cvtsd_f64(_mm256_castpd256_pd128(values)), exp = (23211.24 + -123.421 + 1224.123 + 413.231);

if (got != exp || _mm256_movemask_pd(_mm256_cmp_pd(values, _mm256_set1_pd(exp), _CMP_EQ_OS)) != 0b1111)
    printf("Failed to sum double components, exp: %f, got %f\n", exp, got);
else
    printf("ok\n");

该解决方案具有广播的总和,这可能有用...

如果我误解了问题,我深表歉意。

$ uname -a
Darwin Samys-MacBook-Pro.local 13.3.0 Darwin Kernel Version 13.3.0: Tue Jun  3 21:27:35 PDT 2014; root:xnu-2422.110.17~1/RELEASE_X86_64 x86_64

$ gcc --version
Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/usr/include/c++/4.2.1
Apple LLVM version 5.1 (clang-503.0.40) (based on LLVM 3.4svn)
Target: x86_64-apple-darwin13.3.0
Thread model: posix
于 2014-08-14T15:45:52.197 回答