作者: zyl910
目录
一、背景
先前的2篇文章,说了向量类型的类型选择问题。本文讨论一个使用方面的问题——循环展开。
现在的CPU采用了流水线、超标量等机制来提高运算性能。如果完全是顺序代码,那么流水线的效果会非常好。
但是程序中不可避免的需要 分支 与 循环来处理各种复杂的逻辑。分支与循环会被编译为跳转指令,而跳转指令会导致CPU流水线失效,对性能的影响很大。虽然现代处理器增加了分支预测技术,但总会有预测失败的概率。
尤其是在使用向量类型进行SIMD运算时,因向量类型仅尽可能榨干CPU内部的ALU(算术逻辑单元),于是在跳转时的性能损失更大。
故在使用向量类型处理大规模数学计算时,应尽可能的避免分支与循环。
对于分支,最好尽量将分支挪到内循环外。若是内循环中必须的分支,可尽量用位掩码等办法来写无分支代码。
对于循环,一般可使用循环展开技术,来避免短的循环。
1.1 循环展开简介
摘录——- 循环展开(Loop unrolling)技术是一种提升程序执行速度的非常有效的优化方法,它可以由程序员手工编写,也可由编译器自动优化。循环展开的本质是,利用CPU指令级并行,来降低循环的开销,当然,同时也有利于指令流水线的高效调度。
- ……
- 循环展开的优点:
- 第一,减少了分支预测失败的可能性。
- 第二,增加了循环体内语句并发执行的可能性,当然,这需要循环体内各语句不存在数据相关性。
- 循环展开的缺点:
- 第一,造成代码膨胀,导致ELF文件(或Windows PE文件)尺寸增大。
- 第二,代码可读性显著降低,前一个人写的循环展开代码,很可能被不熟悉的后续维护人员改回去。
复制代码 1.2 测试准备
注意,循环展开提高的是流水线性能,对小循环效果明显。此时分支造成的延时,大多与内循环的运算耗时差不多。
对于有些复杂的大循环,内循环的运算耗时已经很大了,而分支造成的延时仍是常数值,比例下降了很多。此时循环展开的收益就少了。
由于循环展开是程序员手工编写的,故必须在编码前就确定好展开次数。
本文就来探讨一下大多数时候的展开次数选择。
展开2倍的话,性能最多为原来2倍,即大多数情况下只有1倍多的性能提升,提升不大。
展开2倍的话,性能最多为原来4倍。区间大了,很多时候能达到2倍以上的提升。
故一开始可以用展开4倍来测试。下面将进行测试。
测试电脑的配置信息为:lntel(R) Core(TM) i5-8250U CPU @ 1.60GHz、Windows 10。
二、在C#中使用
为了对比测试 Avx指令的效果,故可在 BenchmarkVectorCore30 工程里进行测试。因是64位操作系统,故选取 x64、Release版的测试结果.
2.1 对基础算法做循环展开
回顾一下基础算法:- private static float SumBase(float[] src, int count, int loops) {
- float rt = 0; // Result.
- for (int j=0; j< loops; ++j) {
- for(int i=0; i< count; ++i) {
- rt += src[i];
- }
- }
- return rt;
- }
复制代码 改为循环展开4倍后,代码为:- private static float SumBaseU4(float[] src, int count, int loops) {
- float rt = 0; // Result.
- float rt1 = 0;
- float rt2 = 0;
- float rt3 = 0;
- int nBlockWidth = 4; // Block width.
- int cntBlock = count / nBlockWidth; // Block count.
- int cntRem = count % nBlockWidth; // Remainder count.
- int p; // Index for src data.
- int i;
- for (int j = 0; j < loops; ++j) {
- p = 0;
- // Block processs.
- for (i = 0; i < cntBlock; ++i) {
- rt += src[p];
- rt1 += src[p + 1];
- rt2 += src[p + 2];
- rt3 += src[p + 3];
- p += nBlockWidth;
- }
- // Remainder processs.
- //p = cntBlock * nBlockWidth;
- for (i = 0; i < cntRem; ++i) {
- rt += src[p + i];
- }
- }
- // Reduce.
- rt = rt + rt1 + rt2 + rt3;
- return rt;
- }
复制代码 之前内循环只处理1个数据,现在内循环处理了4个数据。
注意内循环在处理者4个数据时,并不是直接将结果全部累加到 rt 变量,而是使用新增的 rt1、rt2、rt3 变量来临时存储累加值。这是为了消除变量之间的相关性,因为变量之间的相关性会影响流水线性能,故分别使用独立的变量就好了。
最后在 Reduce 阶段,将 rt1、rt2、rt3 的值累加到 rt。
2.1.1 测试结果:
测试结果摘录如下:- SumBase: 6.871948E+10 # msUsed=4938, MFLOPS/s=829.485621709194
- SumBaseU4: 2.748779E+11 # msUsed=1875, MFLOPS/s=2184.5333333333333, scale=2.6336
复制代码 可以发现,基础算法使用4倍循环展开后,性能是原先的 2.6336 倍。
2.2 对 Vector4 版算法做循环展开
回顾一下Vector4 版算法:- private static float SumVector4(float[] src, int count, int loops) {
- float rt = 0; // Result.
- const int VectorWidth = 4;
- int nBlockWidth = VectorWidth; // Block width.
- int cntBlock = count / nBlockWidth; // Block count.
- int cntRem = count % nBlockWidth; // Remainder count.
- Vector4 vrt = Vector4.Zero; // Vector result.
- int p; // Index for src data.
- int i;
- // Load.
- Vector4[] vsrc = new Vector4[cntBlock]; // Vector src.
- p = 0;
- for (i = 0; i < vsrc.Length; ++i) {
- vsrc[i] = new Vector4(src[p], src[p + 1], src[p + 2], src[p + 3]);
- p += VectorWidth;
- }
- // Body.
- for (int j = 0; j < loops; ++j) {
- // Vector processs.
- for (i = 0; i < cntBlock; ++i) {
- // Equivalent to scalar model: rt += src[i];
- vrt += vsrc[i]; // Add.
- }
- // Remainder processs.
- p = cntBlock * nBlockWidth;
- for (i = 0; i < cntRem; ++i) {
- rt += src[p + i];
- }
- }
- // Reduce.
- rt += vrt.X + vrt.Y + vrt.Z + vrt.W;
- return rt;
- }
复制代码 改为循环展开4倍后,代码为:- private static float SumVector4U4(float[] src, int count, int loops) {
- float rt = 0; // Result.
- const int LoopUnrolling = 4;
- const int VectorWidth = 4;
- int nBlockWidth = VectorWidth * LoopUnrolling; // Block width.
- int cntBlock = count / nBlockWidth; // Block count.
- int cntRem = count % nBlockWidth; // Remainder count.
- Vector4 vrt = Vector4.Zero; // Vector result.
- Vector4 vrt1 = Vector4.Zero;
- Vector4 vrt2 = Vector4.Zero;
- Vector4 vrt3 = Vector4.Zero;
- int p; // Index for src data.
- int i;
- // Load.
- Vector4[] vsrc = new Vector4[count / VectorWidth]; // Vector src.
- p = 0;
- for (i = 0; i < vsrc.Length; ++i) {
- vsrc[i] = new Vector4(src[p], src[p + 1], src[p + 2], src[p + 3]);
- p += VectorWidth;
- }
- // Body.
- for (int j = 0; j < loops; ++j) {
- p = 0;
- // Vector processs.
- for (i = 0; i < cntBlock; ++i) {
- vrt += vsrc[p]; // Add.
- vrt1 += vsrc[p + 1];
- vrt2 += vsrc[p + 2];
- vrt3 += vsrc[p + 3];
- p += LoopUnrolling;
- }
- // Remainder processs.
- p = cntBlock * nBlockWidth;
- for (i = 0; i < cntRem; ++i) {
- rt += src[p + i];
- }
- }
- // Reduce.
- vrt = vrt + vrt1 + vrt2 + vrt3;
- rt += vrt.X + vrt.Y + vrt.Z + vrt.W;
- return rt;
- }
复制代码 跟刚才的办法一样,使用新增的 rt1、rt2、rt3 变量来临时存储累加值,消除变量之间的相关性。
最后在 Reduce 阶段,将 vrt1、vrt2、vrt3 的值累加到 vrt。
2.2.1 测试结果:
测试结果摘录如下:- SumBase: 6.871948E+10 # msUsed=4938, MFLOPS/s=829.485621709194
- SumBaseU4: 2.748779E+11 # msUsed=1875, MFLOPS/s=2184.5333333333333, scale=2.6336SumVector4: 2.748779E+11 # msUsed=1218, MFLOPS/s=3362.8899835796387, scale=4.054187192118227SumVector4U4: 1.0995116E+12 # msUsed=532, MFLOPS/s=7699.248120300752, scale=9.281954887218046
复制代码 SumVector4U4对比基础算法(SumBase),性能倍数是 9.281954887218046。
SumVector4U4对比未循环展开的算法(SumVector4),倍数是 9.281954887218046/4.054187192118227=2.2894736842105263092984587836542
2.3 对 Vector 版算法做循环展开
回顾一下 Vector 版算法:- private static float SumVectorT(float[] src, int count, int loops) {
- float rt = 0; // Result.
- int VectorWidth = Vector<float>.Count; // Block width.
- int nBlockWidth = VectorWidth; // Block width.
- int cntBlock = count / nBlockWidth; // Block count.
- int cntRem = count % nBlockWidth; // Remainder count.
- Vector<float> vrt = Vector<float>.Zero; // Vector result.
- int p; // Index for src data.
- int i;
- // Load.
- Vector<float>[] vsrc = new Vector<float>[cntBlock]; // Vector src.
- p = 0;
- for (i = 0; i < vsrc.Length; ++i) {
- vsrc[i] = new Vector<float>(src, p);
- p += VectorWidth;
- }
- // Body.
- for (int j = 0; j < loops; ++j) {
- // Vector processs.
- for (i = 0; i < cntBlock; ++i) {
- vrt += vsrc[i]; // Add.
- }
- // Remainder processs.
- p = cntBlock * nBlockWidth;
- for (i = 0; i < cntRem; ++i) {
- rt += src[p + i];
- }
- }
- // Reduce.
- for (i = 0; i < VectorWidth; ++i) {
- rt += vrt[i];
- }
- return rt;
- }
复制代码 改为循环展开4倍后,代码为:- private static float SumVectorTU4(float[] src, int count, int loops) {
- float rt = 0; // Result.
- const int LoopUnrolling = 4;
- int VectorWidth = Vector<float>.Count; // Block width.
- int nBlockWidth = VectorWidth * LoopUnrolling; // Block width.
- int cntBlock = count / nBlockWidth; // Block count.
- int cntRem = count % nBlockWidth; // Remainder count.
- Vector<float> vrt = Vector<float>.Zero; // Vector result.
- Vector<float> vrt1 = Vector<float>.Zero;
- Vector<float> vrt2 = Vector<float>.Zero;
- Vector<float> vrt3 = Vector<float>.Zero;
- int p; // Index for src data.
- int i;
- // Load.
- Vector<float>[] vsrc = new Vector<float>[count / VectorWidth]; // Vector src.
- p = 0;
- for (i = 0; i < vsrc.Length; ++i) {
- vsrc[i] = new Vector<float>(src, p);
- p += VectorWidth;
- }
- // Body.
- for (int j = 0; j < loops; ++j) {
- p = 0;
- // Vector processs.
- for (i = 0; i < cntBlock; ++i) {
- vrt += vsrc[p]; // Add.
- vrt1 += vsrc[p + 1];
- vrt2 += vsrc[p + 1];
- vrt3 += vsrc[p + 1];
- p += LoopUnrolling;
- }
- // Remainder processs.
- p = cntBlock * nBlockWidth;
- for (i = 0; i < cntRem; ++i) {
- rt += src[p + i];
- }
- }
- // Reduce.
- vrt = vrt + vrt1 + vrt2 + vrt3;
- for (i = 0; i < VectorWidth; ++i) {
- rt += vrt[i];
- }
- return rt;
- }
复制代码 跟刚才的办法一样,使用新增的 rt1、rt2、rt3 变量来临时存储累加值,消除变量之间的相关性。
最后在 Reduce 阶段,将 vrt1、vrt2、vrt3 的值累加到 vrt。
2.3.1 测试结果:
测试结果摘录如下:- SumBase: 6.871948E+10 # msUsed=4938, MFLOPS/s=829.485621709194
- SumBaseU4: 2.748779E+11 # msUsed=1875, MFLOPS/s=2184.5333333333333, scale=2.6336SumVectorT: 5.497558E+11 # msUsed=609, MFLOPS/s=6725.7799671592775, scale=8.108374384236454SumVectorTU4: 2.1990233E+12 # msUsed=203, MFLOPS/s=20177.339901477833, scale=24.32512315270936
复制代码 SumVectorTU4对比基础算法(SumBase),性能倍数是 24.32512315270936。
SumVectorTU4对比未循环展开的算法(SumVectorT),倍数是 24.32512315270936/8.108374384236454=2.9999999999999997533414337788579
初步发现 Vector循环展开(2.9999)带来的性能提升, 比VectorT循环展开(2.2894)更高一些。
2.4 对 Avx版算法做循环展开
先前分别尝试用 数组、Span、指针 的办法来操纵数据、使用Avx指令集。现在对这3种办法,均写一套循环展开4次的代码:- /// <summary>
- /// Sum - Vector AVX.
- /// </summary>
- /// <param name="src">Soure array.</param>
- /// <param name="count">Soure array count.</param>
- /// <param name="loops">Benchmark loops.</param>
- /// <returns>Return the sum value.</returns>
- private static float SumVectorAvx(float[] src, int count, int loops) {
- #if Allow_Intrinsics
- float rt = 0; // Result.
- //int VectorWidth = 32 / 4; // sizeof(__m256) / sizeof(float);
- int VectorWidth = Vector256<float>.Count; // Block width.
- int nBlockWidth = VectorWidth; // Block width.
- int cntBlock = count / nBlockWidth; // Block count.
- int cntRem = count % nBlockWidth; // Remainder count.
- Vector256<float> vrt = Vector256<float>.Zero; // Vector result.
- int p; // Index for src data.
- int i;
- // Load.
- Vector256<float>[] vsrc = new Vector256<float>[cntBlock]; // Vector src.
- p = 0;
- for (i = 0; i < cntBlock; ++i) {
- vsrc[i] = Vector256.Create(src[p], src[p + 1], src[p + 2], src[p + 3], src[p + 4], src[p + 5], src[p + 6], src[p + 7]); // Load.
- p += VectorWidth;
- }
- // Body.
- for (int j = 0; j < loops; ++j) {
- // Vector processs.
- for (i = 0; i < cntBlock; ++i) {
- vrt = Avx.Add(vrt, vsrc[i]); // Add. vrt += vsrc[i];
- }
- // Remainder processs.
- p = cntBlock * nBlockWidth;
- for (i = 0; i < cntRem; ++i) {
- rt += src[p + i];
- }
- }
- // Reduce.
- for (i = 0; i < VectorWidth; ++i) {
- rt += vrt.GetElement(i);
- }
- return rt;
- #else
- throw new NotSupportedException();
- #endif
- }
- /// <summary>
- /// Sum - Vector AVX - Loop unrolling *4.
- /// </summary>
- /// <param name="src">Soure array.</param>
- /// <param name="count">Soure array count.</param>
- /// <param name="loops">Benchmark loops.</param>
- /// <returns>Return the sum value.</returns>
- private static float SumVectorAvxU4(float[] src, int count, int loops) {
- #if Allow_Intrinsics
- float rt = 0; // Result.
- const int LoopUnrolling = 4;
- int VectorWidth = Vector256<float>.Count; // Block width.
- int nBlockWidth = VectorWidth * LoopUnrolling; // Block width.
- int cntBlock = count / nBlockWidth; // Block count.
- int cntRem = count % nBlockWidth; // Remainder count.
- Vector256<float> vrt = Vector256<float>.Zero; // Vector result.
- Vector256<float> vrt1 = Vector256<float>.Zero;
- Vector256<float> vrt2 = Vector256<float>.Zero;
- Vector256<float> vrt3 = Vector256<float>.Zero;
- int p; // Index for src data.
- int i;
- // Load.
- Vector256<float>[] vsrc = new Vector256<float>[count / VectorWidth]; // Vector src.
- p = 0;
- for (i = 0; i < vsrc.Length; ++i) {
- vsrc[i] = Vector256.Create(src[p], src[p + 1], src[p + 2], src[p + 3], src[p + 4], src[p + 5], src[p + 6], src[p + 7]); // Load.
- p += VectorWidth;
- }
- // Body.
- for (int j = 0; j < loops; ++j) {
- p = 0;
- // Vector processs.
- for (i = 0; i < cntBlock; ++i) {
- vrt = Avx.Add(vrt, vsrc[p]); // Add. vrt += vsrc[p];
- vrt1 = Avx.Add(vrt1, vsrc[p + 1]);
- vrt2 = Avx.Add(vrt2, vsrc[p + 2]);
- vrt3 = Avx.Add(vrt3, vsrc[p + 3]);
- p += LoopUnrolling;
- }
- // Remainder processs.
- p = cntBlock * nBlockWidth;
- for (i = 0; i < cntRem; ++i) {
- rt += src[p + i];
- }
- }
- // Reduce.
- vrt = Avx.Add(Avx.Add(vrt, vrt1), Avx.Add(vrt2, vrt3)); // vrt = vrt + vrt1 + vrt2 + vrt3;
- for (i = 0; i < VectorWidth; ++i) {
- rt += vrt.GetElement(i);
- }
- return rt;
- #else
- throw new NotSupportedException();
- #endif
- }
- /// <summary>
- /// Sum - Vector AVX - Span.
- /// </summary>
- /// <param name="src">Soure array.</param>
- /// <param name="count">Soure array count.</param>
- /// <param name="loops">Benchmark loops.</param>
- /// <returns>Return the sum value.</returns>
- private static float SumVectorAvxSpan(float[] src, int count, int loops) {
- #if Allow_Intrinsics
- float rt = 0; // Result.
- int VectorWidth = Vector256<float>.Count; // Block width.
- int nBlockWidth = VectorWidth; // Block width.
- int cntBlock = count / nBlockWidth; // Block count.
- int cntRem = count % nBlockWidth; // Remainder count.
- Vector256<float> vrt = Vector256<float>.Zero; // Vector result.
- int p; // Index for src data.
- ReadOnlySpan<Vector256<float>> vsrc; // Vector src.
- int i;
- // Body.
- for (int j = 0; j < loops; ++j) {
- // Vector processs.
- vsrc = System.Runtime.InteropServices.MemoryMarshal.Cast<float, Vector256<float> >(new Span<float>(src)); // Reinterpret cast. `float*` to `Vector256<float>*`.
- for (i = 0; i < cntBlock; ++i) {
- vrt = Avx.Add(vrt, vsrc[i]); // Add. vrt += vsrc[i];
- }
- // Remainder processs.
- p = cntBlock * nBlockWidth;
- for (i = 0; i < cntRem; ++i) {
- rt += src[p + i];
- }
- }
- // Reduce.
- for (i = 0; i < VectorWidth; ++i) {
- rt += vrt.GetElement(i);
- }
- return rt;
- #else
- throw new NotSupportedException();
- #endif
- }
- /// <summary>
- /// Sum - Vector AVX - Span - Loop unrolling *4.
- /// </summary>
- /// <param name="src">Soure array.</param>
- /// <param name="count">Soure array count.</param>
- /// <param name="loops">Benchmark loops.</param>
- /// <returns>Return the sum value.</returns>
- private static float SumVectorAvxSpanU4(float[] src, int count, int loops) {
- #if Allow_Intrinsics
- float rt = 0; // Result.
- const int LoopUnrolling = 4;
- int VectorWidth = Vector256<float>.Count; // Block width.
- int nBlockWidth = VectorWidth * LoopUnrolling; // Block width.
- int cntBlock = count / nBlockWidth; // Block count.
- int cntRem = count % nBlockWidth; // Remainder count.
- Vector256<float> vrt = Vector256<float>.Zero; // Vector result.
- Vector256<float> vrt1 = Vector256<float>.Zero;
- Vector256<float> vrt2 = Vector256<float>.Zero;
- Vector256<float> vrt3 = Vector256<float>.Zero;
- int p; // Index for src data.
- ReadOnlySpan<Vector256<float>> vsrc; // Vector src.
- int i;
- // Body.
- for (int j = 0; j < loops; ++j) {
- p = 0;
- // Vector processs.
- vsrc = System.Runtime.InteropServices.MemoryMarshal.Cast<float, Vector256<float>>(new Span<float>(src)); // Reinterpret cast. `float*` to `Vector256<float>*`.
- for (i = 0; i < cntBlock; ++i) {
- vrt = Avx.Add(vrt, vsrc[p]); // Add. vrt += vsrc[p];
- vrt1 = Avx.Add(vrt1, vsrc[p + 1]);
- vrt2 = Avx.Add(vrt2, vsrc[p + 2]);
- vrt3 = Avx.Add(vrt3, vsrc[p + 3]);
- p += LoopUnrolling;
- }
- // Remainder processs.
- p = cntBlock * nBlockWidth;
- for (i = 0; i < cntRem; ++i) {
- rt += src[p + i];
- }
- }
- // Reduce.
- vrt = Avx.Add(Avx.Add(vrt, vrt1), Avx.Add(vrt2, vrt3)); // vrt = vrt + vrt1 + vrt2 + vrt3;
- for (i = 0; i < VectorWidth; ++i) {
- rt += vrt.GetElement(i);
- }
- return rt;
- #else
- throw new NotSupportedException();
- #endif
- }
- /// <summary>
- /// Sum - Vector AVX - Ptr.
- /// </summary>
- /// <param name="src">Soure array.</param>
- /// <param name="count">Soure array count.</param>
- /// <param name="loops">Benchmark loops.</param>
- /// <returns>Return the sum value.</returns>
- private static float SumVectorAvxPtr(float[] src, int count, int loops) {
- #if Allow_Intrinsics && UNSAFE
- unsafe {
- float rt = 0; // Result.
- int VectorWidth = Vector256<float>.Count; // Block width.
- int nBlockWidth = VectorWidth; // Block width.
- int cntBlock = count / nBlockWidth; // Block count.
- int cntRem = count % nBlockWidth; // Remainder count.
- Vector256<float> vrt = Vector256<float>.Zero; // Vector result.
- Vector256<float> vload;
- float* p; // Pointer for src data.
- int i;
- // Body.
- fixed(float* p0 = &src[0]) {
- for (int j = 0; j < loops; ++j) {
- p = p0;
- // Vector processs.
- for (i = 0; i < cntBlock; ++i) {
- vload = Avx.LoadVector256(p); // Load. vload = *(*__m256)p;
- vrt = Avx.Add(vrt, vload); // Add. vrt += vsrc[i];
- p += nBlockWidth;
- }
- // Remainder processs.
- for (i = 0; i < cntRem; ++i) {
- rt += p[i];
- }
- }
- }
- // Reduce.
- for (i = 0; i < VectorWidth; ++i) {
- rt += vrt.GetElement(i);
- }
- return rt;
- }
- #else
- throw new NotSupportedException();
- #endif
- }
- /// <summary>
- /// Sum - Vector AVX - Ptr - Loop unrolling *4.
- /// </summary>
- /// <param name="src">Soure array.</param>
- /// <param name="count">Soure array count.</param>
- /// <param name="loops">Benchmark loops.</param>
- /// <returns>Return the sum value.</returns>
- private static float SumVectorAvxPtrU4(float[] src, int count, int loops) {
- #if Allow_Intrinsics && UNSAFE
- unsafe {
- float rt = 0; // Result.
- const int LoopUnrolling = 4;
- int VectorWidth = Vector256<float>.Count; // Block width.
- int nBlockWidth = VectorWidth * LoopUnrolling; // Block width.
- int cntBlock = count / nBlockWidth; // Block count.
- int cntRem = count % nBlockWidth; // Remainder count.
- Vector256<float> vrt = Vector256<float>.Zero; // Vector result.
- Vector256<float> vrt1 = Vector256<float>.Zero;
- Vector256<float> vrt2 = Vector256<float>.Zero;
- Vector256<float> vrt3 = Vector256<float>.Zero;
- Vector256<float> vload;
- Vector256<float> vload1;
- Vector256<float> vload2;
- Vector256<float> vload3;
- float* p; // Pointer for src data.
- int i;
- // Body.
- fixed (float* p0 = &src[0]) {
- for (int j = 0; j < loops; ++j) {
- p = p0;
- // Vector processs.
- for (i = 0; i < cntBlock; ++i) {
- vload = Avx.LoadVector256(p); // Load. vload = *(*__m256)p;
- vload1 = Avx.LoadVector256(p + VectorWidth * 1);
- vload2 = Avx.LoadVector256(p + VectorWidth * 2);
- vload3 = Avx.LoadVector256(p + VectorWidth * 3);
- vrt = Avx.Add(vrt, vload); // Add. vrt += vsrc[i];
- vrt1 = Avx.Add(vrt1, vload1);
- vrt2 = Avx.Add(vrt2, vload2);
- vrt3 = Avx.Add(vrt3, vload3);
- p += nBlockWidth;
- }
- // Remainder processs.
- for (i = 0; i < cntRem; ++i) {
- rt += p[i];
- }
- }
- }
- // Reduce.
- vrt = Avx.Add(Avx.Add(vrt, vrt1), Avx.Add(vrt2, vrt3)); // vrt = vrt + vrt1 + vrt2 + vrt3;
- for (i = 0; i < VectorWidth; ++i) {
- rt += vrt.GetElement(i);
- }
- return rt;
- }
- #else
- throw new NotSupportedException();
- #endif
- }
复制代码 2.4.1 测试结果:
测试结果摘录如下:- SumBase: 6.871948E+10 # msUsed=4938, MFLOPS/s=829.485621709194
- SumBaseU4: 2.748779E+11 # msUsed=1875, MFLOPS/s=2184.5333333333333, scale=2.6336SumVectorAvx: 5.497558E+11 # msUsed=609, MFLOPS/s=6725.7799671592775, scale=8.108374384236454SumVectorAvxSpan: 5.497558E+11 # msUsed=625, MFLOPS/s=6553.6, scale=7.9008SumVectorAvxPtr: 5.497558E+11 # msUsed=610, MFLOPS/s=6714.754098360656, scale=8.095081967213115SumVectorAvxU4: 2.1990233E+12 # msUsed=328, MFLOPS/s=12487.80487804878, scale=15.054878048780488SumVectorAvxSpanU4: 2.1990233E+12 # msUsed=312, MFLOPS/s=13128.205128205129, scale=15.826923076923078SumVectorAvxPtrU4: 2.1990233E+12 # msUsed=157, MFLOPS/s=26089.171974522294, scale=31.452229299363058
复制代码 未做循环展开时,这3钟办法的性能拉不开差距,都是8倍左右。
而现在用了循环展开后,数组版(SumVectorAvxU4)、Span版(SumVectorAvxSpanU4)只有15倍左右的性能提升。而指针版有 31倍性能提升,是 数组版、Span版 的2倍。
可能是因为指针更贴近底层硬件、更易于编译器优化。故当使用内在函数时,推荐优先使用指针。
SumVectorAvxPtrU4 对比基础算法(SumBase),性能倍数是 31.452229299363058。
SumVectorAvxPtrU4 对比未循环展开的算法(SumVectorAvxPtr),倍数是 31.452229299363058/8.095081967213115=3.8853503184713375449974589366746。
2.5 对 Avx版算法做循环展开16次
刚才尝试了4倍循环展开,故理论上限是4倍。而SumVectorAvxPtrU4版有约 3.8853 倍性能提升,故可考虑进一步加大,于是可测试一下 4*4=16 次的循环展开。
将 SumVectorAvxPtr 改造为循环展开16次的,代码如下:- private static float SumVectorAvxPtrU16(float[] src, int count, int loops) {
- #if Allow_Intrinsics && UNSAFE
- unsafe {
- float rt = 0; // Result.
- const int LoopUnrolling = 16;
- int VectorWidth = Vector256<float>.Count; // Block width.
- int nBlockWidth = VectorWidth * LoopUnrolling; // Block width.
- int cntBlock = count / nBlockWidth; // Block count.
- int cntRem = count % nBlockWidth; // Remainder count.
- Vector256<float> vrt = Vector256<float>.Zero; // Vector result.
- Vector256<float> vrt1 = Vector256<float>.Zero;
- Vector256<float> vrt2 = Vector256<float>.Zero;
- Vector256<float> vrt3 = Vector256<float>.Zero;
- Vector256<float> vrt4 = Vector256<float>.Zero;
- Vector256<float> vrt5 = Vector256<float>.Zero;
- Vector256<float> vrt6 = Vector256<float>.Zero;
- Vector256<float> vrt7 = Vector256<float>.Zero;
- Vector256<float> vrt8 = Vector256<float>.Zero;
- Vector256<float> vrt9 = Vector256<float>.Zero;
- Vector256<float> vrt10 = Vector256<float>.Zero;
- Vector256<float> vrt11 = Vector256<float>.Zero;
- Vector256<float> vrt12 = Vector256<float>.Zero;
- Vector256<float> vrt13 = Vector256<float>.Zero;
- Vector256<float> vrt14 = Vector256<float>.Zero;
- Vector256<float> vrt15 = Vector256<float>.Zero;
- float* p; // Pointer for src data.
- int i;
- // Body.
- fixed (float* p0 = &src[0]) {
- for (int j = 0; j < loops; ++j) {
- p = p0;
- // Vector processs.
- for (i = 0; i < cntBlock; ++i) {
- //vload = Avx.LoadVector256(p); // Load. vload = *(*__m256)p;
- vrt = Avx.Add(vrt, Avx.LoadVector256(p)); // Add. vrt[k] += *((*__m256)(p)+k);
- vrt1 = Avx.Add(vrt1, Avx.LoadVector256(p + VectorWidth * 1));
- vrt2 = Avx.Add(vrt2, Avx.LoadVector256(p + VectorWidth * 2));
- vrt3 = Avx.Add(vrt3, Avx.LoadVector256(p + VectorWidth * 3));
- vrt4 = Avx.Add(vrt4, Avx.LoadVector256(p + VectorWidth * 4));
- vrt5 = Avx.Add(vrt5, Avx.LoadVector256(p + VectorWidth * 5));
- vrt6 = Avx.Add(vrt6, Avx.LoadVector256(p + VectorWidth * 6));
- vrt7 = Avx.Add(vrt7, Avx.LoadVector256(p + VectorWidth * 7));
- vrt8 = Avx.Add(vrt8, Avx.LoadVector256(p + VectorWidth * 8));
- vrt9 = Avx.Add(vrt9, Avx.LoadVector256(p + VectorWidth * 9));
- vrt10 = Avx.Add(vrt10, Avx.LoadVector256(p + VectorWidth * 10));
- vrt11 = Avx.Add(vrt11, Avx.LoadVector256(p + VectorWidth * 11));
- vrt12 = Avx.Add(vrt12, Avx.LoadVector256(p + VectorWidth * 12));
- vrt13 = Avx.Add(vrt13, Avx.LoadVector256(p + VectorWidth * 13));
- vrt14 = Avx.Add(vrt14, Avx.LoadVector256(p + VectorWidth * 14));
- vrt15 = Avx.Add(vrt15, Avx.LoadVector256(p + VectorWidth * 15));
- p += nBlockWidth;
- }
- // Remainder processs.
- for (i = 0; i < cntRem; ++i) {
- rt += p[i];
- }
- }
- }
- // Reduce.
- vrt = Avx.Add( Avx.Add( Avx.Add(Avx.Add(vrt, vrt1), Avx.Add(vrt2, vrt3))
- , Avx.Add(Avx.Add(vrt4, vrt5), Avx.Add(vrt6, vrt7)) )
- , Avx.Add( Avx.Add(Avx.Add(vrt8, vrt9), Avx.Add(vrt10, vrt11))
- , Avx.Add(Avx.Add(vrt12, vrt13), Avx.Add(vrt14, vrt15)) ) )
- ; // vrt = vrt + vrt1 + vrt2 + vrt3 + ... vrt15;
- for (i = 0; i < VectorWidth; ++i) {
- rt += vrt.GetElement(i);
- }
- return rt;
- }
- #else
- throw new NotSupportedException();
- #endif
- }
复制代码 2.5.1 测试结果:
测试结果摘录如下:- SumBase: 6.871948E+10 # msUsed=4938, MFLOPS/s=829.485621709194
- SumBaseU4: 2.748779E+11 # msUsed=1875, MFLOPS/s=2184.5333333333333, scale=2.6336SumVectorAvxPtr: 5.497558E+11 # msUsed=610, MFLOPS/s=6714.754098360656, scale=8.095081967213115SumVectorAvxPtrU4: 2.1990233E+12 # msUsed=157, MFLOPS/s=26089.171974522294, scale=31.452229299363058SumVectorAvxPtrU16: 8.386202E+12 # msUsed=125, MFLOPS/s=32768, scale=39.504
复制代码 SumVectorAvxPtrU16 对比基础算法(SumBase),性能倍数是 39.504。
SumVectorAvxPtrU16 对比未循环展开的算法(SumVectorAvxPtr),倍数是 39.504/8.095081967213115=4.8799999999999998517618469015796。
SumVectorAvxPtrU16 对比循环展开4次的算法(SumVectorAvxPtrU4),倍数是 39.504/31.452229299363058=1.2559999999999999730384771162414。
从循环展开4次,改为循环展开16次,性能倍数只是从 31倍多,提升到 39 倍多,仅提升 25% 左右。
性能提升的少,但编码麻烦多了。看来循环展开16次的性价比很低,故一般情况下用循环展开4次就行了。
2.6 尝试用数组来存储循环展开的临时变量
使用循环展开N次时,将会导致临时变量数量是非循环展开版的N倍。例如刚才的 SumVectorAvxPtrU16 函数,因循环展开16次,导致临时变量是非循环展开版的16倍,写起了很啰嗦。
这些变量的类型是一样的,放到数组中的话,代码会清晰不少,但会不会影响性能呢?
于是做了一个测试,代码如下:- private static float SumVectorAvxPtrU16A(float[] src, int count, int loops) {
- #if Allow_Intrinsics && UNSAFE
- unsafe {
- float rt = 0; // Result.
- const int LoopUnrolling = 16;
- int VectorWidth = Vector256<float>.Count; // Block width.
- int nBlockWidth = VectorWidth * LoopUnrolling; // Block width.
- int cntBlock = count / nBlockWidth; // Block count.
- int cntRem = count % nBlockWidth; // Remainder count.
- int i;
- //Vector256<float>[] vrt = new Vector256<float>[LoopUnrolling]; // Vector result.
- Vector256<float>* vrt = stackalloc Vector256<float>[LoopUnrolling]; ; // Vector result.
- for (i = 0; i< LoopUnrolling; ++i) {
- vrt[i] = Vector256<float>.Zero;
- }
- float* p; // Pointer for src data.
- // Body.
- fixed (float* p0 = &src[0]) {
- for (int j = 0; j < loops; ++j) {
- p = p0;
- // Vector processs.
- for (i = 0; i < cntBlock; ++i) {
- //vload = Avx.LoadVector256(p); // Load. vload = *(*__m256)p;
- vrt[0] = Avx.Add(vrt[0], Avx.LoadVector256(p)); // Add. vrt[k] += *((*__m256)(p)+k);
- vrt[1] = Avx.Add(vrt[1], Avx.LoadVector256(p + VectorWidth * 1));
- vrt[2] = Avx.Add(vrt[2], Avx.LoadVector256(p + VectorWidth * 2));
- vrt[3] = Avx.Add(vrt[3], Avx.LoadVector256(p + VectorWidth * 3));
- vrt[4] = Avx.Add(vrt[4], Avx.LoadVector256(p + VectorWidth * 4));
- vrt[5] = Avx.Add(vrt[5], Avx.LoadVector256(p + VectorWidth * 5));
- vrt[6] = Avx.Add(vrt[6], Avx.LoadVector256(p + VectorWidth * 6));
- vrt[7] = Avx.Add(vrt[7], Avx.LoadVector256(p + VectorWidth * 7));
- vrt[8] = Avx.Add(vrt[8], Avx.LoadVector256(p + VectorWidth * 8));
- vrt[9] = Avx.Add(vrt[9], Avx.LoadVector256(p + VectorWidth * 9));
- vrt[10] = Avx.Add(vrt[10], Avx.LoadVector256(p + VectorWidth * 10));
- vrt[11] = Avx.Add(vrt[11], Avx.LoadVector256(p + VectorWidth * 11));
- vrt[12] = Avx.Add(vrt[12], Avx.LoadVector256(p + VectorWidth * 12));
- vrt[13] = Avx.Add(vrt[13], Avx.LoadVector256(p + VectorWidth * 13));
- vrt[14] = Avx.Add(vrt[14], Avx.LoadVector256(p + VectorWidth * 14));
- vrt[15] = Avx.Add(vrt[15], Avx.LoadVector256(p + VectorWidth * 15));
- p += nBlockWidth;
- }
- // Remainder processs.
- for (i = 0; i < cntRem; ++i) {
- rt += p[i];
- }
- }
- }
- // Reduce.
- for (i = 1; i < LoopUnrolling; ++i) {
- vrt[0] = Avx.Add(vrt[0], vrt[i]); // vrt[0] += vrt[i]
- }
- for (i = 0; i < VectorWidth; ++i) {
- rt += vrt[0].GetElement(i);
- }
- return rt;
- }
- #else
- throw new NotSupportedException();
- #endif
- }
复制代码 2.6.1 测试结果:
测试结果摘录如下:- SumBase: 6.871948E+10 # msUsed=4938, MFLOPS/s=829.485621709194
- SumBaseU4: 2.748779E+11 # msUsed=1875, MFLOPS/s=2184.5333333333333, scale=2.6336SumVectorAvxPtr: 5.497558E+11 # msUsed=610, MFLOPS/s=6714.754098360656, scale=8.095081967213115SumVectorAvxPtrU4: 2.1990233E+12 # msUsed=157, MFLOPS/s=26089.171974522294, scale=31.452229299363058SumVectorAvxPtrU16: 8.386202E+12 # msUsed=125, MFLOPS/s=32768, scale=39.504SumVectorAvxPtrU16A: 8.3862026E+12 # msUsed=187, MFLOPS/s=21903.74331550802, scale=26.406417112299465
复制代码 可以发现 SumVectorAvxPtrU16A 的性能比 SumVectorAvxPtrU16 差。
曾经以为是因为数组是在堆中分配的(new Vector256)引起的,有堆内存分配的开销,且需要多次寻址才能定位变量。
随后改为栈中分配的数组(stackalloc Vector256),且用最贴近硬件的指针来操作,可性能几乎一致。故猜测可能是编译优化时难以将它们优化为寄存器变量。
故在使用循环展开时,临时变量不要用数组来存,还是逐个定义局部变量比较好。
2.7 尝试用栈数组来减少相关性
还尝试了用栈数组来减少相关性,代码如下:
[code]private static float SumVectorAvxPtrUX(float[] src, int count, int loops, int LoopUnrolling) {#if Allow_Intrinsics && UNSAFE unsafe { float rt = 0; // Result. //const int LoopUnrolling = 16; if (LoopUnrolling |