【一天一点.NET小知识】运用向量Vector<T>加速求和计算

随着 .NET 版本的演进,从 .NET Standard 2.0 版本开始,支持 Vector<T> 类型。

.NET 8.0 版本开始,大量在 Runtime 提供的各个组件中运用向量计算,​特别是 Linq。
Vector
类型
:表示指定数值类型(适用于并行算法的低级别优化)的单个向量。

假如我们有一个求和函数接受一个int数组入参,当它的长度大于等于8及其倍数以上时,那么我们就可以考虑使用向量Vector<T>加速求和计算。

以下是使用了向量的求和函数代码:

csharp 复制代码
internal class Program
{
    static void Main(string[] args)
    {
        int[] array = Enumerable.Range(1, 32).ToArray();
        int result = Sum(array);
        Console.WriteLine(result);
        Console.ReadKey();
    }

    public static int Sum(int[] numbers)
    {
        ReadOnlySpan<int> span = new ReadOnlySpan<int>(numbers);
        ref int ptr = ref MemoryMarshal.GetReference(span);
        int result = 0;
        int vectorSize = Vector<int>.Count;
        int index;
        int remainder = span.Length % vectorSize;
        int vectorLength = span.Length - remainder;
        Vector<int> vector = Vector<int>.Zero;
        for (index = 0; index < vectorLength; index += vectorSize)
        {
            //Vector<int> vector2 = new Vector<int>(span.Slice(index, vectorSize));
            ref byte address = ref Unsafe.As<int, byte>(ref Unsafe.Add(ref Unsafe.AsRef(in ptr), index));
            Vector<int> vector2 = Unsafe.ReadUnaligned<Vector<int>>(ref address);
            vector += vector2;
        }

        result += Vector.Dot<int>(vector, Vector<int>.One);
        for (; index < span.Length; index++)
        {
            result += Unsafe.Add(ref ptr, index);
        }

        return result;
    }
}

以下是相减函数代码:

csharp 复制代码
static int Sub(int[] numbers)
{
	ReadOnlySpan<int> span = new ReadOnlySpan<int>(numbers);
	ref int ptr = ref MemoryMarshal.GetReference(span);
	int result = 0;
	int vectorSize = Vector<int>.Count;
	int index;
	int remainder = span.Length % vectorSize;
	int vectorLength = span.Length - remainder;
	for (index = 0; index < vectorLength; index += vectorSize)
	{
		ref byte address = ref Unsafe.As<int, byte>(ref Unsafe.Add(ref Unsafe.AsRef(in ptr), index));
		Vector<int> vector = Unsafe.ReadUnaligned<Vector<int>>(ref address);
		result -= Vector.Dot<int>(vector, Vector<int>.One);
	}

	for (; index < span.Length; index++)
	{
		result -= Unsafe.Add(ref ptr, index);
	}

	return result + 2;
}

其它运算,例如相减,也是同理。

以上代码,均可以在 .NET Standard 2.0 及以上版本运行。

当我们向量 Vector<T> 之后,特别是在一些频繁调用计算的场景,将获得指数量级的性能提升。
需要注意的是,向量 Vector<T> 依赖 CPU 硬件的 SIMD 指令集支持,在一些相对较旧的 古董CPU,可能不支持。

PS:

相关推荐
MyBFuture17 分钟前
索引器实战:对象数组访问技巧及命名空间以及项目文件规范
开发语言·前端·c#·visual studio
jghhh0128 分钟前
基于C#的串口电子秤测试程序
开发语言·c#
向宇it1 小时前
【unity游戏开发——网络】使用Unity+PurrNet+Heathens+Steam,在 Unity 中通过 Steam与你的朋友建立联系
网络·游戏·unity·c#·游戏引擎·steam
WebRuntime1 小时前
问世间,exe是何物?直教AI沉默、Web寡言(1)
javascript·c#·.net·web
稀饭过霍2 小时前
【.NET 10.0】使用FluentValidation
c#·mvc·.net
HUST2 小时前
C 语言 第七讲:数组和函数实践:扫雷游戏
c语言·开发语言·数据结构·vscode·算法·游戏·c#
youngee113 小时前
hot100-49前缀树
开发语言·c#
Dillon Dong3 小时前
从C到SIMULINK: 字节/字偏移 + 位偏移实现故障与故障字保存操作
c语言·开发语言·c#
m5655bj3 小时前
如何通过 C# 将 Markdown 转换为 PDF 文档
开发语言·pdf·c#
张人玉3 小时前
WPF HTTPS 通信示例使用说明
数据库·网络协议·http·c#·wpf