아마 내가 잘못하고있는 것이지만, 그 SIMD가 scallar 버전보다 느리다.SIMD가 무차별적인 힘보다 느린 이유
배열의 값을 증가시키고 싶습니다. Microsoft SIMD (NuGet 패키지 Microsoft.Bcl.Simd 시험판)를 사용하고 있습니다. 그것은 베타 버전이지만 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 (및 병렬화)를 사용하면 오버 헤드가 발생합니다. 충분히 큰 데이터 크기로 사용하지 않으면 보상을받지 못할 수도 있습니다. – SJuan76
왜'var increment = new Vector (inc);'이 루프 안에 있습니까? –
'VectorMath.IsHardwareAccelerated'가 true를 반환합니까? (RyuJIT를 제대로 설정했는지 궁금합니다.) – BrainSlugs83