mirror of
https://github.com/dotnet/skills.git
synced 2026-09-20 09:49:54 +08:00
Add general .NET vectorization skill
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
This commit is contained in:
+3
-3
@@ -24,6 +24,9 @@
|
||||
/plugins/dotnet-advanced/skills/nuget-trusted-publishing/ @lewing @kartheekp-ms @dotnet/skills-csharp-language-reviewers
|
||||
/tests/dotnet-advanced/nuget-trusted-publishing/ @lewing @kartheekp-ms @dotnet/skills-csharp-language-reviewers
|
||||
|
||||
/plugins/dotnet-advanced/skills/vectorization/ @jeffschw @artl93
|
||||
/tests/dotnet-advanced/vectorization/ @jeffschw @artl93
|
||||
|
||||
# dotnet-upgrade (migrating and upgrading .NET projects)
|
||||
/plugins/dotnet-upgrade/skills/thread-abort-migration/ @dotnet/appmodel @dotnet/skills-upgrade-reviewers
|
||||
/tests/dotnet-upgrade/thread-abort-migration/ @dotnet/appmodel @dotnet/skills-upgrade-reviewers
|
||||
@@ -104,9 +107,6 @@
|
||||
/plugins/dotnet-experimental/skills/exp-mock-usage-analysis/ @dotnet/dotnet-testing
|
||||
/tests/dotnet-experimental/exp-mock-usage-analysis/ @dotnet/dotnet-testing
|
||||
|
||||
/plugins/dotnet-experimental/skills/exp-simd-vectorization/ @jeffschw @artl93
|
||||
/tests/dotnet-experimental/exp-simd-vectorization/ @jeffschw @artl93
|
||||
|
||||
# dotnet-maui
|
||||
/plugins/dotnet-maui/ @Redth @jfversluis @dotnet/skills-maui-reviewers
|
||||
/tests/dotnet-maui/ @Redth @jfversluis @dotnet/skills-maui-reviewers
|
||||
|
||||
@@ -11,3 +11,4 @@ Advanced .NET and C# skills for niche scenarios and coding agents.
|
||||
- csharp-scripts
|
||||
- dotnet-pinvoke
|
||||
- nuget-trusted-publishing
|
||||
- vectorization
|
||||
|
||||
@@ -0,0 +1,180 @@
|
||||
---
|
||||
name: vectorization
|
||||
description: >
|
||||
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 Vector<T>, for contract equivalence, tail handling, memory
|
||||
safety, portability, fallbacks, and measured performance. DO NOT USE FOR:
|
||||
performance work unrelated to SIMD or vectorization.
|
||||
license: MIT
|
||||
---
|
||||
|
||||
# .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](https://learn.microsoft.com/dotnet/standard/simd) 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.** `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. 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.
|
||||
- **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 small inputs.
|
||||
- **Loads and stores:** prefer span-based `Vector128.Create(span)` and `CopyTo`; the JIT keeps them
|
||||
efficient and they require no pinning or reference arithmetic. Unsafe loads and stores are largely
|
||||
unnecessary. When a path genuinely must walk a buffer by managed reference, use the element-offset
|
||||
`LoadUnsafe(ref T, nuint)` and `StoreUnsafe` overloads rather than pointers or manually advanced
|
||||
references.
|
||||
- **Empty inputs:** in a reference-based path, obtain the starting reference with
|
||||
`MemoryMarshal.GetReference(span)` or `MemoryMarshal.GetArrayDataReference(array)`, not by indexing
|
||||
element `0`.
|
||||
- **Unsupported element types:** the fixed-width vectors support primitive numeric element types,
|
||||
not `char` or `bool`. Reinterpret with `MemoryMarshal.Cast` or `As<TFrom, TTo>`; reinterpretation
|
||||
changes only the type, not the bits. Keep Boolean data as `0` or `1` and characters as valid
|
||||
UTF-16, normalizing results before storing when necessary.
|
||||
- **Offsets:** prove the input contains a full vector before subtracting `Count` or converting an
|
||||
index to `nuint`; otherwise a negative value becomes a huge unsigned offset.
|
||||
- **Managed references:** never create a reference before the start or past the end of its object,
|
||||
even temporarily. A collection during that interval can leave an interior reference untracked.
|
||||
- **Remainders:** cover every length, including `0`, `Count - 1`, `Count`, `Count + 1`, and
|
||||
nonmultiples of each width. Once the input contains a full vector, keep the tail vectorized by
|
||||
reprocessing the last full vector. An idempotent operation can fold that overlap in directly. A
|
||||
non-idempotent operation must use `ConditionalSelect` to replace repeated lanes with the
|
||||
operation's identity before folding them in. For in-place transforms, preserve the original tail
|
||||
values before overlapping stores and write only valid results.
|
||||
- **Buffer overlap:** choose a traversal direction or staging strategy that prevents stores from
|
||||
corrupting values not yet loaded.
|
||||
- **Numeric behavior:** account for floating-point reassociation, NaN and signed-zero semantics,
|
||||
checked or unchecked integer overflow, and endianness where the algorithm depends on byte order.
|
||||
`Native` and `Estimate` operations can intentionally relax precision or IEEE edge-case behavior;
|
||||
use them only when the contract permits it and measurements justify them.
|
||||
|
||||
The official guidance contains the complete dispatch, small-input, unrolling, and remainder
|
||||
templates; use those for the full implementation. The following excerpt illustrates only the inner
|
||||
safe `Vector128<T>` loop for an in-place elementwise transform, after its self-contained dispatch
|
||||
block has established at least one full vector. `Transform` represents the operation being
|
||||
implemented:
|
||||
|
||||
```csharp
|
||||
Span<int> tail = data.Slice(data.Length - Vector128<int>.Count);
|
||||
Vector128<int> end = Vector128.Create<int>(tail);
|
||||
Span<int> remaining = data;
|
||||
|
||||
while (remaining.Length >= Vector128<int>.Count)
|
||||
{
|
||||
Vector128<int> values = Vector128.Create<int>(remaining);
|
||||
Transform(values).CopyTo(remaining);
|
||||
remaining = remaining.Slice(Vector128<int>.Count);
|
||||
}
|
||||
|
||||
if (!remaining.IsEmpty)
|
||||
{
|
||||
Transform(end).CopyTo(tail);
|
||||
}
|
||||
```
|
||||
|
||||
The early `end` load preserves original values before overlapping stores. For a read-only reduction,
|
||||
load the same final span after the main loop and use `ConditionalSelect` to replace already-processed
|
||||
lanes with the operation's identity. Do not substitute `LoadUnsafe`/`StoreUnsafe` or a scalar
|
||||
epilogue merely to avoid span bounds checks.
|
||||
|
||||
## Testing checklist
|
||||
|
||||
- Compare the optimized implementation with the scalar contract across boundary lengths,
|
||||
randomized values, empty inputs, supported overlap, and numeric edge cases. Cover every
|
||||
implemented width and the scalar path with inputs both large enough and too small to benefit.
|
||||
- Exercise every implemented width and the scalar fallback in separate processes. On x86/x64
|
||||
CoreCLR, `DOTNET_EnableAVX2=0` disables AVX2 and `DOTNET_EnableHWIntrinsic=0` disables hardware
|
||||
intrinsics. Use the repository's normal test command and do not change these process-wide
|
||||
settings inside a unit test. These settings do not change code already compiled as ReadyToRun or
|
||||
ahead of time, so confirm the target code is JIT-compiled when using them to force a path.
|
||||
- For unsafe loads and stores, use guard-page or equivalent boundary tests when available. Put the
|
||||
inaccessible page after the buffer for forward iteration and before it for backwards iteration,
|
||||
and include nonmultiple lengths. An ordinary array allocation does not reliably expose an
|
||||
out-of-bounds read.
|
||||
|
||||
## Benchmarking
|
||||
|
||||
Use BenchmarkDotNet to measure representative small and large inputs before keeping the added
|
||||
complexity. Compare scalar, `Vector128<T>`, and each wider implemented path in the same run. Small
|
||||
inputs can be slower because setup dominates, and speedups are rarely the theoretical vector-width
|
||||
multiple because memory throughput, alignment, and latency still apply. Report throughput or time
|
||||
with noise context and, when relevant, generated code size or instruction counts. Control allocation
|
||||
alignment for stable measurements or randomize it to observe the distribution. A wider vector is
|
||||
not automatically faster.
|
||||
|
||||
If the project cannot target the required framework, run the relevant architecture, or execute the
|
||||
fallback configuration, state exactly which path remains unverified. Do not claim success from a
|
||||
default-hardware test alone.
|
||||
|
||||
## Completion contract
|
||||
|
||||
- **Authoring:** leave the scalar contract covered by tests; identify the framework or SIMD layer
|
||||
selected; report measurements for the representative workload; name any architecture or fallback
|
||||
path that could not be exercised.
|
||||
- **Review:** report only concrete findings, ordered by correctness, memory safety, portability,
|
||||
tests, then performance evidence. If none remain, say so directly.
|
||||
- Do not call an optimization complete when it only builds, only passes on the current machine, or
|
||||
has no comparison against the scalar baseline.
|
||||
|
||||
## Review checklist
|
||||
|
||||
Review in this order:
|
||||
|
||||
1. Scalar-contract equivalence, including signed zero, NaN, overflow, and relevant endianness
|
||||
2. Reuse of an existing accelerated framework API
|
||||
3. Tail correctness for idempotent versus non-idempotent work
|
||||
4. Memory safety, unsigned offset arithmetic, empty inputs, and overlapping buffers
|
||||
5. Portable dispatch and behaviorally equivalent fallbacks
|
||||
6. Tests that force each width and the scalar path
|
||||
7. Benchmarks that justify explicit SIMD and additional widths
|
||||
@@ -1,197 +0,0 @@
|
||||
---
|
||||
name: exp-simd-vectorization
|
||||
description: "Optimizes hot-path scalar loops in .NET 8+ with cross-platform Vector128/Vector256/Vector512 SIMD intrinsics, or replaces manual math loops with single TensorPrimitives API calls. Covers byte-range validation, character counting, bulk bitwise ops, cross-type conversion, fused multi-array computations, and float/double math operations."
|
||||
license: MIT
|
||||
---
|
||||
|
||||
# SIMD Vectorization
|
||||
|
||||
## Decision Gate
|
||||
1. **Check `Span<T>` and `MemoryExtensions` first.** If the operation can be expressed using built-in `Span<T>` methods (e.g., `Contains`, `IndexOf`, `CopyTo`, `SequenceEqual`) or `MemoryExtensions`, use them — no additional dependency is needed and the runtime already vectorizes many of these internally.
|
||||
2. **Check for TensorPrimitives next.** If one or more TensorPrimitives methods cover the operation → use them. If the `.csproj` does NOT already reference `System.Numerics.Tensors`, **add the package**, for example: `<PackageReference Include="System.Numerics.Tensors" />` (or use the versioning approach already used by your solution). Then replace the scalar loop with TP calls and stop. See the full API table below. Compose multiple TP calls when needed (e.g., finding both min and max → `TensorPrimitives.Min(span)` + `TensorPrimitives.Max(span)` as two calls). Do NOT write manual Vector128 code for operations TP already handles.
|
||||
3. **Scalar loop over contiguous array/span** of `byte`, `sbyte`, `short`, `ushort`, `int`, `uint`, `long`, `ulong`, `nint`, `nuint`, `float`, `double` (and `char` via reinterpretation as `ushort`)? → Implement with explicit `Vector128<T>` / `Vector256<T>` / `Vector512<T>` intrinsics using the patterns below.
|
||||
4. **No contiguous numeric arrays to process** (dictionary lookups, tree traversals, linked lists, state machines, string formatting, small collections, enum comparisons, recursive algorithms, decimal arithmetic)? → Report `[NO SIMD OPPORTUNITY]` and write a **full paragraph** explaining WHY, referencing the specific code characteristics that prevent vectorization (e.g., "State machines require sequential branching on enum values — there are no contiguous numeric arrays to process in parallel, and each transition depends on the previous state"). This explanation is graded.
|
||||
|
||||
## TensorPrimitives API Reference
|
||||
TensorPrimitives APIs are generic and work for any primitive type that satisfies the method's generic constraints — not just `float`/`double`. For example, `Sum` requires `IAdditionOperators<T,T,T>` + `IAdditiveIdentity<T,T>` and works for all primitive numeric types, while `CosineSimilarity` requires `IRootFunctions<T>` and only works for `float`/`double`. If the project doesn't already reference `System.Numerics.Tensors`, add it to the `.csproj`. Replace the entire manual loop with **one or more** `TensorPrimitives` calls as needed (prefer a single call when possible):
|
||||
|
||||
### Reductions (span → scalar)
|
||||
| Operation | API |
|
||||
|-----------|-----|
|
||||
| Sum | `TensorPrimitives.Sum(span)` |
|
||||
| Sum of squares | `TensorPrimitives.SumOfSquares(span)` |
|
||||
| Sum of magnitudes (L1 norm) | `TensorPrimitives.SumOfMagnitudes(span)` |
|
||||
| L2 norm | `TensorPrimitives.Norm(span)` |
|
||||
| Product of all elements | `TensorPrimitives.Product(span)` |
|
||||
| Min value | `TensorPrimitives.Min(span)` |
|
||||
| Max value | `TensorPrimitives.Max(span)` |
|
||||
| Index of max | `TensorPrimitives.IndexOfMax(span)` |
|
||||
| Index of min | `TensorPrimitives.IndexOfMin(span)` |
|
||||
| Dot product | `TensorPrimitives.Dot(a, b)` |
|
||||
| Cosine similarity | `TensorPrimitives.CosineSimilarity(a, b)` |
|
||||
| Euclidean distance | `TensorPrimitives.Distance(a, b)` |
|
||||
|
||||
### Element-wise transforms (span → span)
|
||||
| Operation | API |
|
||||
|-----------|-----|
|
||||
| Negate | `TensorPrimitives.Negate(src, dst)` |
|
||||
| Abs | `TensorPrimitives.Abs(src, dst)` |
|
||||
| Sqrt | `TensorPrimitives.Sqrt(src, dst)` |
|
||||
| Exp | `TensorPrimitives.Exp(src, dst)` |
|
||||
| Log | `TensorPrimitives.Log(src, dst)` |
|
||||
| Log2 | `TensorPrimitives.Log2(src, dst)` |
|
||||
| Tanh | `TensorPrimitives.Tanh(src, dst)` |
|
||||
| Sigmoid | `TensorPrimitives.Sigmoid(src, dst)` |
|
||||
| SoftMax | `TensorPrimitives.SoftMax(src, dst)` |
|
||||
| Sinh | `TensorPrimitives.Sinh(src, dst)` |
|
||||
| Cosh | `TensorPrimitives.Cosh(src, dst)` |
|
||||
| Round | `TensorPrimitives.Round(src, dst)` |
|
||||
| Floor | `TensorPrimitives.Floor(src, dst)` |
|
||||
| Ceiling | `TensorPrimitives.Ceiling(src, dst)` |
|
||||
| CopySign | `TensorPrimitives.CopySign(src, sign, dst)` |
|
||||
| Pow | `TensorPrimitives.Pow(bases, exponents, dst)` |
|
||||
|
||||
### Two-span operations (a, b → dst)
|
||||
| Operation | API |
|
||||
|-----------|-----|
|
||||
| Add | `TensorPrimitives.Add(a, b, dst)` |
|
||||
| Subtract | `TensorPrimitives.Subtract(a, b, dst)` |
|
||||
| Multiply | `TensorPrimitives.Multiply(a, b, dst)` |
|
||||
| Divide | `TensorPrimitives.Divide(a, b, dst)` |
|
||||
| Element-wise Min | `TensorPrimitives.Min(a, b, dst)` |
|
||||
| Element-wise Max | `TensorPrimitives.Max(a, b, dst)` |
|
||||
|
||||
### Three-span fused operations
|
||||
| Operation | API |
|
||||
|-----------|-----|
|
||||
| (x+y)*z | `TensorPrimitives.AddMultiply(x, y, z, dst)` |
|
||||
| x*y+z | `TensorPrimitives.MultiplyAdd(x, y, z, dst)` |
|
||||
| fma(x,y,z) | `TensorPrimitives.FusedMultiplyAdd(x, y, z, dst)` |
|
||||
|
||||
> `AddMultiply` and `MultiplyAdd` are distinct — they optimize differently depending on whether the dependency chain flows from the addend or the multiplier. `FusedMultiplyAdd` is the IEEE 754 fused form of (x*y)+z with a single rounding step.
|
||||
|
||||
## Manual SIMD with Vector128/Vector256/Vector512
|
||||
|
||||
Use this when TensorPrimitives doesn't have a single API for the operation. This is required for byte-level operations, character class counting, range validation, bitwise bulk ops, cross-type conversions, and custom patterns.
|
||||
|
||||
### Required imports
|
||||
```csharp
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Runtime.Intrinsics;
|
||||
```
|
||||
Prefer cross-platform APIs (`System.Runtime.Intrinsics`). Only use platform-specific intrinsics (`System.Runtime.Intrinsics.X86`, `.Arm`) when there is a significant performance advantage that justifies the increased code complexity of maintaining separate code paths.
|
||||
|
||||
### Three-tier dispatch pattern
|
||||
Always include all three tiers. Use `if`/`else if` so that small inputs hit only one branch before reaching the scalar fallback — a fallthrough pattern (sequential `if`s) pessimizes the scalar case by requiring up to three not-taken branches that may mispredict. The `IsHardwareAccelerated` checks are JIT-time constants, so dead paths are eliminated at compile time:
|
||||
```csharp
|
||||
ref var src = ref MemoryMarshal.GetReference(span);
|
||||
uint i = 0;
|
||||
uint length = (uint)span.Length;
|
||||
|
||||
if (Vector512.IsHardwareAccelerated && Vector512<T>.IsSupported)
|
||||
{
|
||||
uint vec512Count = (uint)Vector512<T>.Count;
|
||||
while (i + vec512Count <= length)
|
||||
{
|
||||
var vec = Vector512.LoadUnsafe(ref src, i);
|
||||
// ... process vec ...
|
||||
i += vec512Count;
|
||||
}
|
||||
}
|
||||
else if (Vector256.IsHardwareAccelerated && Vector256<T>.IsSupported)
|
||||
{
|
||||
uint vec256Count = (uint)Vector256<T>.Count;
|
||||
while (i + vec256Count <= length)
|
||||
{
|
||||
var vec = Vector256.LoadUnsafe(ref src, i);
|
||||
// ... process vec ...
|
||||
i += vec256Count;
|
||||
}
|
||||
}
|
||||
else if (Vector128.IsHardwareAccelerated && Vector128<T>.IsSupported)
|
||||
{
|
||||
uint vec128Count = (uint)Vector128<T>.Count;
|
||||
while (i + vec128Count <= length)
|
||||
{
|
||||
var vec = Vector128.LoadUnsafe(ref src, i);
|
||||
// ... process vec ...
|
||||
i += vec128Count;
|
||||
}
|
||||
}
|
||||
// Scalar fallback for remaining elements (and the only loop hit for small inputs)
|
||||
for (; i < length; i++)
|
||||
{
|
||||
// ... scalar processing ...
|
||||
}
|
||||
```
|
||||
|
||||
### Core SIMD operations
|
||||
- **Load/Store:** `Vector128.LoadUnsafe(ref src, offset)` / `.StoreUnsafe(ref dst, offset)`
|
||||
- **Arithmetic:** `+`, `-`, `*`, `/` operators on vector types
|
||||
- **Multiply-add (approximate):** `Vector128.MultiplyAddEstimate(a, b, c)` — performs a multiply-add with implementation-defined approximation; not guaranteed to be a strict IEEE fused multiply-add. For precise fused semantics, use `Vector128.FusedMultiplyAdd(a, b, c)`.
|
||||
- **Comparison:** `Vector128.Equals`, `.LessThan`, `.GreaterThan` — returns mask vector
|
||||
- **Mask ops:** `Vector128.All(mask)`, `.Any(mask)`, `.None(mask)`, `.Count(mask)`, `.CountWhereAllBitsSet(mask)`
|
||||
- **Horizontal:** `Vector128.Sum(vec)` for reduction; `.Min(a,b)`, `.Max(a,b)` element-wise
|
||||
- **Broadcast:** `Vector128.Create(scalarValue)` — fill all lanes with one value
|
||||
- **Bitwise:** `&`, `|`, `^`, `~` operators; `Vector128.ShiftLeft`, `.ShiftRightLogical`
|
||||
- **Widening:** `Vector128.WidenLower(v)` / `.WidenUpper(v)` for byte→short, short→int
|
||||
- **Narrowing:** `Vector128.Narrow(lower, upper)` for int→short, short→byte
|
||||
- **Type convert:** `Vector128.ConvertToSingle(intVec)`, `.ConvertToInt32(floatVec)`
|
||||
- **Shuffle:** `Vector128.Shuffle(vec, indices)` — lookup table / permutation
|
||||
- **Conditional:** `Vector128.ConditionalSelect(mask, trueVec, falseVec)`
|
||||
|
||||
### Pattern: Unsigned range check (byte-range validation)
|
||||
For checking if all bytes are in range [lo, hi]:
|
||||
```csharp
|
||||
var vLo = Vector128.Create((byte)lo);
|
||||
var vRange = Vector128.Create((byte)(hi - lo));
|
||||
// (b - lo) > range means out-of-range (unsigned wraparound catches b < lo)
|
||||
var shifted = Vector128.Subtract(vec, vLo);
|
||||
var inRange = Vector128.LessThanOrEqual(shifted, vRange);
|
||||
if (!Vector128.All(inRange.AsByte())) return false; // for validation
|
||||
// or: count += Vector128.CountWhereAllBitsSet(inRange); // for counting
|
||||
```
|
||||
|
||||
### Pattern: Nibble-lookup counting (character classes, popcount, etc.)
|
||||
For counting bytes matching a sparse set of values (vowels, digits, punctuation, bit counts) — build two 16-byte lookup tables indexed by low/high nibble:
|
||||
```csharp
|
||||
var lo_lut = Vector128.Create(/* 16 bytes: bit pattern for low nibble match */);
|
||||
var hi_lut = Vector128.Create(/* 16 bytes: bit pattern for high nibble match */);
|
||||
var nibbleMask = Vector128.Create((byte)0x0F);
|
||||
|
||||
var lo_nibble = vec & nibbleMask;
|
||||
var hi_nibble = Vector128.ShiftRightLogical(vec.AsUInt16(), 4).AsByte() & nibbleMask;
|
||||
var lo_match = Vector128.Shuffle(lo_lut, lo_nibble);
|
||||
var hi_match = Vector128.Shuffle(hi_lut, hi_nibble);
|
||||
var match = lo_match & hi_match;
|
||||
count += Vector128.CountWhereAllBitsSet(~Vector128.Equals(match, Vector128<byte>.Zero));
|
||||
```
|
||||
This same technique works for popcount (LUT = {0,1,1,2,1,2,2,3,1,2,2,3,2,3,3,4}).
|
||||
For simpler cases (single byte value, adjacent range), use `Equals` + `Count` or range check instead.
|
||||
|
||||
### Pattern: Cross-type conversion (widening chains)
|
||||
When the source and destination types differ (e.g., byte→float for dequantization, short→byte for narrowing):
|
||||
```csharp
|
||||
// Widen: byte → short → int → float
|
||||
var bytes = Vector128.LoadUnsafe(ref src, offset);
|
||||
var (lo16, hi16) = Vector128.Widen(bytes);
|
||||
var (lo32a, lo32b) = Vector128.Widen(lo16);
|
||||
var f0 = Vector128.ConvertToSingle(lo32a.AsInt32());
|
||||
|
||||
// Narrow: int → short → byte (with saturation via Min/Max clamping)
|
||||
var clamped = Vector128.Min(Vector128.Max(vec, Vector128<short>.Zero), Vector128.Create((short)255));
|
||||
var narrowed = Vector128.Narrow(clamped.AsUInt16(), nextVec.AsUInt16());
|
||||
```
|
||||
|
||||
### Trailing elements
|
||||
- **Idempotent ops** (validation, search): overlap last vector — re-processing is safe
|
||||
- **Aggregations** (sum, count, min/max): scalar loop for remainder to avoid double-counting
|
||||
- **Store ops** (transform in-place): use `ConditionalSelect` to merge with last stored vector
|
||||
|
||||
## Key Rules
|
||||
- Preserve original method signature — drop-in replacement
|
||||
- Keep scalar code as fallback — never delete it
|
||||
- Use `Vector128<T>` / `Vector256<T>` / `Vector512<T>` explicitly — never `Vector<T>`
|
||||
- Prefer portable `Vector128<T>`/`Vector256<T>`/`Vector512<T>` APIs over platform-specific intrinsics (`Avx2`, `Sse42`, `AdvSimd`, `Fma`) unless there is a significant performance advantage
|
||||
- Testing: use `dotnet run` (NOT `dotnet test`) — xunit.v3 is an in-process runner
|
||||
@@ -0,0 +1,314 @@
|
||||
name: vectorization
|
||||
description: Evaluates the dotnet-advanced/vectorization skill
|
||||
type: capability
|
||||
defaults:
|
||||
timeout: 5m
|
||||
runs: 1
|
||||
stimuli:
|
||||
- name: Detect unsigned tail offset underflow
|
||||
prompt: |
|
||||
Review TailSearch.cs for correctness and memory safety across every input
|
||||
length. Do not edit the file. Report only issues that can affect behavior.
|
||||
environment:
|
||||
files:
|
||||
- src: fixtures/TailSearch.cs
|
||||
dest: TailSearch.cs
|
||||
constraints:
|
||||
reject_tools: [edit, create]
|
||||
graders:
|
||||
- type: exit-success
|
||||
- type: prompt
|
||||
rubric:
|
||||
- Identifies that short inputs produce a negative last-vector index which becomes a very large unsigned offset
|
||||
- Explains that the length must be checked before subtracting a vector width
|
||||
- Prefers an existing accelerated span search over retaining a hand-written loop
|
||||
- If retaining explicit SIMD, recommends a scalar small-input path, safe span loading, and an overlapping final vector
|
||||
|
||||
- name: Repair vectorized reduction safely
|
||||
prompt: |
|
||||
SumValues.cs produces wrong answers for some input lengths, and profiling
|
||||
shows this method is hot. Fix the implementation without losing SIMD for
|
||||
inputs that contain at least one full vector.
|
||||
environment:
|
||||
files:
|
||||
- src: fixtures/sum-values
|
||||
dest: .
|
||||
graders:
|
||||
- type: file-contains
|
||||
config:
|
||||
path: SumValues.cs
|
||||
value: Vector128.Create
|
||||
- type: file-contains
|
||||
config:
|
||||
path: SumValues.cs
|
||||
value: ConditionalSelect
|
||||
- type: file-contains
|
||||
config:
|
||||
path: SumValues.cs
|
||||
value: Vector128.IsHardwareAccelerated
|
||||
- type: file-not-contains
|
||||
config:
|
||||
path: SumValues.cs
|
||||
value: LoadUnsafe
|
||||
- type: file-not-contains
|
||||
config:
|
||||
path: SumValues.cs
|
||||
value: MemoryMarshal
|
||||
- type: run-command
|
||||
config:
|
||||
command: dotnet run --project SumValues.csproj
|
||||
expected_exit_code: 0
|
||||
stdout_matches: PASS
|
||||
- type: run-command
|
||||
config:
|
||||
command: DOTNET_EnableHWIntrinsic=0 dotnet run --project SumValues.csproj
|
||||
expected_exit_code: 0
|
||||
stdout_matches: PASS
|
||||
- type: exit-success
|
||||
- type: prompt
|
||||
rubric:
|
||||
- Corrects the double-counted overlap for exact-width and nonmultiple lengths
|
||||
- Preserves unchecked integer overflow behavior
|
||||
- Uses bounds-checked span operations rather than managed-reference arithmetic
|
||||
- Keeps the final partial block vectorized by excluding already-counted lanes
|
||||
- Uses the scalar implementation for short inputs and when hardware acceleration is unavailable
|
||||
|
||||
- name: Detect out-of-range backwards reference
|
||||
prompt: |
|
||||
Review LastMatch.cs for correctness and managed-memory safety. Do not edit
|
||||
the file.
|
||||
environment:
|
||||
files:
|
||||
- src: fixtures/LastMatch.cs
|
||||
dest: LastMatch.cs
|
||||
constraints:
|
||||
reject_tools: [edit, create]
|
||||
graders:
|
||||
- type: exit-success
|
||||
- type: prompt
|
||||
rubric:
|
||||
- Identifies the one-past-end managed reference as unsafe even though it is decremented before dereference
|
||||
- Explains that a garbage collection while the reference is outside the object can invalidate tracking
|
||||
- Recommends keeping the base reference in range and using an element offset
|
||||
|
||||
- name: Detect empty-span reference access
|
||||
prompt: |
|
||||
Review ZeroBytes.cs for edge-case correctness and memory safety. Do not edit
|
||||
the file. The public contract permits an empty span.
|
||||
environment:
|
||||
files:
|
||||
- src: fixtures/ZeroBytes.cs
|
||||
dest: ZeroBytes.cs
|
||||
constraints:
|
||||
reject_tools: [edit, create]
|
||||
graders:
|
||||
- type: exit-success
|
||||
- type: prompt
|
||||
rubric:
|
||||
- Identifies that indexing element zero throws before the empty-input behavior can be honored
|
||||
- Prefers an existing accelerated span operation that handles empty input
|
||||
- If retaining explicit SIMD, recommends bounds-checked span-based vector creation rather than managed-reference loading
|
||||
|
||||
- name: Detect unsupported vector element type
|
||||
prompt: |
|
||||
Review AsciiLetters.cs for portability and runtime correctness on .NET 8.
|
||||
Do not edit the file.
|
||||
environment:
|
||||
files:
|
||||
- src: fixtures/AsciiLetters.cs
|
||||
dest: AsciiLetters.cs
|
||||
constraints:
|
||||
reject_tools: [edit, create]
|
||||
graders:
|
||||
- type: exit-success
|
||||
- type: prompt
|
||||
rubric:
|
||||
- Identifies that char is not a supported fixed-width vector element type
|
||||
- Identifies that evaluating Vector128<char>.Count throws when the hardware-accelerated branch is reached
|
||||
- Recognizes that the method examines only the first character and has no useful vectorizable work
|
||||
- Recommends retaining the direct char.IsAsciiLetter check rather than reinterpreting the input
|
||||
|
||||
- name: Detect architecture-specific behavior change
|
||||
prompt: |
|
||||
Review ContainsZero.cs. It must return the same answer on x64, Arm64, and
|
||||
machines with hardware intrinsics disabled. Do not edit the file.
|
||||
environment:
|
||||
files:
|
||||
- src: fixtures/ContainsZero.cs
|
||||
dest: ContainsZero.cs
|
||||
constraints:
|
||||
reject_tools: [edit, create]
|
||||
graders:
|
||||
- type: exit-success
|
||||
- type: prompt
|
||||
rubric:
|
||||
- Identifies that unsupported AVX2 currently changes the result instead of selecting an equivalent implementation
|
||||
- Prefers an existing accelerated span search that behaves consistently across architectures
|
||||
- If retaining explicit SIMD, recommends portable fixed-width dispatch, safe span loading, and an equivalent narrower or scalar fallback
|
||||
|
||||
- name: Preserve product empty-input contract
|
||||
prompt: |
|
||||
Product.cs is in a .NET 10 project and processes arrays containing
|
||||
100-1000 values. Optimize it for throughput without changing any
|
||||
observable behavior, including empty input.
|
||||
environment:
|
||||
files:
|
||||
- src: fixtures/product
|
||||
dest: .
|
||||
graders:
|
||||
- type: file-contains
|
||||
config:
|
||||
path: Product.cs
|
||||
value: TensorPrimitives
|
||||
- type: run-command
|
||||
config:
|
||||
command: dotnet run --project Product.csproj
|
||||
expected_exit_code: 0
|
||||
stdout_matches: PASS
|
||||
- type: exit-success
|
||||
- type: prompt
|
||||
rubric:
|
||||
- Produces a concise optimized implementation without duplicating a framework-provided operation
|
||||
- Preserves the existing zero result for empty input and correct results for non-empty input
|
||||
- Leaves the supplied project building and running successfully
|
||||
|
||||
- name: Vectorize conditional increment safely
|
||||
prompt: |
|
||||
ConditionalIncrement.cs is in a .NET 10 project and processes arrays with
|
||||
more than 100,000 elements. This is the first implementation pass and no
|
||||
representative benchmark data is available yet. Optimize IncrementAbove
|
||||
for throughput on x64 and Arm64 while preserving its behavior for every
|
||||
input length.
|
||||
environment:
|
||||
files:
|
||||
- src: fixtures/conditional-increment
|
||||
dest: .
|
||||
graders:
|
||||
- type: file-contains
|
||||
config:
|
||||
path: ConditionalIncrement.cs
|
||||
value: Vector128.Create
|
||||
- type: file-contains
|
||||
config:
|
||||
path: ConditionalIncrement.cs
|
||||
value: Vector128.IsHardwareAccelerated
|
||||
- type: file-contains
|
||||
config:
|
||||
path: ConditionalIncrement.cs
|
||||
value: CopyTo
|
||||
- type: file-not-contains
|
||||
config:
|
||||
path: ConditionalIncrement.cs
|
||||
value: LoadUnsafe
|
||||
- type: file-not-contains
|
||||
config:
|
||||
path: ConditionalIncrement.cs
|
||||
value: MemoryMarshal
|
||||
- type: file-not-contains
|
||||
config:
|
||||
path: ConditionalIncrement.cs
|
||||
value: System.Runtime.Intrinsics.X86
|
||||
- type: file-not-contains
|
||||
config:
|
||||
path: ConditionalIncrement.cs
|
||||
value: Vector256
|
||||
- type: run-command
|
||||
config:
|
||||
command: dotnet run --project ConditionalIncrement.csproj
|
||||
expected_exit_code: 0
|
||||
stdout_matches: PASS
|
||||
- type: run-command
|
||||
config:
|
||||
command: DOTNET_EnableHWIntrinsic=0 dotnet run --project ConditionalIncrement.csproj
|
||||
expected_exit_code: 0
|
||||
stdout_matches: PASS
|
||||
- type: exit-success
|
||||
- type: prompt
|
||||
rubric:
|
||||
- Produces the same mutations as the scalar contract for empty, short, exact-width, nonmultiple, and large inputs
|
||||
- Uses an implementation that can run on both x64 and Arm64
|
||||
- Retains correct processing when hardware intrinsics are unavailable
|
||||
- Keeps the nonmultiple tail vectorized without applying the increment twice to overlapping elements
|
||||
- Does not add wider vector paths without measurements showing they improve this method
|
||||
|
||||
- name: Extend an existing vectorized path
|
||||
prompt: |
|
||||
A benchmark shows that ClampNegative.ToZero benefits from a Vector256 path
|
||||
on supported hardware. Add that path while retaining the existing
|
||||
self-contained Vector128 dispatch and scalar behavior on other machines.
|
||||
environment:
|
||||
files:
|
||||
- src: fixtures/widen-clamp
|
||||
dest: .
|
||||
graders:
|
||||
- type: file-contains
|
||||
config:
|
||||
path: ClampNegative.cs
|
||||
value: Vector256
|
||||
- type: file-contains
|
||||
config:
|
||||
path: ClampNegative.cs
|
||||
value: Vector128
|
||||
- type: file-contains
|
||||
config:
|
||||
path: ClampNegative.cs
|
||||
value: Vector256.IsHardwareAccelerated
|
||||
- type: file-contains
|
||||
config:
|
||||
path: ClampNegative.cs
|
||||
value: Vector128.IsHardwareAccelerated
|
||||
- type: file-contains
|
||||
config:
|
||||
path: ClampNegative.cs
|
||||
value: ToZeroSmall
|
||||
- type: file-not-contains
|
||||
config:
|
||||
path: ClampNegative.cs
|
||||
value: System.Runtime.Intrinsics.X86
|
||||
- type: file-not-contains
|
||||
config:
|
||||
path: ClampNegative.cs
|
||||
value: LoadUnsafe
|
||||
- type: file-not-contains
|
||||
config:
|
||||
path: ClampNegative.cs
|
||||
value: MemoryMarshal
|
||||
- type: run-command
|
||||
config:
|
||||
command: dotnet run --project WidenClamp.csproj
|
||||
expected_exit_code: 0
|
||||
stdout_matches: PASS
|
||||
- type: run-command
|
||||
config:
|
||||
command: DOTNET_EnableAVX2=0 dotnet run --project WidenClamp.csproj
|
||||
expected_exit_code: 0
|
||||
stdout_matches: PASS
|
||||
- type: run-command
|
||||
config:
|
||||
command: DOTNET_EnableHWIntrinsic=0 dotnet run --project WidenClamp.csproj
|
||||
expected_exit_code: 0
|
||||
stdout_matches: PASS
|
||||
- type: exit-success
|
||||
- type: prompt
|
||||
rubric:
|
||||
- Adds a portable wider path without removing the existing Vector128 or scalar paths
|
||||
- Keeps the wider and narrower implementations structurally consistent
|
||||
- Selects the widest accelerated width before checking length, then returns through either that width or a dedicated small-input path
|
||||
- Does not cascade through narrower hardware-dispatch guards merely because the input is shorter than a wider vector
|
||||
- Preserves correct behavior for empty, short, exact-width, nonmultiple, and large inputs
|
||||
- Selects an equivalent fallback when the wider instruction set or all hardware intrinsics are unavailable
|
||||
|
||||
- name: Ignore unrelated parser performance request
|
||||
prompt: |
|
||||
A profile of an ASP.NET Core endpoint attributes 62% of samples to parsing
|
||||
nested JSON into polymorphic objects and 21% to dictionary lookups. How
|
||||
should I approach optimizing it?
|
||||
expect_activation: false
|
||||
graders:
|
||||
- type: exit-success
|
||||
- type: prompt
|
||||
rubric:
|
||||
- Treats the request as general profiling and application optimization rather than assuming data-parallel work
|
||||
- Does not propose handwritten vector operations without evidence of a suitable contiguous loop
|
||||
- Did not apply SIMD transformations to the parser or dictionary operations
|
||||
- Recommends measuring the actual parser and allocation bottlenecks
|
||||
@@ -0,0 +1,15 @@
|
||||
using System.Runtime.Intrinsics;
|
||||
|
||||
public static class AsciiLetters
|
||||
{
|
||||
public static bool StartsWithAsciiLetter(ReadOnlySpan<char> text)
|
||||
{
|
||||
if (!Vector128.IsHardwareAccelerated || text.Length < Vector128<char>.Count)
|
||||
{
|
||||
return text.Length != 0 && char.IsAsciiLetter(text[0]);
|
||||
}
|
||||
|
||||
Vector128<char> chars = Vector128.Create(text);
|
||||
return chars[0] is >= 'A' and <= 'Z' or >= 'a' and <= 'z';
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Runtime.Intrinsics;
|
||||
using System.Runtime.Intrinsics.X86;
|
||||
|
||||
public static class ContainsZero
|
||||
{
|
||||
public static bool Search(ReadOnlySpan<byte> data)
|
||||
{
|
||||
if (!Avx2.IsSupported)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
ref byte start = ref MemoryMarshal.GetReference(data);
|
||||
nuint i = 0;
|
||||
|
||||
for (; i + (nuint)Vector256<byte>.Count <= (nuint)data.Length; i += (nuint)Vector256<byte>.Count)
|
||||
{
|
||||
if (Vector256.EqualsAny(Vector256.LoadUnsafe(ref start, i), Vector256<byte>.Zero))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
for (; i < (nuint)data.Length; i++)
|
||||
{
|
||||
if (Unsafe.Add(ref start, i) == 0)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
public static class LastMatch
|
||||
{
|
||||
public static int LastIndexOf(ReadOnlySpan<int> values, int target)
|
||||
{
|
||||
ref int start = ref MemoryMarshal.GetReference(values);
|
||||
ref int current = ref Unsafe.Add(ref start, values.Length);
|
||||
|
||||
for (int i = values.Length - 1; i >= 0; i--)
|
||||
{
|
||||
current = ref Unsafe.Subtract(ref current, 1);
|
||||
if (current == target)
|
||||
{
|
||||
return i;
|
||||
}
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Runtime.Intrinsics;
|
||||
|
||||
public static class TailSearch
|
||||
{
|
||||
public static bool ContainsZero(ReadOnlySpan<int> values)
|
||||
{
|
||||
ref int start = ref MemoryMarshal.GetReference(values);
|
||||
|
||||
for (int i = 0; i < values.Length - Vector128<int>.Count; i++)
|
||||
{
|
||||
if (values[i] == 0)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
nuint last = (nuint)(values.Length - Vector128<int>.Count);
|
||||
Vector128<int> tail = Vector128.LoadUnsafe(ref start, last);
|
||||
|
||||
return Vector128.EqualsAny(tail, Vector128<int>.Zero);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
using System.Runtime.Intrinsics;
|
||||
|
||||
public static class ZeroBytes
|
||||
{
|
||||
public static bool IsAllZero(Span<byte> data)
|
||||
{
|
||||
ref byte start = ref data[0];
|
||||
|
||||
if (data.IsEmpty)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
int i = 0;
|
||||
|
||||
if (data.Length >= Vector128<byte>.Count)
|
||||
{
|
||||
Vector128<byte> first = Vector128.LoadUnsafe(ref start);
|
||||
if (first != Vector128<byte>.Zero)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
i = Vector128<byte>.Count;
|
||||
}
|
||||
|
||||
for (; i < data.Length; i++)
|
||||
{
|
||||
if (data[i] != 0)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
+3
-5
@@ -1,15 +1,13 @@
|
||||
using System;
|
||||
|
||||
namespace IntConditionalInc;
|
||||
|
||||
public class ConditionalIncrement
|
||||
public static class ConditionalIncrement
|
||||
{
|
||||
public static void IncrementAbove(Span<int> data, int threshold)
|
||||
{
|
||||
for (int i = 0; i < data.Length; i++)
|
||||
{
|
||||
if (data[i] > threshold)
|
||||
{
|
||||
data[i]++;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
+8
@@ -0,0 +1,8 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net10.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
</Project>
|
||||
@@ -0,0 +1,30 @@
|
||||
int[] lengths = [0, 1, 3, 4, 5, 7, 8, 15, 16, 17, 31, 32, 33, 127, 128, 129, 1024];
|
||||
|
||||
foreach (int length in lengths)
|
||||
{
|
||||
int[] actual = new int[length];
|
||||
|
||||
for (int i = 0; i < actual.Length; i++)
|
||||
{
|
||||
actual[i] = (i * 17 % 23) - 11;
|
||||
}
|
||||
|
||||
int[] expected = (int[])actual.Clone();
|
||||
|
||||
for (int i = 0; i < expected.Length; i++)
|
||||
{
|
||||
if (expected[i] > 2)
|
||||
{
|
||||
expected[i]++;
|
||||
}
|
||||
}
|
||||
|
||||
ConditionalIncrement.IncrementAbove(actual, 2);
|
||||
|
||||
if (!actual.AsSpan().SequenceEqual(expected))
|
||||
{
|
||||
throw new InvalidOperationException($"Incorrect result for length {length}.");
|
||||
}
|
||||
}
|
||||
|
||||
Console.WriteLine("PASS");
|
||||
@@ -0,0 +1,18 @@
|
||||
public static class Product
|
||||
{
|
||||
public static int Calculate(ReadOnlySpan<int> values)
|
||||
{
|
||||
if (values.IsEmpty)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
int product = 1;
|
||||
foreach (int value in values)
|
||||
{
|
||||
product *= value;
|
||||
}
|
||||
|
||||
return product;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net10.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
<ItemGroup>
|
||||
<PackageReference Include="System.Numerics.Tensors" Version="10.0.0" />
|
||||
</ItemGroup>
|
||||
</Project>
|
||||
@@ -0,0 +1,12 @@
|
||||
AssertEqual(0, Product.Calculate([]));
|
||||
AssertEqual(24, Product.Calculate([2, 3, 4]));
|
||||
AssertEqual(-2, Product.Calculate([int.MaxValue, 2]));
|
||||
Console.WriteLine("PASS");
|
||||
|
||||
static void AssertEqual(int expected, int actual)
|
||||
{
|
||||
if (expected != actual)
|
||||
{
|
||||
throw new InvalidOperationException($"Expected {expected}, got {actual}.");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
int[] lengths = [0, 1, 3, 4, 5, 7, 8, 15, 16, 17, 31, 32, 33, 127, 128, 129, 1024];
|
||||
|
||||
foreach (int length in lengths)
|
||||
{
|
||||
int[] values = new int[length];
|
||||
|
||||
for (int i = 0; i < values.Length; i++)
|
||||
{
|
||||
values[i] = (i * 31 % 47) + 1;
|
||||
}
|
||||
|
||||
int expected = 0;
|
||||
|
||||
foreach (int value in values)
|
||||
{
|
||||
expected = unchecked(expected + value);
|
||||
}
|
||||
|
||||
int actual = SumValues.Sum(values);
|
||||
|
||||
if (actual != expected)
|
||||
{
|
||||
throw new InvalidOperationException(
|
||||
$"Incorrect result for length {length}: expected {expected}, got {actual}.");
|
||||
}
|
||||
}
|
||||
|
||||
int[] overflowing = [int.MaxValue, int.MaxValue, 2, 1];
|
||||
int overflowExpected = 0;
|
||||
|
||||
foreach (int value in overflowing)
|
||||
{
|
||||
overflowExpected = unchecked(overflowExpected + value);
|
||||
}
|
||||
|
||||
if (SumValues.Sum(overflowing) != overflowExpected)
|
||||
{
|
||||
throw new InvalidOperationException("Unchecked overflow behavior changed.");
|
||||
}
|
||||
|
||||
Console.WriteLine("PASS");
|
||||
@@ -0,0 +1,34 @@
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Runtime.Intrinsics;
|
||||
|
||||
public static class SumValues
|
||||
{
|
||||
public static int Sum(ReadOnlySpan<int> values)
|
||||
{
|
||||
if (values.Length < Vector128<int>.Count)
|
||||
{
|
||||
int scalar = 0;
|
||||
|
||||
foreach (int value in values)
|
||||
{
|
||||
scalar += value;
|
||||
}
|
||||
|
||||
return scalar;
|
||||
}
|
||||
|
||||
ref int start = ref MemoryMarshal.GetReference(values);
|
||||
Vector128<int> sum = Vector128<int>.Zero;
|
||||
nuint i = 0;
|
||||
|
||||
for (; i + (nuint)Vector128<int>.Count <= (nuint)values.Length; i += (nuint)Vector128<int>.Count)
|
||||
{
|
||||
sum += Vector128.LoadUnsafe(ref start, i);
|
||||
}
|
||||
|
||||
nuint last = (nuint)(values.Length - Vector128<int>.Count);
|
||||
sum += Vector128.LoadUnsafe(ref start, last);
|
||||
|
||||
return Vector128.Sum(sum);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net10.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
</Project>
|
||||
@@ -0,0 +1,54 @@
|
||||
using System.Runtime.Intrinsics;
|
||||
|
||||
public static class ClampNegative
|
||||
{
|
||||
public static void ToZero(Span<int> values)
|
||||
{
|
||||
if (Vector128.IsHardwareAccelerated)
|
||||
{
|
||||
if (values.Length >= Vector128<int>.Count)
|
||||
{
|
||||
ToZeroVector128(values);
|
||||
}
|
||||
else
|
||||
{
|
||||
ToZeroSmall(values);
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
ToZeroScalar(values);
|
||||
}
|
||||
|
||||
private static void ToZeroVector128(Span<int> values)
|
||||
{
|
||||
Vector128<int> zero = Vector128<int>.Zero;
|
||||
Span<int> remaining = values;
|
||||
|
||||
while (remaining.Length >= Vector128<int>.Count)
|
||||
{
|
||||
Vector128.Max(Vector128.Create<int>(remaining), zero).CopyTo(remaining);
|
||||
remaining = remaining.Slice(Vector128<int>.Count);
|
||||
}
|
||||
|
||||
if (!remaining.IsEmpty)
|
||||
{
|
||||
Span<int> tail = values.Slice(values.Length - Vector128<int>.Count);
|
||||
Vector128.Max(Vector128.Create<int>(tail), zero).CopyTo(tail);
|
||||
}
|
||||
}
|
||||
|
||||
private static void ToZeroScalar(Span<int> values)
|
||||
{
|
||||
for (int i = 0; i < values.Length; i++)
|
||||
{
|
||||
values[i] = Math.Max(values[i], 0);
|
||||
}
|
||||
}
|
||||
|
||||
private static void ToZeroSmall(Span<int> values)
|
||||
{
|
||||
ToZeroScalar(values);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
int[] lengths = [0, 1, 3, 4, 5, 7, 8, 9, 15, 16, 17, 31, 32, 33, 127, 128, 129, 1024];
|
||||
|
||||
foreach (int length in lengths)
|
||||
{
|
||||
int[] actual = new int[length];
|
||||
|
||||
for (int i = 0; i < actual.Length; i++)
|
||||
{
|
||||
actual[i] = (i * 17 % 23) - 11;
|
||||
}
|
||||
|
||||
int[] expected = (int[])actual.Clone();
|
||||
|
||||
for (int i = 0; i < expected.Length; i++)
|
||||
{
|
||||
expected[i] = Math.Max(expected[i], 0);
|
||||
}
|
||||
|
||||
ClampNegative.ToZero(actual);
|
||||
|
||||
if (!actual.AsSpan().SequenceEqual(expected))
|
||||
{
|
||||
throw new InvalidOperationException($"Incorrect result for length {length}.");
|
||||
}
|
||||
}
|
||||
|
||||
Console.WriteLine("PASS");
|
||||
@@ -0,0 +1,8 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net10.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
</Project>
|
||||
@@ -1,86 +0,0 @@
|
||||
name: exp-simd-vectorization
|
||||
description: Evaluates the dotnet-experimental/exp-simd-vectorization skill
|
||||
type: capability
|
||||
config:
|
||||
timeout: 3m
|
||||
stimuli:
|
||||
- name: Optimize manual min/max with TensorPrimitives
|
||||
prompt: This .NET 10 project has a method that finds the minimum and maximum values in a float array. It's used in a
|
||||
real-time sensor monitoring system to compute value ranges over sliding windows of 10K–100K readings. Please
|
||||
optimize FindMinMax for maximum throughput.
|
||||
environment:
|
||||
files:
|
||||
- src: fixtures/simd-tensor-primitives-minmax.cs
|
||||
dest: RangeCalculator.cs
|
||||
graders:
|
||||
- type: exit-success
|
||||
- type: prompt
|
||||
rubric:
|
||||
- Identifies that finding min and max of a float span is already covered by TensorPrimitives.Min and
|
||||
TensorPrimitives.Max
|
||||
- Replaces the scalar loop with TensorPrimitives.Min/Max or equivalent framework API — not manual SIMD
|
||||
- Does not introduce Vector128/Vector256/Vector512 intrinsics for operations already optimized by TensorPrimitives
|
||||
- name: Optimize manual product with TensorPrimitives
|
||||
prompt: This .NET 10 project computes the product of all elements in a float array for probability calculations. Arrays
|
||||
are 100-1000 elements. Please optimize Product for maximum throughput.
|
||||
environment:
|
||||
files:
|
||||
- src: fixtures/simd-tensor-primitives-product.cs
|
||||
dest: MathOps.cs
|
||||
graders:
|
||||
- type: exit-success
|
||||
- type: prompt
|
||||
rubric:
|
||||
- Identifies that aggregate product is already covered by TensorPrimitives.Product
|
||||
- Replaces the scalar loop with TensorPrimitives.Product — not manual SIMD
|
||||
- Does not introduce Vector128/Vector256/Vector512 intrinsics for an operation already optimized by
|
||||
TensorPrimitives
|
||||
- name: No optimization opportunity — dictionary-based lookup service
|
||||
prompt: This .NET 10 project is a product catalog lookup service that maps product codes to categories using
|
||||
dictionaries. The development team suspects it could benefit from SIMD optimization. Please analyze and optimize
|
||||
for maximum throughput.
|
||||
environment:
|
||||
files:
|
||||
- src: fixtures/simd-no-opportunity-catalog.cs
|
||||
dest: ProductCatalog.cs
|
||||
graders:
|
||||
- type: exit-success
|
||||
- type: prompt
|
||||
rubric:
|
||||
- Correctly identifies that this code has no meaningful SIMD optimization opportunity
|
||||
- Explains why SIMD is not applicable (dictionary lookups, hash-based operations, small string keys, branching
|
||||
logic)
|
||||
- Does not introduce unnecessary SIMD code that adds complexity without benefit
|
||||
- name: Optimize int array conditional increment with SIMD
|
||||
prompt: This .NET 10 project conditionally increments each element of an int array based on a threshold for counter
|
||||
updates. Arrays are 100K+ elements. Please optimize ConditionalIncrement.IncrementAbove for maximum throughput.
|
||||
environment:
|
||||
files:
|
||||
- src: fixtures/simd-conditional-increment.cs
|
||||
dest: ConditionalIncrement.cs
|
||||
graders:
|
||||
- type: output-contains
|
||||
config:
|
||||
substring: Vector
|
||||
- type: exit-success
|
||||
- type: prompt
|
||||
rubric:
|
||||
- Introduces Vector128/Vector256/Vector512 with GreaterThan comparison mask and ConditionalSelect or masked Add
|
||||
for conditional increment
|
||||
- Preserves scalar fallback for trailing elements or non-accelerated hardware
|
||||
- name: Optimize byte buffer bit reversal with SIMD
|
||||
prompt: This .NET 10 project reverses the bit order of each byte in a buffer for serial protocol decoding. Buffers are
|
||||
64KB+. Please optimize BitReverser.ReverseInPlace for maximum throughput.
|
||||
environment:
|
||||
files:
|
||||
- src: fixtures/simd-bit-reverser.cs
|
||||
dest: BitReverser.cs
|
||||
graders:
|
||||
- type: output-contains
|
||||
config:
|
||||
substring: Vector
|
||||
- type: exit-success
|
||||
- type: prompt
|
||||
rubric:
|
||||
- Introduces Vector128/Vector256/Vector512 with nibble-based Shuffle lookup tables for parallel bit reversal
|
||||
- Preserves scalar fallback for trailing elements or non-accelerated hardware
|
||||
@@ -1,16 +0,0 @@
|
||||
using System;
|
||||
|
||||
namespace ByteBitReverse;
|
||||
|
||||
public class BitReverser
|
||||
{
|
||||
public static void ReverseInPlace(Span<byte> data)
|
||||
{
|
||||
for (int i = 0; i < data.Length; i++)
|
||||
{
|
||||
byte b = data[i];
|
||||
b = (byte)((b * 0x0202020202UL & 0x010884422010UL) % 1023);
|
||||
data[i] = b;
|
||||
}
|
||||
}
|
||||
}
|
||||
-38
@@ -1,38 +0,0 @@
|
||||
namespace CatalogSvc;
|
||||
|
||||
public class ProductCatalog
|
||||
{
|
||||
private readonly Dictionary<string, string> _categories = new(StringComparer.OrdinalIgnoreCase);
|
||||
private readonly Dictionary<string, decimal> _prices = new(StringComparer.OrdinalIgnoreCase);
|
||||
|
||||
public void AddProduct(string code, string category, decimal price)
|
||||
{
|
||||
_categories[code] = category;
|
||||
_prices[code] = price;
|
||||
}
|
||||
|
||||
public string? GetCategory(string code)
|
||||
=> _categories.TryGetValue(code, out var cat) ? cat : null;
|
||||
|
||||
public decimal GetTotalPrice(IEnumerable<string> codes)
|
||||
{
|
||||
decimal total = 0m;
|
||||
foreach (var code in codes)
|
||||
{
|
||||
if (_prices.TryGetValue(code, out var price))
|
||||
total += price;
|
||||
}
|
||||
return total;
|
||||
}
|
||||
|
||||
public List<string> GetProductsByCategory(string category)
|
||||
{
|
||||
var result = new List<string>();
|
||||
foreach (var kvp in _categories)
|
||||
{
|
||||
if (kvp.Value.Equals(category, StringComparison.OrdinalIgnoreCase))
|
||||
result.Add(kvp.Key);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
||||
-22
@@ -1,22 +0,0 @@
|
||||
namespace MinMax;
|
||||
|
||||
public static class RangeCalculator
|
||||
{
|
||||
/// <summary>
|
||||
/// Returns the minimum and maximum values in <paramref name="values"/>.
|
||||
/// </summary>
|
||||
public static (float Min, float Max) FindMinMax(ReadOnlySpan<float> values)
|
||||
{
|
||||
if (values.Length == 0)
|
||||
throw new ArgumentException("Span must not be empty.");
|
||||
|
||||
float min = values[0];
|
||||
float max = values[0];
|
||||
for (int i = 1; i < values.Length; i++)
|
||||
{
|
||||
if (values[i] < min) min = values[i];
|
||||
if (values[i] > max) max = values[i];
|
||||
}
|
||||
return (min, max);
|
||||
}
|
||||
}
|
||||
-14
@@ -1,14 +0,0 @@
|
||||
namespace ProdOp;
|
||||
|
||||
public static class MathOps
|
||||
{
|
||||
/// <summary>Computes the product of all elements.</summary>
|
||||
public static float Product(ReadOnlySpan<float> values)
|
||||
{
|
||||
if (values.IsEmpty) return 0f;
|
||||
float product = 1f;
|
||||
for (int i = 0; i < values.Length; i++)
|
||||
product *= values[i];
|
||||
return product;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user