本文へスキップ
Skillpedia
一般的なプログラミング5.8k · #14557

vectorization

できること

このスキルは、一度に複数の値を処理するSIMDを使って.NETのコードを最適化する手助けをします。結果は元のコードと同じに保ちます。性能が重要な数値計算のコードを持つ.NETの開発者のためのものです。

使う場面

  • ✓.NETの数値のループが遅く、ベクターのコードで速くしたいとき。
  • ✓既存のSIMDのコードの正しさと移植性をレビューしたいとき。
  • ✓TensorPrimitivesやハードウェアのイントリンシクスを安全に使いたいとき。

スキルのリンク

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

コピーしてAI(Claude、ChatGPT、Cursor)に貼り付け、指示に従うよう頼んでください。

使い方

いちばん簡単な方法:メッセージをコピーしてAIに貼り付けるだけ。AIがスキルを読んで指示に従います。

貼り付け用メッセージ

このスキルを使って手伝ってください:https://github.com/dotnet/skills/blob/main/plugins/dotnet-advanced/skills/vectorization/SKILL.md このリンクのSKILL.mdファイルを読んで、私の作業でその指示に従ってください。
こういうスキルを、AIが自分で見つけてくれたら?無料のSkillpedia MCPをインストール。1行で、作業の前に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

ログイン して投票しましょう。

コメント

  • …

このリポジトリの他のスキル

似ているスキル