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

推荐订阅源

V
Visual Studio Blog
Google DeepMind News
Google DeepMind News
V
V2EX
B
Blog RSS Feed
有赞技术团队
有赞技术团队
博客园 - Franky
美团技术团队
月光博客
月光博客
酷 壳 – CoolShell
酷 壳 – CoolShell
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
腾讯CDC
云风的 BLOG
云风的 BLOG
L
LangChain Blog
GbyAI
GbyAI
The Cloudflare Blog
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
C
Check Point Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
Stack Overflow Blog
Stack Overflow Blog
博客园 - 【当耐特】
The Register - Security
The Register - Security
大猫的无限游戏
大猫的无限游戏
D
Docker
Vercel News
Vercel News
Blog — PlanetScale
Blog — PlanetScale
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
博客园 - 司徒正美
人人都是产品经理
人人都是产品经理
雷峰网
雷峰网
阮一峰的网络日志
阮一峰的网络日志
P
Proofpoint News Feed
N
Netflix TechBlog - Medium
博客园_首页
A
About on SuperTechFans
J
Java Code Geeks
量子位
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
MongoDB | Blog
MongoDB | Blog
Recent Announcements
Recent Announcements
G
Google Developers Blog
小众软件
小众软件
博客园 - 叶小钗
WordPress大学
WordPress大学
博客园 - 聂微东
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Martin Fowler
Martin Fowler
S
SegmentFault 最新的问题
F
Full Disclosure
Jina AI
Jina AI
H
Help Net Security

博客园 - 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倍之巨。