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

zr.*_*zr. 12 x86 simd vectorization intel-mic avx512

AVX512CD指令系列包括:VPCONFLICT,VPLZCNT和VPBROADCASTM.

关于这些指令的维基百科部分说:

AVX-512冲突检测(AVX-512CD)中的指令旨在帮助有效地计算通常无法安全矢量化的循环中元素的无冲突子集.

有哪些例子表明这些指令在向量化循环中有用?如果答案将包括标量循环及其矢量化对应物将会有所帮助.

谢谢!

Pau*_*l R 9

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

load bin index
load bin count at index
increment bin count
store updated bin count at index
Run Code Online (Sandbox Code Playgroud)

通常你不能对直方图进行矢量化,因为你可能在向量中多次使用相同的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
Run Code Online (Sandbox Code Playgroud)

但是,如果向量中的任何索引相同,则会出现冲突,并且生成的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
Run Code Online (Sandbox Code Playgroud)

在实践中,这个例子效率很低,并且不比标量代码好,但还有其他更多的计算密集型示例,其中使用CD指令似乎是值得的.通常,这些将是模拟,其中数据元素将以非确定性方式更新.一个例子(来自LAMMPS分子动力学模拟器)在Jeffers 等人KNL书中提到.

  • 另请参阅 [Kirill Yukhin 2014 年幻灯片第 50 页](https://gcc.gnu.org/wiki/cauldron2014?action=AttachFile&amp;do=get&amp;target=Cauldron14_AVX-512_Vector_ISA_Kirill_Yukhin_20140711.pdf#page=50)。我发布了一些关于使用/不使用 AVX512CD 加速直方图类型问题的内容 [关于最近的问题](/sf/ask/2748653351/查找)。克隆垃圾箱并在最后进行总计的常用技巧可以帮助避免冲突(甚至对于标量也可以避免存储转发数据依赖性瓶颈)。 (2认同)