c - 如何并行比较两个以上的数字?

标签 c algorithm parallel-processing sse sse4

是否可以使用 SSE4 在一条指令中比较多于一对数字?

Intel Reference以下是关于 PCMPGTQ 的内容

PCMPGTQ — Compare Packed Data for Greater Than

Performs an SIMD compare for the packed quadwords in the destination operand (first operand) and the source operand (second operand). If the data element in the first (destination) operand is greater than the corresponding element in the second (source) operand, the corresponding data element in the destination is set to all 1s; otherwise, it is set to 0s.

这并不是我真正想要的,因为我希望能够确定 vector 中哪些整数较大,哪些整数较小。

比如我需要比较

32 with 45
13 with 78
44 with 12
99 with 66

我计划将 [32, 13, 44, 99] 放在一个 vector 中,将 [45, 78, 12, 66] 放在另一个 vector 中并比较它们在一条指令中使用 SSE4,并将 [0, 0, 1, 1] 作为结果(0 - 小于,1 - 大于)

但这似乎不是 PCMPGTQ 所做的。关于如何在此级别使用并行性来加速比较,有什么建议吗?

最佳答案

我相信这实际上就是 PCMPGT 运算符家族所做的。后缀指定元素的大小 - B 表示 8 位元素,W 表示 16 位元素,D 表示 32 位元素, Q 用于 64 位元素。因此,如果您想一次比较 4 个 32 位数字,请使用带有 128 位 vector 参数的 PCMPGTD。有关这些操作码的伪代码描述,请参阅 this page

不过,他们不会只写10;他们将全 1 或全 0 写入每个元素,因此使用 PCMPGTB 比较 0x12345678876543210x8765432112345678 应该得到 0x0000FFFFFFFF0000.

This Intel white paper 给出了执行操作 a[i] = (a[i] > b[i]) ? a[i] : b[i](即 a[i] = max(a[i], b[i]))使用 vector 运算。

关于c - 如何并行比较两个以上的数字?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12558729/

相关文章:

c++ - CUDA: vector 列表实现

c - MPI 意外输出

Python 并行子进程命令同时抑制输出

c - C 中的 Printf() 打印 0 以获取额外的格式说明符

c - 循环因条件终止,但条件是必要的?

模拟现场投票的算法

algorithm - 主方法 - 为什么它不能解 T(n) = T(n/2) + n^2/logn?

bash - 在 GNU 并行命令中执行子进程

c - 如何用确定的值初始化数组?

c - 循环无法达到极限