我受到此链接 https://www.sigarch.org/simd-instructions-considered-harmful/的启发,研究了 AVX512 的性能。我的想法是循环之后的清理循环可以使用 AVX512 掩码操作删除。
这是我正在使用的代码
void daxpy2(int n, double a, const double x[], double y[]) {
__m512d av = _mm512_set1_pd(a);
int r = n&7, n2 = n - r;
for(int i=-n2; i<0; i+=8) {
__m512d yv = _mm512_loadu_pd(&y[i+n2]);
__m512d xv = _mm512_loadu_pd(&x[i+n2]);
yv = _mm512_fmadd_pd(av, xv, yv);
_mm512_storeu_pd(&y[i+n2], yv);
}
__m512d yv = _mm512_loadu_pd(&y[n2]);
__m512d xv = _mm512_loadu_pd(&x[n2]);
yv = _mm512_fmadd_pd(av, xv, yv);
__mmask8 mask = (1 << r) -1;
//__mmask8 mask = _bextr_u32(-1, 0, r);
_mm512_mask_storeu_pd(&y[n2], mask, yv);
}
我认为使用 BMI1 和/或 BMI2 指令可以生成指令更少的掩码。然而,
__mmask8 mask = _bextr_u32(-1, 0, r)
(在指令数量上)并不比
__mmask8 mask = (1 << r) -1;
请参阅https://godbolt.org/z/BFQCM3和https://godbolt.org/z/tesmB_。
这似乎是因为 _bextr_u32 无论如何都会移动 8 位。
可以使用更少的指令(例如使用 BMI 或其他方法)或更优化地生成掩码吗?
我用我的 AVX512 结果扩充了链接中的表格。
ISA | MIPS-32 | AVX2 | RV32V | AVX512 |
******************************|*********|****** |*******|******* |
Instructions(static) | 22 | 29 | 13 | 28 |
Instructions per Main Loop | 7 | 6* | 10 | 5*|
Bookkeeping Instructions | 15 | 23 | 3 | 23 |
Results per Main Loop | 2 | 4 | 64 | 8 |
Instructions (dynamic n=1000) | 3511 | 1517**| 163 | 645 |
*macro-op fusion will reduce the number of uops in the main loop by 1
** without the unnecessary cmp instructions it would only be 1250+ instructions.
我认为,如果链接的作者从-n
up to0
而不是 from 0
to计数,n
他们可能会cmp
像我在主循环中那样跳过指令(参见下面的程序集),所以对于 AVX,它应该是主循环中的 5 条指令。
这是ICC19的程序集和-O3 -xCOMMON-AVX512
daxpy2(int, double, double const*, double*):
mov eax, edi #6.13
and eax, 7 #6.13
movsxd r9, edi #6.25
sub r9, rax #6.21
mov ecx, r9d #7.14
neg ecx #7.14
movsxd rcx, ecx #7.14
vbroadcastsd zmm16, xmm0 #5.16
lea rdi, QWORD PTR [rsi+r9*8] #9.35
lea r8, QWORD PTR [rdx+r9*8] #8.35
test rcx, rcx #7.20
jge ..B1.5 # Prob 36% #7.20
..B1.3: # Preds ..B1.1 ..B1.3
vmovups zmm17, ZMMWORD PTR [rdi+rcx*8] #10.10
vfmadd213pd zmm17, zmm16, ZMMWORD PTR [r8+rcx*8] #10.10
vmovups ZMMWORD PTR [r8+rcx*8], zmm17 #11.23
add rcx, 8 #7.23
js ..B1.3 # Prob 82% #7.20
..B1.5: # Preds ..B1.3 ..B1.1
vmovups zmm17, ZMMWORD PTR [rsi+r9*8] #15.8
vfmadd213pd zmm16, zmm17, ZMMWORD PTR [rdx+r9*8] #15.8
mov edx, -1 #17.19
shl eax, 8 #17.19
bextr eax, edx, eax #17.19
kmovw k1, eax #18.3
vmovupd ZMMWORD PTR [r8]{k1}, zmm16 #18.3
vzeroupper #19.1
ret #19.1
在哪里
add r8, 8
js ..B1.3
宏操作应该融合到一条指令。但是,正如 Peter Cordes在此答案 中指出的js cannot fuse。编译器本可以生成jl
,而不是融合。
我使用 Agner Fog 的testp实用程序来获取核心时钟(不是参考时钟)、指令、微指令。我为 SSE2(实际上是带有 FMA 但带有 128 位向量的 AVX2)、AVX2 和 AVX512 执行了此操作,用于三种不同的循环变体
v1 = for(int64_t i=0; i<n; i+=vec_size) // generates cmp instruction
v2 = for(int64_t i=-n2; i<0; i+=vec_size) // no cmp but uses js
v3 = for(int64_t i=-n2; i!=0; i+=vec_size) // no cmp and uses jne
vec_size = 2 for SSE, 4 for AVX2, and 8 for AVX512
vec_size version core cycle instructions uops
2 v1 895 3014 3524
2 v2 900 2518 3535
2 v3 870 2518 3035
4 v1 527 1513 1777
4 v2 520 1270 1777
4 v3 517 1270 1541
8 v1 285 765 910
8 v2 285 645 910
8 v3 285 645 790
请注意,核心时钟实际上并不是循环版本的函数。它仅取决于循环的迭代。它与 成正比2*n/vec_size
。
SSE 2*1000/2=1000
AVX2 2*1000/4=500
AVX512 2*1000/8=250
指令的数量确实从 v1 变为 v2,但在 v2 和 v3 之间没有变化。6*n/vec_size
对于 v1,它与 v2 和 v3成正比5*n/vec_size
最后,v1 和 v2 的微指令数量或多或少相同,但 v3 的微指令数量有所下降。7*n/vec_size
对于 v1 和 v2 它与 v3成正比6*n/vec_size
。
这是 IACA3 对于 vec_size=2 的结果
Throughput Analysis Report
--------------------------
Block Throughput: 1.49 Cycles Throughput Bottleneck: FrontEnd
Loop Count: 50
Port Binding In Cycles Per Iteration:
--------------------------------------------------------------------------------------------------
| Port | 0 - DV | 1 | 2 - D | 3 - D | 4 | 5 | 6 | 7 |
--------------------------------------------------------------------------------------------------
| Cycles | 0.5 0.0 | 0.5 | 1.5 1.0 | 1.5 1.0 | 1.0 | 0.0 | 0.0 | 0.0 |
--------------------------------------------------------------------------------------------------
DV - Divider pipe (on port 0)
D - Data fetch pipe (on ports 2 and 3)
F - Macro Fusion with the previous instruction occurred
* - instruction micro-ops not bound to a port
^ - Micro Fusion occurred
# - ESP Tracking sync uop was issued
@ - SSE instruction followed an AVX256/AVX512 instruction, dozens of cycles penalty is expected
X - instruction not supported, was not accounted in Analysis
| Num Of | Ports pressure in cycles | |
| Uops | 0 - DV | 1 | 2 - D | 3 - D | 4 | 5 | 6 | 7 |
-----------------------------------------------------------------------------------------
| 1 | | | 0.5 0.5 | 0.5 0.5 | | | | | vmovupd xmm1, xmmword ptr [r8+rax*8]
| 2 | 0.5 | 0.5 | 0.5 0.5 | 0.5 0.5 | | | | | vfmadd213pd xmm1, xmm2, xmmword ptr [rcx+rax*8]
| 2 | | | 0.5 | 0.5 | 1.0 | | | | vmovups xmmword ptr [rcx+rax*8], xmm1
| 1* | | | | | | | | | add rax, 0x2
| 0*F | | | | | | | | | js 0xffffffffffffffe3
Total Num Of Uops: 6
IACA 声称js
宏融合add
不同意 Agner 和testp
实用程序的性能计数器。见上文,v27*n/vec_size
与 v3 成正比6*n/vec_size
,我推断这意味着js
不会进行宏融合。
我认为除了指令数量之外,链接的作者还应该考虑核心周期,也许还有微指令。