惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

C
Check Point Blog
aimingoo的专栏
aimingoo的专栏
Jina AI
Jina AI
Microsoft Security Blog
Microsoft Security Blog
IT之家
IT之家
V
Visual Studio Blog
量子位
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
博客园 - 聂微东
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
大猫的无限游戏
大猫的无限游戏
Y
Y Combinator Blog
Stack Overflow Blog
Stack Overflow Blog
D
Docker
MyScale Blog
MyScale Blog
小众软件
小众软件
云风的 BLOG
云风的 BLOG
美团技术团队
Microsoft Azure Blog
Microsoft Azure Blog
酷 壳 – CoolShell
酷 壳 – CoolShell
Last Week in AI
Last Week in AI
Apple Machine Learning Research
Apple Machine Learning Research
博客园 - 【当耐特】

博客园 - ProjectDD

NET 中 Async/Await 的演进:从状态机到运行时优化的 Continuation C# 指针用法小结 C# BinaryPrimitives 类 C# 内存对齐 linq 查询关于 from子句 关于C# await的一点新理解 关于排序算法 C# Dev Kit 经常导致崩溃 不太会用Span<T> 看文档上的优点估摸着试试 span,memory,ArrayPool,MemoryPool,等的性能对比 C# 模式匹配里应该注意的几点 高中生理解梯度为何是方向导数极大值 概略 deep net 通过relu 进行函数逼近 win10 下安装 rust 的 依赖配置,通过vs2022 C# 有多需要aot 24个希腊字母的中文拼音版 英语发音探讨 sagemath 9.x 下的 jupyter 工作路径设置 Serializing delegates is not supported on this platform
C# simd 性能雷点记录
ProjectDD · 2023-04-18 · via 博客园 - ProjectDD

先看两段代码对比:

  static public T SimdDot(T[] a, T[] b) {
    if (a.Length != b.Length) throw new ArgumentException("The size of two matrix is not equal.");
    // if (a.Length == 0) throw new ArgumentException("The length of input matrix must be greater than zero.");
    var result = default(T);
    var vcount= Vector<T>.Count;
    int remaining = a.Length % vcount;
    var idx = 0;
    for (int i = 0; i < a.Length - remaining; i += vcount) {
      var va = new Vector<T>(a, idx * vcount);
      var vb = new Vector<T>(b, idx * vcount);
      result += Vector.Dot(va, vb);
      idx++;
    }
    for (int i = a.Length - remaining; i < a.Length; i++) {
      result += a[i] * b[i];
    }
    return result;
  }
  static public T SimdDot(ReadOnlySpan<T> a, ReadOnlySpan<T> b) {
    if (a.Length != b.Length) throw new ArgumentException("The size of two matrix is not equal.");
    // if (a.Length == 0) throw new ArgumentException("The length of input matrix must be greater than zero.");
    var result = default(T);
    var vcount= Vector<T>.Count;
    int remaining = a.Length % vcount;
    var idx = 0;
    for (int i = 0; i < a.Length - remaining; i += vcount) {
      var va = new Vector<T>(a.Slice(idx*vcount,vcount));
      var vb = new Vector<T>(b.Slice(idx*vcount,vcount));
      result += Vector.Dot(va, vb);
      idx++;
    }
    for (int i = a.Length - remaining; i < a.Length; i++) {
      result += a[i] * b[i];
    }
    return result;
  }

这是向量的点乘运算,下面代码的性能差于传统sisd的性能,而上面的代码略好于sisd代码,它们之间的差别在于 使用 ReadOnlySpan<T>.Slice函数,看来这是个坑点,明明它是号称性能优势的Span<T>系列啊,所以比较出乎意料。特此记上一笔。

这结果是使用上面代码(非下面有红字部分代码段)的测试结果,如果使用有标红部分代码来测试的话,其性能竟然会达到sisd的 3-5倍之巨。