使用 AVX-512 或 AVX-2 对大数据计算 1 位(人口计数) [英] Counting 1 bits (population count) on large data using AVX-512 or AVX-2
问题描述
我有一大块内存,比如 256 KiB 或更长.我想计算整个块中 1 位的数量,或者换句话说:将所有字节的人口计数"值相加.
I have a long chunk of memory, say, 256 KiB or longer. I want to count the number of 1 bits in this entire chunk, or in other words: Add up the "population count" values for all bytes.
我知道 AVX-512 有一个 VPOPCNTDQ 指令计算 512 位向量内每个连续 64 位中 1 位的数量,并且 IIANM 应该可以在每个周期发出其中一个(如果有合适的 SIMD 向量寄存器可用) - 但我没有任何经验编写 SIMD 代码(我更像是一个 GPU 人).另外,我不是 100% 确定编译器支持 AVX-512 目标.
I know that AVX-512 has a VPOPCNTDQ instruction which counts the number of 1 bits in each consecutive 64 bits within a 512-bit vector, and IIANM it should be possible to issue one of these every cycle (if an appropriate SIMD vector register is available) - but I don't have any experience writing SIMD code (I'm more of a GPU guy). Also, I'm not 100% sure about compiler support for AVX-512 targets.
在大多数 CPU 上,仍然不(完全)支持 AVX-512;但 AVX-2 是广泛可用的.我找不到类似于 VPOPCNTDQ 的小于 512 位的向量化指令,所以即使从理论上讲,我也不确定如何使用支持 AVX-2 的 CPU 快速计算位;也许存在这样的东西,而我只是以某种方式错过了它?
On most CPUs, still, AVX-512 is not (fully) supported; but AVX-2 is widely-available. I've not been able to find an less-than-512-bit vectorized instruction similar to VPOPCNTDQ, so even theoretically I'm not sure how to count bits fast with AVX-2 capable CPUs; maybe something like this exists and I just missed it somehow?
无论如何,对于两个指令集中的每一个,我都希望有一个简短的 C/C++ 函数 - 要么使用一些 intristics-wrapper 库,要么使用内联汇编.签名是
Anyway, I'd appreciate a short C/C++ function - either using some intristics-wrapper library or with inline assembly - for each of the two instruction sets. The signature is
uint64_t count_bits(void* ptr, size_t size);
注意事项:
- 与如何在 Sandy Bridge 上的一系列整数中快速将位计数到单独的 bin 中? 相关,但不是骗子.
- 如果这很重要,我们可以假设输入是对齐的.
- 忘记多个内核或套接字,我想要单个(单个线程上的)内核的代码.
推荐答案
AVX-2
@HadiBreis 的评论链接到 文章Wojciech Muła 使用 SSSE3 进行快速人口计数;文章链接到此 GitHub 存储库;并且存储库 具有以下 AVX-2 实施.它基于向量化查找指令,并使用 16 值查找表来查找半字节的位数.
AVX-2
@HadiBreis' comment links to an article on fast population-count with SSSE3, by Wojciech Muła; the article links to this GitHub repository; and the repository has the following AVX-2 implementation. It's based on a vectorized lookup instruction, and using a 16-value lookup table for the bit counts of nibbles.
# include <immintrin.h>
# include <x86intrin.h>
std::uint64_t popcnt_AVX2_lookup(const uint8_t* data, const size_t n) {
size_t i = 0;
const __m256i lookup = _mm256_setr_epi8(
/* 0 */ 0, /* 1 */ 1, /* 2 */ 1, /* 3 */ 2,
/* 4 */ 1, /* 5 */ 2, /* 6 */ 2, /* 7 */ 3,
/* 8 */ 1, /* 9 */ 2, /* a */ 2, /* b */ 3,
/* c */ 2, /* d */ 3, /* e */ 3, /* f */ 4,
/* 0 */ 0, /* 1 */ 1, /* 2 */ 1, /* 3 */ 2,
/* 4 */ 1, /* 5 */ 2, /* 6 */ 2, /* 7 */ 3,
/* 8 */ 1, /* 9 */ 2, /* a */ 2, /* b */ 3,
/* c */ 2, /* d */ 3, /* e */ 3, /* f */ 4
);
const __m256i low_mask = _mm256_set1_epi8(0x0f);
__m256i acc = _mm256_setzero_si256();
#define ITER {
const __m256i vec = _mm256_loadu_si256(reinterpret_cast<const __m256i*>(data + i));
const __m256i lo = _mm256_and_si256(vec, low_mask);
const __m256i hi = _mm256_and_si256(_mm256_srli_epi16(vec, 4), low_mask);
const __m256i popcnt1 = _mm256_shuffle_epi8(lookup, lo);
const __m256i popcnt2 = _mm256_shuffle_epi8(lookup, hi);
local = _mm256_add_epi8(local, popcnt1);
local = _mm256_add_epi8(local, popcnt2);
i += 32;
}
while (i + 8*32 <= n) {
__m256i local = _mm256_setzero_si256();
ITER ITER ITER ITER
ITER ITER ITER ITER
acc = _mm256_add_epi64(acc, _mm256_sad_epu8(local, _mm256_setzero_si256()));
}
__m256i local = _mm256_setzero_si256();
while (i + 32 <= n) {
ITER;
}
acc = _mm256_add_epi64(acc, _mm256_sad_epu8(local, _mm256_setzero_si256()));
#undef ITER
uint64_t result = 0;
result += static_cast<uint64_t>(_mm256_extract_epi64(acc, 0));
result += static_cast<uint64_t>(_mm256_extract_epi64(acc, 1));
result += static_cast<uint64_t>(_mm256_extract_epi64(acc, 2));
result += static_cast<uint64_t>(_mm256_extract_epi64(acc, 3));
for (/**/; i < n; i++) {
result += lookup8bit[data[i]];
}
return result;
}
AVX-512
同一个存储库还有一个基于 VPOPCNT 的 AVX-512 实现.在列出它的代码之前,这里是简化且更易读的伪代码:
AVX-512
The same repository also has a VPOPCNT-based AVX-512 implementation. Before listing the code for it, here's the simplified and more readable pseudocode:
对于每个连续的 64 字节序列:
For every consecutive sequence of 64 bytes:
- 将序列加载到 64x8 = 512 位的 SIMD 寄存器中
- 在该寄存器上执行 8 次 64 位的并行总体计数
- 将 8 个人口计数结果并行添加到累加器"中;登记持有 8 笔款项
将累加器中的 8 个值相加
Sum up the 8 values in the accumulator
如果尾部小于 64 字节,则以更简单的方式计算其中的位数
If there's a tail of less than 64 bytes, count the bits there in some simpler way
返回主和加上尾和
现在是真正的交易:
# include <immintrin.h>
# include <x86intrin.h>
uint64_t avx512_vpopcnt(const uint8_t* data, const size_t size) {
const size_t chunks = size / 64;
uint8_t* ptr = const_cast<uint8_t*>(data);
const uint8_t* end = ptr + size;
// count using AVX512 registers
__m512i accumulator = _mm512_setzero_si512();
for (size_t i=0; i < chunks; i++, ptr += 64) {
// Note: a short chain of dependencies, likely unrolling will be needed.
const __m512i v = _mm512_loadu_si512((const __m512i*)ptr);
const __m512i p = _mm512_popcnt_epi64(v);
accumulator = _mm512_add_epi64(accumulator, p);
}
// horizontal sum of a register
uint64_t tmp[8] __attribute__((aligned(64)));
_mm512_store_si512((__m512i*)tmp, accumulator);
uint64_t total = 0;
for (size_t i=0; i < 8; i++) {
total += tmp[i];
}
// popcount the tail
while (ptr + 8 < end) {
total += _mm_popcnt_u64(*reinterpret_cast<const uint64_t*>(ptr));
ptr += 8;
}
while (ptr < end) {
total += lookup8bit[*ptr++];
}
return total;
}
lookup8bit
是一个针对字节而不是位的 popcnt 查找表,定义为 此处.正如评论者所指出的,在最后使用 8 位查找表并不是一个好主意,可以改进.
The lookup8bit
is a popcnt lookup table for bytes rather than bits, and is defined here. edit: As commenters note, using an 8-bit lookup table at the end is not a very good idea and can be improved on.
这篇关于使用 AVX-512 或 AVX-2 对大数据计算 1 位(人口计数)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!