Skip to content

world_clock: one clock for every simulation (lockstep/batch, followers, state-tree binding, sim_thread on the clock) - #629

Merged
transfix merged 7 commits into
masterfrom
feat/world-clock-followers
Oct 15, 2026
Merged

transfix merged 7 commits into
masterfrom
feat/world-clock-followers

Conversation

@transfix

Copy link
Copy Markdown
Owner

What

One cvc::world_clock mechanism for every simulation: one driver advances the clock, every simulation follows it at its own whole-quantum period, and speed, pause, mode and single-step live on the state tree. This is the first step of docs/roadmap/WORLD-CLOCK-SYNC-ROADMAP.md, which migrates every demo and simulation loop onto the clock. It is also the prerequisite for a physics engine: a physics tick without a fixed quantum shared with the navigator cannot be deterministic.

cvc::world_clock

  • lockstep mode: exactly lockstep_steps quanta per advance(), whatever the wall says (frame-by-frame inspection).
  • advance_ticks(n): batch; exactly n quanta now, any mode, no cap.
  • configure(config) / current_config(): a host picks a quantum every simulation divides, before any of them starts. It restarts at tick 0 and keeps the mode.
  • step_result::first_tick: the quanta a call ran are first_tick + 1 … first_tick + steps.
  • realized_scale(): world seconds actually simulated per wall second over a ≥ 0.5 s window. A speed the machine cannot deliver reads below the request instead of quietly running slower.
  • cap_scales_with_rate: the live cap grows with scale, so asking for 2x is not silently capped at what a 1x-sized cap allows. Off by default.
  • wait_for_tick_change(): a follower thread blocks until the driver moves the clock.

cvc/core/clock_follower.h

  • ticks_per_step(dt, fixed_dt) refuses a step that is not a whole number of quanta (a 0.06 s navigator step on a 1/120 s clock would be 7.2). It tolerates a float32 trained dt.
  • steps_due and clock_follower (take, due_at) let any number of simulations follow one clock at their own periods.
  • alpha is the presentation fraction into a follower's own step.

cvc::world_clock_binding

The clock on the state tree under a prefix (default sim: the keys Ariadne's sim_transport.ari already binds).

  • Controls: speed, paused, mode, lockstep_steps, and step (a request counter).
  • Telemetry: time, tick, fixed_dt, realized_speed, dropped.
  • The driver's one call: drive(wall_dt) pulls the controls, advances, runs requested steps and pushes the telemetry.
  • Robustness: unknown values are ignored, never thrown into a frame.

nav::sim_thread

  • The shared-clock form: sim_thread(world, clock, options) follows a shared clock, one navigator step every veh.dt / fixed_dt quanta. It waits on the clock instead of polling. Pausing one simulation leaves the shared clock alone.
  • The original sim_thread(world, hz): it now drives a private clock at hz * veh.dt. Its behaviour is unchanged, and stalls are counted in behind().

Also in this PR

  • pycvc wraps all of the above: the lockstep mode name, set_config, advance_ticks, realized_scale, ticks_per_step, clock_follower, and world_clock_binding(app, clock[, prefix]).
  • Docs: docs/WORLD_CLOCK_API.md, the sync roadmap, and an update note in NAV_UNITS_ROADMAP.md (a nav step is k quanta; simulation hosts use 0.01 s).

The contract, pinned by tests

Pacing decides when quanta run, never which.

  • ClockFollowerTest.TheTrajectoryIsTheSameUnderEveryPacingMode runs one follower under seven pacings and asserts identical traces: live 1x, 2x and 0.5x, lockstep, batch, and single-step.
  • NavSimThread.AFollowerMatchesDirectSteppingExactly checks that a sim_world stepped through a threaded follower in uneven clock batches matches direct stepping bit for bit.

Tests

Suite What
world_clock_test +18 cases: lockstep, batch, first_tick, configure, realized speed, the rate-scaled cap, the wait, followers, steps_due (brute-force sweep), alpha, pacing invariance
app_test +5 binding cases: seeding, speed/pause/mode from the tree, step requests, no replay of pre-existing requests, the shared Ariadne keys
nav_test +3 sim_thread cases: follows a shared clock at period 6, refuses 7.2 quanta, matches direct stepping exactly
test_pycvc_world_clock.py +4 cases

Nav kernels are untouched (they never see the clock), so the goldens do not move.

Verified

Windows (MSVC 2022, Ninja). Built against a local cvcpkg prefix: install-deps cvcpkg/recipes/libcvc, plus cmake, ninja, googletest, python311, numpy-cp311 and swig from cvcpkg.

  • world_clock_test: 49/49.
  • app_test clock and binding cases: 7/7.
  • system_macro_headers_test: passes.
  • nav_test: 63/63, including the 4 NavSimThread cases.
  • test_pycvc_world_clock.py: all 14 against the freshly built _pycvc.pyd.

Linux (gcc 11). An earlier revision of the core clock passed world_clock_test and the binding cases.

Two things the local run caught and this PR fixes:

  • A start-up race. sim_thread re-anchored its follower on the worker thread, so a clock advanced right after start() could lose steps. It now anchors in start().
  • A SWIG keep-alive. It was dropped when the binding's constructor overloads merged.

CI does not build pycvc, so the Python results above are the only check on the bindings.

transfix added a commit to cy-pca/cvcpkg that referenced this pull request Oct 14, 2026
clang-format had only linux and macos columns, so Windows hosts had no clang-format 18 to match
CI format gates. libcvc's CI pins 18. Visual Studio ships 19.x, whose output differs.

- build.ps1 (new) mirrors build.sh with MSVC + Ninja: only the clang-format target, static LLVM
  and clang libraries, and a static CRT (CMAKE_MSVC_RUNTIME_LIBRARY=MultiThreaded, the counterpart
  of -static-libstdc++), with LLVM_ENABLE_DIA_SDK=OFF as in llvm18. It runs the same smoke test:
  version 18.x, plus formatting a snippet, tolerating CRLF.
- package.files: bin/clang-format scoped to linux/macos, bin/clang-format.exe on windows; the two
  helper scripts are shared. A note covers running git-clang-format through an interpreter on
  Windows.
- Same revision. No windows bundle existed at any revision, and linux/macos do not change, so
  populate builds only the missing windows variant.

Verified locally on Windows (install-deps + pack --strict-globs, as windows-build.yml runs them):
- packed clang-format-18.1.8+cvc.1-windows-x86_64-release-shared.zip (1.5 MB);
- clang-format.exe imports only KERNEL32 and ADVAPI32;
- `git-clang-format --diff` with it reports a real change set (transfix/libcvc#629) clean.
@transfix
transfix merged commit 962a8e7 into master Oct 15, 2026
15 checks passed
@transfix
transfix deleted the feat/world-clock-followers branch October 15, 2026 01:42
transfix added a commit to CVC-Lab/GRL-SNAM that referenced this pull request Oct 15, 2026
…ch, followers) (#116)

* clock: parity with cvc::world_clock's shared-clock API

Brings grl_snam.clock.WorldClock to parity with libcvc's one-clock-for-every-simulation work
(transfix/libcvc#629):
- lockstep mode;
- advance_ticks (batch);
- configure;
- StepResult.first_tick;
- realized_scale;
- cap_scales_with_rate.

Adds ticks_per_step, steps_due and ClockFollower (take/due_at/alpha), so a Python simulation follows
a shared clock at its own whole-quantum period, as a C++ one does. All additions keep the existing
API and behaviour.

tests/test_clock.py mirrors the C++ cases (pacing invariance included) and checks tick-for-tick
parity with pycvc.world_clock where pycvc is installed. 32 tests pass locally (Windows, cvcpkg
python311 + torch-cp311, pycvc built from libcvc#629), along with the existing WorldClock tests in
test_fog_demo.py.

* tests/test_clock.py: black
@transfix
transfix restored the feat/world-clock-followers branch October 15, 2026 01:44
@transfix
transfix deleted the feat/world-clock-followers branch October 15, 2026 01:45
transfix added a commit that referenced this pull request Oct 17, 2026
Minor release. It carries:
- the world clock: lockstep and batch, followers, the sim.* binding, and nav
  on the clock (#629, #630, #631, #640);
- the units registry with SI <-> normalized-frame conversions (#632);
- cvc::physics with the Jolt backend (#641);
- the material trainer on the kinematic bicycle and its pycvc bindings
  (#642).
Bumps project(VERSION) 3.5.1 -> 3.6.0 and every cvcpkg recipe's
upstream_version to match. cvc_revision stays 1, per the recipe-version gate.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant