x86 - 冲突检测指令如何使循环矢量化变得更容易?

标签 x86 vectorization simd intel-mic avx512

AVX512CD 指令系列包括:VPCONFLICT、VPLZCNT 和 VPBROADCASTM。

The Wikipedia section about these instruction说:

The instructions in AVX-512 conflict detection (AVX-512CD) are designed to help efficiently calculate conflict-free subsets of elements in loops that normally could not be safely vectorized.

有哪些示例表明这些指令在矢量化循环中很有用?如果答案包含标量循环及其矢量化对应部分,将会很有帮助。

谢谢!

最佳答案

CD 指令可能有用的一个示例是直方图。对于标量代码,直方图只是一个简单的循环,如下所示:

load bin index
load bin count at index
increment bin count
store updated bin count at index

通常你不能矢量化直方图,因为你可能在一个向量中多次具有相同的 bin 索引 - 你可能会天真地尝试这样的事情:

load vector of N bin indices
perform gathered load using N bin indices to get N bin counts
increment N bin counts
store N updated bin counts using scattered store

但如果向量中的任何索引相同,则会出现冲突,并且生成的 bin 更新将不正确。

所以,CD 指令可以解决这个问题:

load vector of N bin indices
use CD instruction to test for duplicate indices
set mask for all unique indices
while mask not empty
    perform masked gathered load using <N bin indices to get <N bin counts
    increment <N bin counts
    store <N updated bin counts using masked scattered store
    remove non-masked indices and update mask
end

实际上,这个示例效率很低,并不比标量代码更好,但还有其他计算密集型示例,在这些示例中使用 CD 指令似乎是值得的。通常,这些都是模拟,其中数据元素将以非确定性方式更新。 LAMMPS Molecular Dynamics Simulator 中引用了一个示例(来自 KNL book by Jeffers et al ) .

关于x86 - 冲突检测指令如何使循环矢量化变得更容易?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39913707/

相关文章:

assembly - `test` 指令有什么作用?

memory-management - 我不明白内存寻址中的某些东西

python - 加速二维数组上的 NumPy 循环 - 删除相似的行

r - 应用函数循环遍历 R 中数组的 3 个维度中的 2 个维度

c++ - 没有 AVX2 的 32 位整数的 SSE 整数 2^n 次幂

x86 - 链接器和内核问题

matlab - MATLAB 中函数数组的矢量化或单行求值

simd - 如何使用avx(但没有avx-512)将int 64转换为int 32

c++ - c=c+a*b 的 OpenMP 4 simd 矢量化

x86 - 是否可以在不使用 SSE4 的情况下对 VC++ 中的乘法进行向量化?