跳到正文
Skillpedia
通用编程5.8k · #14557

vectorization

它能做什么

这项技能帮助使用 SIMD 优化 .NET 代码。SIMD 可以同时处理多个数值,同时保持结果与原始代码一致。适合拥有对性能要求高的数值计算代码的 .NET 开发者。

适用场景

  • ✓当 .NET 中的数值循环很慢,并且你想用向量代码加速它时。
  • ✓当你想检查现有 SIMD 代码的正确性和可移植性时。
  • ✓当你想安全地使用 TensorPrimitives 或硬件内建函数的时候。

技能链接

https://github.com/dotnet/skills/blob/main/plugins/dotnet-advanced/skills/vectorization/SKILL.md

复制后粘贴给你的 AI(Claude、ChatGPT 或 Cursor),让它按照说明操作。

使用方法

最简单的方法:复制下面的消息,粘贴给你的 AI。它会读取这个技能并按说明操作。

可直接粘贴的消息

请用这个技能帮我:https://github.com/dotnet/skills/blob/main/plugins/dotnet-advanced/skills/vectorization/SKILL.md 阅读该链接中的 SKILL.md 文件,并在我的任务中按照它的说明来做。
想让你的 AI 自己找到这样的技能吗?安装免费的 Skillpedia MCP:只需一行,每次任务前它都会在 15,000+ 个技能中搜索。1 分钟完成安装 →

可以这样问 AI

“请把这个对大型 float 数组求和并缩放的 .NET 函数向量化,并检查结果是否与原始版本一致。”

每周 5 个新技能,1 分钟读完。

工作原理

智能体会保留标量版本作为参考,编写能正确处理剩余元素的向量版本,测试结果是否一致,并在接受前测量速度。

技术内容(原始 SKILL.md)

作者在 GitHub 上的英文原文。

.NET SIMD vectorization

Produce a portable optimization that preserves the scalar contract, remains memory-safe at every length, and earns its complexity with measured results. Read the official SIMD and hardware-intrinsics guidance first and follow its comprehensive implementation templates. In particular, use its self-contained per-width dispatch, dedicated small-input handling, loop, and remainder shapes rather than reducing them to a chain of width checks. This skill supplies the decision rules and validation checks to apply while changing real code.

Inputs and prerequisites

Discover these from the repository before asking the user:

Input Required What to establish
Scalar implementation and tests Yes Existing contract, representative call sites, and supported overlap
Target frameworks and platforms Yes Available SIMD APIs and architectures that must behave consistently
Build and test workflow Yes The repository's normal commands and how to launch separate test processes
Representative workload or benchmark For optimization Typical input sizes and the baseline to beat

Do not add a package merely because an API exists there. First check the target framework and the project's existing dependency/versioning policy.

Core rules

  1. Use the highest-level API that matches the contract, then stop. Span<T> and string operations, TensorPrimitives, and tensor types already accelerate many operations. LINQ reductions such as Sum, Min, Max, and Average can also accelerate when the source exposes its underlying span. Verify empty-input and floating-point behavior rather than assuming similarly named operations are interchangeable. Once an existing API preserves the contract, use it instead of continuing into handwritten SIMD. Before writing an explicit loop, name the framework APIs considered and why none applies. Fixed-shape System.Numerics types remain appropriate for graphics and similar domains.
  2. Start new explicit SIMD loops with Vector128<T>. It is accelerated across the broadest hardware set. Add wider fixed-width paths only when measurements justify them.
  3. Keep platforms consistent. Prefer cross-platform operations on the fixed-width vector types; they lower to the appropriate target instructions. For example, (vector & mask) == Vector128<byte>.Zero becomes ptest on x86/x64. Use architecture-specific intrinsics only for a measured gap, guard them with IsSupported, and retain equivalent portable or scalar behavior.
  4. Read IsHardwareAccelerated, IsSupported, and Count directly. The JIT treats them as constants, so caching them adds no value and obscures which branches disappear.
  5. Prefer operators where they are clear. Parenthesize expressions that mix bitwise and comparison operators so precedence is explicit.

If the task is review-only, do not rewrite the code. Report correctness and memory-safety defects before performance opportunities.

Authoring checklist

  • Contract: identify behavior for empty and short inputs, overlap, overflow, NaN, signed zero, ordering, and exceptions before changing the implementation.
  • Framework gate: inspect the target framework and existing package references, then compile or probe the highest-level candidate API with the required edge cases. A small contract adapter, such as preserving special empty-input behavior, does not justify reimplementing the operation. If the API preserves the contract, use it and stop; do not claim it is unavailable without checking.
  • Structure: for new explicit SIMD, implement Vector128<T> and scalar first. Only after measurements justify wider paths, check Vector512<T>, then Vector256<T>, optional Vector<T>, Vector128<T>, and finally scalar. Omit paths the implementation does not need. Each outer fixed-width guard checks only its IsHardwareAccelerated property and, for generic element types, IsSupported. Inside that block, run the width-specific helper when the input has at least Count elements; otherwise run a dedicated small-input helper, then return. Do not put the length check in the outer guard and fall through to repeat dispatch at narrower widths. Keeping each supported-width block self-contained lets the JIT remove unsupported blocks and avoids redundant work on common sm
常见问题

它会改变结果吗?

不会。目标是保持与标量代码相同的结果,并通过测试进行核对。

它总能让代码变快吗?

不能。它会测量结果,只有在优化的收益足以抵消其复杂度时才会保留。

它涵盖哪些平台?

它涵盖可移植的向量 API 以及不同架构上的硬件特定内建函数。

详情

作者的原始描述

Design, implement, optimize, and review SIMD code in .NET. USE FOR: vectorizing scalar loops with TensorPrimitives, Vector64/128/256/512, or platform hardware intrinsics; reviewing existing SIMD code, including the generic Vector type, for contract equivalence, tail handling, memory safety, portability, fallbacks, and measured performance. DO NOT USE FOR: performance work unrelated to SIMD or vectorization.

repo: dotnet/skills

热度

GitHub 星标
5.8k
排名
#14557
许可证
MIT
更新时间
2026/11/2

这个技能有用吗?

0

登录 后即可投票。

评论

  • …

同一仓库的更多技能

相似技能