Simulation dev

From spiriMirror/libuipc · View interactive version

Simulation dev is an AI agent skill from the GitHub repository spiriMirror/libuipc. General simulation development best practices for correctness, stability, and debuggability. Use when implementing or modifying simulation systems, solvers, constraints, or GPU kernels, especially for index safety, NaN/Inf issues, and diagnostics.

How to use this skill

Skill file

Simulation Development Best Practices

General rules for building simulation code that is correct first, debuggable second, and fast third.

Follow the owner working agreements, especially CUDA rules 9-16.

MUDA is removed; do not restore its dependency, wrappers, or compatibility layer.

When to Use

Use this skill when:

Core Principles

  1. Correctness before performance.
  2. Fail fast on invalid states.
  3. Reproduce with fixed inputs while preserving parallel execution.
  4. Use named raw-CUDA kernels and the in-tree cuda_tool diagnostics.
  5. Validate data at subsystem boundaries.
  6. Instrument first, optimize after evidence.

Safety Rules (Always Apply)

Input and shape validation

Index safety for GPU/parallel code

Numerical robustness

Assertion Best Practices

Every assertion should answer:

  1. What failed?
  2. Where in the pipeline?
  3. With what values?
  4. What is the likely cause?

Checklist for high-quality assert messages:

Diagnostics and Observability

src/backends/cuda/cuda_tool/debug.h; kernel assertions follow uipc::RUNTIME_CHECK.

launches so logs and profiler symbols identify the owning subsystem.

buffers; pass explicit counts/offsets and report their values on failure.

iteration context. Do not use removed .name() or file_line() wrappers.

dumps and synchronization so the normal path retains its existing overhead.

CUDA Debug Checklist

For each touched GPU code path:

remove or gate these barriers after diagnosis.

(resize, resize_discard, resize_preserve).

per-kernel-address cache.

Reproduction Workflow

  1. Reproduce on the smallest failing case with fixed inputs and random seeds.
  2. Compare isolated runs with the single-process aggregate to detect global-state

pollution; preserve existing concurrent assembly and solver paths.

  1. Enable strict checks and locate the first invalid value or missing dependency.
  2. Bisect by pipeline stage (assemble -> solve -> update).
  3. Distinguish an algorithmic error or synchronization defect from rounding due

solely to valid atomic-add arrival order. Do not add sorting, fixed-order

reductions or serialization merely to obtain bitwise-identical trajectories.

Simulation Debug Loop (Iterative)

Use this loop as the default process for debugging simulation issues:

  1. Build a minimal reproducible scene/case that is easy to debug.
  2. Use diagnostics to understand behavior, form a concrete assumption, then add debug code to validate that assumption.
  3. After validating the assumption, implement the fix in the minimal case first.
  4. Re-run the original scene/case to verify the real issue is resolved.
  5. Add a regression test for the confirmed fix.

If any step fails or new evidence contradicts the assumption, return to step 1 or 2 and iterate until root cause and fix are both confirmed.

During active GPU debugging, insert cuda_tool::debug_sync_all() at key boundaries to stop immediately when a kernel fails, then remove or gate it once diagnosis is complete.

Convergence and Stability Guidelines

Change Workflow

  1. Write/modify logic with invariants first.
  2. Add assertions at boundaries and first-use sites.
  3. Add diagnostics (timers, buffer names, optional dumps).
  4. Verify on:
  1. Only then profile and optimize.
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.