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

How do the Conflict Detection instructions make it easier to vectorize loops?

A​​VX512CD 指令系列是: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) is referred to in the KNL book by Jeffers et al.