System.Numerics.Vector <T> ประสิทธิภาพการเริ่มต้นบน. NET Framework

Nov 07 2020

System.Numerics.Vector นำการสนับสนุน SIMD ไปยัง. NET Core และ. NET Framework ทำงานบน. 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];
    }
}

น่าเสียดายที่การเริ่มต้น Vector อาจเป็นขั้นตอนที่ จำกัด ในการแก้ไขปัญหานี้แหล่งข้อมูลหลายแหล่งแนะนำให้ใช้ 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 |

มีวิธีเพิ่มประสิทธิภาพการเริ่มต้น Vector บน. NET Framework และได้ประสิทธิภาพใกล้เคียงกับ. NET Core หรือไม่ ทำการวัดโดยใช้แอปพลิเคชันตัวอย่างนี้ [1]

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

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

คำตอบ

2 harold Nov 08 2020 at 10:18

เท่าที่ฉันทราบวิธีเดียวที่มีประสิทธิภาพในการโหลดเวกเตอร์ใน. 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 แต่ก็สามารถทำได้โดยไม่ต้องใช้เช่นกัน