我目前正在尝试仅使用 C# 计算一个巨大数组中所有值的总和,并使用 SIMD 来比较性能,而 SIMD 版本要慢得多。请查看下面的代码片段,如果我遗漏了什么,请告诉我。“vals”是从图像文件中读取的巨大数组,并省略了该部分以保持精简。
var watch1 = new Stopwatch();
watch1.Start();
var total = vals.Aggregate(0, (a, i) => a + i);
watch1.Stop();
Console.WriteLine(string.Format("Total is: {0}", total));
Console.WriteLine(string.Format("Time taken: {0}", watch1.ElapsedMilliseconds));
var watch2 = new Stopwatch();
watch2.Start();
var sTotal = GetSIMDVectors(vals).Aggregate((a, i) => a + i);
int sum = 0;
for (int i = 0; i < Vector<int>.Count; i++)
sum += sTotal[i];
watch2.Stop();
Console.WriteLine(string.Format("Another Total is: {0}", sum));
Console.WriteLine(string.Format("Time taken: {0}", watch2.ElapsedMilliseconds));
和 GetSIMDVectors 方法
private static IEnumerable<Vector<int>> GetSIMDVectors(short[] source)
{
int vecCount = Vector<int>.Count;
int i = 0;
int len = source.Length;
for(i = 0; i + vecCount < len; i = i + vecCount)
{
var items = new int[vecCount];
for (int k = 0; k < vecCount; k++)
{
items[k] = source[i + k];
}
yield return new Vector<int>(items);
}
var remaining = new int[vecCount];
for (int j = i, k =0; j < len; j++, k++)
{
remaining[k] = source[j];
}
yield return new Vector<int>(remaining);
}