【发布时间】:2014-04-26 16:43:34
【问题描述】:
也许我做错了什么,但我知道 SIMD 比标量版本慢。
我只想增加数组的值。我正在使用 Microsoft SIMD(NuGet 包 Microsoft.Bcl.Simd Prerelease)。它是 Beta 版,但它应该可以与 int 和 float 一起正常工作,但事实并非如此。
我的长凳
using System;
using System.Diagnostics;
using System.Linq;
using System.Numerics;
using System.Runtime.CompilerServices;
namespace ConsoleApplication58
{
class Program
{
static void Main()
{
var r = new Random();
var sw = Stopwatch.StartNew();
int[] values = Enumerable.Range(0, 1000000).ToArray();
sw.Stop();
Console.WriteLine("TEST GENERATED IN {0}", sw.Elapsed);
int trash = 0;
Stopwatch sw1 = new Stopwatch(), sw2 = new Stopwatch();
for (int i = 0; i < 100; i++)
{
sw1.Start();
var result = SimdIncrement(values, 10);
sw1.Stop();
sw2.Start();
var result2 = SimpleIncrement(values, 10);
sw2.Stop();
GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();
unchecked
{
trash ^= result[r.Next(values.Length)];
trash ^= result2[r.Next(values.Length)];
}
}
Console.WriteLine("SIMD = {0}", sw1.Elapsed);
Console.WriteLine("Brute = {0}", sw2.Elapsed);
Console.WriteLine("Trash value = {0}", trash);
}
[MethodImpl(MethodImplOptions.NoInlining)]
private static int[] SimpleIncrement(int[] values,int inc)
{
int[] result = new int[values.Length];
for (int i = 0; i < result.Length; i++)
{
result[i] = values[i] + inc;
}
return result;
}
[MethodImpl(MethodImplOptions.NoInlining)]
private static int[] SimdIncrement(int[] values,int inc)
{
int[] result = new int[values.Length];
for (int i = 0; i < values.Length; i += 4)
{
var vector = new Vector<int>(values, i);
var increment = new Vector<int>(inc);
vector += increment;
vector.CopyTo(result, i);
}
return result;
}
}
}
结果:
TEST GENERATED IN 00:00:00.0171804
SIMD = 00:00:02.1456817
Brute = 00:00:00.1576084
Trash value = 548547
Press any key . . .
【问题讨论】:
-
除了其他可能的错误,你应该看看
simd标签的描述;特别是“用于更长的流”和“天真优化的 SIMD 代码更慢”。使用 SIMD(以及一般的并行化)会产生开销;除非您将其用于足够大的数据量,否则可能无法获得补偿。 -
为什么
var increment = new Vector<int>(inc);在循环内? -
VectorMath.IsHardwareAccelerated是否为您返回 true? (不知道你是否正确设置了 RyuJIT?)
标签: c# .net performance sse simd