微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

.NET Framework上System.Numerics.Vector <T>的初始化性能

如何解决.NET Framework上System.Numerics.Vector <T>的初始化性能

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将源数组转换为Vectors [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]进行了测量。

[1] https://github.com/CBGonzalez/SIMDPerformance

[2] https://stackoverflow.com/a/62702334/430935

解决方法

据我所知,在.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来获得它,但是也可以不用这样做。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。