c# - System.Numerics.Vector.NET Framework 上的初始化性能
问题描述
System.Numerics.Vector 为 .NET Core 和 .NET Framework 带来了 SIMD 支持。它适用于 .NET Framework 4.6+ 和 .NET Core。
// Baseline
public void SimpleSumArray()
{
for (int i = 0; i < left.Length; i++)
results[i] = left[i] + right[i];
}
// Using Vector<T> for SIMD support
public void SimpleSumVectors()
{
int ceiling = left.Length / floatSlots * floatSlots;
for (int i = 0; i < ceiling; i += floatSlots)
{
Vector<float> v1 = new Vector<float>(left, i);
Vector<float> v2 = new Vector<float>(right, i);
(v1 + v2).CopyTo(results, i);
}
for (int i = ceiling; i < left.Length; i++)
{
results[i] = left[i] + right[i];
}
}
不幸的是,向量的初始化可能是限制步骤。为了解决这个问题,一些来源建议使用 MemoryMarshal 将源数组转换为向量数组 [1][2]。例如:
// Improving Vector<T> Initialization Performance
public void SimpleSumVectorsNoCopy()
{
int numVectors = left.Length / floatSlots;
int ceiling = numVectors * floatSlots;
// leftMemory is simply a ReadOnlyMemory<float> referring to the "left" array
ReadOnlySpan<Vector<float>> leftVecArray = MemoryMarshal.Cast<float, Vector<float>>(leftMemory.Span);
ReadOnlySpan<Vector<float>> rightVecArray = MemoryMarshal.Cast<float, Vector<float>>(rightMemory.Span);
Span<Vector<float>> resultsVecArray = MemoryMarshal.Cast<float, Vector<float>>(resultsMemory.Span);
for (int i = 0; i < numVectors; i++)
resultsVecArray[i] = leftVecArray[i] + rightVecArray[i];
}
在 .NET Core 上运行时,这会显着提高性能:
| Method | Mean | Error | StdDev |
|----------------------- |----------:|----------:|----------:|
| SimpleSumArray | 165.90 us | 0.1393 us | 0.1303 us |
| SimpleSumVectors | 53.69 us | 0.0473 us | 0.0443 us |
| SimpleSumVectorsNoCopy | 31.65 us | 0.1242 us | 0.1162 us |
不幸的是,在.NET Framework上,这种初始化向量的方式会产生相反的效果。它实际上会导致更差的性能:
| Method | Mean | Error | StdDev |
|----------------------- |----------:|---------:|---------:|
| SimpleSumArray | 152.92 us | 0.128 us | 0.114 us |
| SimpleSumVectors | 52.35 us | 0.041 us | 0.038 us |
| SimpleSumVectorsNoCopy | 77.50 us | 0.089 us | 0.084 us |
有没有办法在 .NET Framework 上优化 Vector 的初始化并获得与 .NET Core 相似的性能?已使用此示例应用程序 [1] 进行了测量。
解决方案
据我所知,在 .NET Framework 4.6 或 4.7(大概这将在 5.0 中全部更改)中加载矢量的唯一有效方法是使用不安全的代码,例如使用Unsafe.Read<Vector<float>>
(或其未对齐的变体,如果适用):
public unsafe void SimpleSumVectors()
{
int ceiling = left.Length / floatSlots * floatSlots;
fixed (float* leftp = left, rightp = right, resultsp = results)
{
for (int i = 0; i < ceiling; i += floatSlots)
{
Unsafe.Write(resultsp + i,
Unsafe.Read<Vector<float>>(leftp + i) + Unsafe.Read<Vector<float>>(rightp + i));
}
}
for (int i = ceiling; i < left.Length; i++)
{
results[i] = left[i] + right[i];
}
}
这使用了System.Runtime.CompilerServices.Unsafe
可以通过 NuGet 获得的包,但也可以不使用它。
推荐阅读
- php - 谁能帮助我如何在 laravel 中保存多个复选框数据?
- excel - 调试文件对话框代码后的跳线
- android - Xamarin.Android listview getPosition 方法返回对象
- android - 根据另一个 RV 滚动增加和减少 RV 高度
- list - 在 Python 列表中显示所有最长的单词
- angular - 在角度 cli 中检查单选按钮组
- java - Maven:如何创建源文件和测试文件的jar文件?
- java - 频道 ManagedChannelImpl{logId=1, target=photoslibrary.googleapis.com:443} 未正确关闭
- regular-language - 将常规语言插入到其他常规语言中
- laravel - 不应静态调用非静态方法 Illuminate\Routing\Route::middleware()