Cpp hpc optimization

From archibate/dotfiles-claude · View interactive version

Cpp hpc optimization is an AI agent skill from the GitHub repository archibate/dotfiles-claude. Evidence-driven C++ high-performance computing design, profiling, and optimization across algorithmic time/space complexity, data layout, cache and memory behavior, numerics, SIMD, multicore scheduling, and CPU/accelerator pipelines. Use before designing or changing computation-intensive kernels, hi

How to use this skill

Skill file

C++ HPC Optimization

Optimize the measured data path, not the code that merely looks low-level.

Preserve a simple reference implementation and make every optimization earn its

complexity with correctness evidence and a representative benchmark.

Non-negotiable rules

  1. Define the contract first. Record inputs, sizes, distributions, target

hardware, accuracy, determinism, memory, latency, and throughput objectives.

  1. Pass the complexity gate. Compare the best practical algorithmic families

for the actual size range before tuning constants inside one family.

  1. Profile before kernel redesign. Identify the hot path with end-to-end evidence.
  2. Keep an oracle. Retain a scalar or otherwise obviously-correct reference

implementation and compare every optimized path against it.

  1. Model before tuning. Estimate useful operations, bytes transferred,

working set, dependency depth, synchronization, and launch overhead.

  1. Change one axis at a time. Measure after each transformation and revert

changes that do not win on the target workload.

  1. Never encode a benchmark accident as a law. Cache sizes, SIMD width,

thread count, tile size, padding, and crossover thresholds are target- and

workload-dependent.

  1. Make ISA requirements explicit. Use feature dispatch and a compatible

fallback when targets vary, or a verified fixed ISA for controlled deployments.

Both require tail handling and equivalence tests.

Workflow

1. Frame the performance contract

Write down:

are permitted.

Do not optimize an unspecified target. Throughput and latency often demand

opposite choices: batching and deep queues improve throughput but add waiting.

2. Pass the algorithmic time/space complexity gate

Before applying cache, SIMD, threading, or instruction-level techniques:

the declared sizes, distributions, sparsity, dimensionality, accuracy,

update/query ratio, and number of timesteps or solves;

behavior, worst/expected cases, and implementation maturity;

different algorithms win in different regimes.

An asymptotically better method may lose at small n; an asymptotically worse

regular kernel may exploit the target exceptionally well. Conversely, no amount

of constant-factor kernel tuning rescues an avoidable complexity class at scale.

Choose the industrially useful family for the declared range first, then use the

rest of this skill to reduce its realized cost. Preserve the declared semantic or

numerical contract; optimization need not be bit-exact when the contract permits

an approximate, reordered, or mixed-precision algorithm.

3. Establish correctness and measurement baselines

math, or concurrency.

NaN/Inf, sparse/dense, and aliasing cases that the contract permits.

dead-code elimination, report distribution rather than one lucky sample, and

retain end-to-end measurements beside microbenchmarks.

compiler failed.

Read references/profiling-and-cost-model.md before profiling, benchmarking,

or declaring a bottleneck.

4. Classify the limiting resource

Calculate at least an approximate per-item model:

Classify the current regime as memory-bandwidth, cache-capacity/latency,

compute/issue, dependency-latency, synchronization, task-granularity, launch, or

transfer bound. A kernel can move between regimes after each optimization.

5. Evaluate change scope and ROI

Record the currently authorized edit boundary, but do not mistake it for the

root-cause boundary. When evidence places the limiting work upstream or in data

representation, ownership, or construction, compare:

For each candidate, estimate end-to-end speedup, complexity and crossover,

memory, implementation and migration work, write amplification, invariant and

test surface, and ongoing maintenance. If wider scope has credible net value,

present the evidence and obtain user approval before expanding it. Keep the

experiment reversible, measure the representative full workflow, and retain the

wider change only when its observed performance and complexity benefits repay

its lifecycle cost; otherwise revert it.

6. Apply transformations in economic order

Prefer the first measured transformation that attacks the current limit:

  1. Remove unnecessary work, copies, allocation, conversion, and materialization.
  2. Reduce footprint and improve data layout or traversal locality.
  3. Fuse passes or tile/block computation to reuse data before eviction.
  4. Expose compiler optimization by removing false alias/dependency barriers.
  5. Vectorize, using intrinsics only when generated code proves they are needed.
  6. Parallelize with enough work per task and no shared hot write locations.
  7. Batch, pipeline, or overlap copies and computation when latency can be hidden.
  8. Offload only when transfer, launch, and synchronization costs fit the model.

Load the relevant references before choosing:

sparse data, locality, tiling, fusion, and Morton order.

bottlenecks, preallocation, PMR, arenas, pools, TLS scratch, general-purpose

allocators, NUMA, and allocator benchmarks.

encodings, sidecar topology, packed sparse sets, polygon-corner topology,

aligned attributes, and scan-built output.

exponent, quantization, accumulation, and numerical validation.

intrinsics, SIMD tricks, tails, and ISA dispatch.

false sharing, work stealing, queues, async launch, and pipelines.

streams, occupancy, synchronization, and launch amortization.

and data-oriented polymorphism.

7. Validate and integrate

often lose below a threshold.

alternatives near the benchmark or design documentation.

layout or ISA details across the rest of the system.

Relationship to C++ OOP design

Apply $cpp-oop-style to ownership, orchestration, I/O, error handling, and

module boundaries. Inside a measured hot kernel, prefer flat data, value views,

batch operations, static dispatch, and explicit SIMD when evidence requires it.

Use its debug-instrumentation guidance for correctness and runtime diagnosis;

this skill owns profiling, tracing, counters, and benchmarks used to locate or

quantify a performance bottleneck.

The boundary should normally look like:

entering the hot/data side;

Read references/hotpath-polymorphism.md before removing abstractions or adding

type tags. Virtual dispatch is rarely the largest cost by itself; the lost

inlining, scattered objects, unpredictable branches, and pointer-chasing around

it are often the actual problem.

Source material and provenance

Read references/parallel101-case-studies.md when looking for concrete lesson

progressions or deeper examples from archibate's parallel101/course and

parallel101/simdtutor repositories. Search the bundled offline corpus under

references/parallel101/ before depending on a maintainer's checkout or the

network. Use references/parallel101/provenance.tsv to recover the repository,

author, source URL, commit, original path, teaching classification, license, and

SHA-256 for every excerpt.

Treat the corpus as educational experiments, not production code. The case-study

index marks outdated, unsafe, incomplete, or broken examples so their ideas can

be learned without copying their defects. The skill and bundled corpus are

licensed under CC BY-NC-SA 4.0; retain attribution and compatible terms when

redistributing adaptations.

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.