From e1b731f4446e52f6fe72900f020a47859606afec Mon Sep 17 00:00:00 2001 From: Claude Date: Sat, 14 Nov 2026 00:29:08 +0000 Subject: [PATCH] feat(v4): Phase 7 Task 3b -- compudynamics on the 32 F18 opcodes as on the words, behind OPCODE_PHYSICS (#189) Captain Bob, 2026-11-14: "we're supposed to be working on f18 opcode as well as our composed f18 dictionary"; ruled every loop on both, "and an easy revert if opcodes are too granular". - The engine: a hook at retire (exec.h on_retire), as Part A's at call; NULL unless set. test_exec.c. - The mesh: 32 opcode records per node, an opcode window, opcode transitions under L4; at every retired instruction decay at the opcodes' own slope, heat, temperature, the window, the transition; in each cycle all 32 cooled, the opcode window's service, inference on its heat. L8 scores from the words and its gates govern both; the tick stays paced by word calls. The window is now one type for words and opcodes. - The revert: OPCODE_PHYSICS (Kconfig.physics, default y; the kernel's flags) -- 0 and the opcodes are counted and charged to the Stadium as in Phase 1. make -C v4 mesh-boot-noop builds and tests it. - Tests first: test_mesh_boot.c 77 checks as built, 86 with pipelining on, 74 with the revert. - Measured on amd64, back to back: 219 s on, 196 s off (+12% under TCG). Both boots' logs labelled. Not pushed (held to the end of Phase 7). Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_017RzZgrf7V7Zk2iczBXDRbm --- Kconfig.physics | 11 ++ ...ngine-step4-phase7-the-loops-and-parity.md | 22 +++ .../runs/doe-amd64-20261114-002054.csv | 0 .../runs/doe-amd64-20261114-002519.csv | 0 kernel/Makefile | 3 +- logs/20261114-002054/amd64/README.md | 1 + .../amd64/qemu-amd64-20261114-002054.log | 3 + logs/20261114-002519/amd64/README.md | 1 + .../amd64/qemu-amd64-20261114-002519.log | 3 + v4/Makefile | 12 +- v4/include/v4/exec.h | 6 + v4/include/v4/mesh.h | 43 ++++- v4/src/exec.c | 3 + v4/system/mesh.c | 170 +++++++++++++----- v4/tests/test_exec.c | 29 +++ v4/tests/test_mesh_boot.c | 46 ++++- 16 files changed, 296 insertions(+), 57 deletions(-) create mode 100644 experiments/bare_metal/runs/doe-amd64-20261114-002054.csv create mode 100644 experiments/bare_metal/runs/doe-amd64-20261114-002519.csv create mode 100644 logs/20261114-002054/amd64/README.md create mode 100644 logs/20261114-002054/amd64/qemu-amd64-20261114-002054.log create mode 100644 logs/20261114-002519/amd64/README.md create mode 100644 logs/20261114-002519/amd64/qemu-amd64-20261114-002519.log diff --git a/Kconfig.physics b/Kconfig.physics index d87ef96e..4dca465e 100644 --- a/Kconfig.physics +++ b/Kconfig.physics @@ -20,6 +20,17 @@ config ENABLE_HOTWORDS_CACHE migration exists to collapse to one source of truth. Set to y to enable; also gates L8 Jacquard's L1_heat_tracking bit. +config OPCODE_PHYSICS + bool "Compudynamics on the 32 F18 opcodes as well as the words (OPCODE_PHYSICS)" + default y + help + #189 Phase 7, ruling 7 (Captain Bob, 2026-11-14): every loop -- heat, + decay, temperature, the rolling window, pipelining, inference -- runs + on a v4 node's 32 opcodes, fed at every retired instruction, as on its + composed words. "And an easy revert if opcodes are too granular": this + is it. Off, the opcodes are counted (HEAT-OP) and charged to the + Stadium as in Phase 1, and nothing more. + config ENABLE_PIPELINING bool "Speculative execution via word transition prediction (ENABLE_PIPELINING)" default n diff --git a/docs/v4.0.0/plans/2026-11-13-engine-step4-phase7-the-loops-and-parity.md b/docs/v4.0.0/plans/2026-11-13-engine-step4-phase7-the-loops-and-parity.md index b46d1cca..79cad379 100644 --- a/docs/v4.0.0/plans/2026-11-13-engine-step4-phase7-the-loops-and-parity.md +++ b/docs/v4.0.0/plans/2026-11-13-engine-step4-phase7-the-loops-and-parity.md @@ -37,6 +37,17 @@ `Kconfig.physics` and `kernel/Makefile`), so v3's boot records no transitions and uses no cache. Ruled: both written and tested, behind the same two build options, off by default as in v3. +7. **Compudynamics on the F18 opcodes as well as the composed words** (2026-11-14: "we're supposed to be + working on f18 opcode as well as our composed f18 dictionary"; ruled "#1 and an easy revert if opcodes + are too granular"). Every loop runs on both: the 32 opcodes are a second set of records on each node, + fed at every retired instruction (a hook at retire, as Part A's at `call`) -- heat, decay, temperature, + an opcode window, opcode transitions (under L4, as the words'), background decay and inference in the + cycle. L8 scores from the words, as v3, and its card's gates govern both. The tick stays paced by word + calls, as v3's. **The revert:** one build option, `OPCODE_PHYSICS` (`Kconfig.physics`, default y); + off, the opcodes are counted and charged to the Stadium as in Phase 1, and nothing more. + **Where the arithmetic lives:** compudynamics is the Stadium/fabric's (2026-11-14: "The compudynamics + is stadium/fabric"): the kernel computes; a word that reads or sets it is F18 that asks the kernel. + ## What ruling 3 brings in v3's POST is 26 modules (`v3/src/test_runner/modules/`, about 1,050 cases; v3's bare-metal boot says @@ -146,6 +157,17 @@ Phase 4). - [x] The snapshot (v3's `heartbeat_publish_snapshot`): each cycle, the tick, window, slope and the dictionary's heat as inference counted it (v3's `*_at_check`). One copy: v3 double-buffers for its heartbeat thread, which v4 has no counterpart of. Test first (73 checks; 81 on). +### Task 3b: the loops on the 32 opcodes (ruling 7), behind `OPCODE_PHYSICS` -- measured +- [x] The engine: a hook at retire (`exec.h`, `on_retire`), as Part A's at `call`; test first (`test_exec.c`). +- [x] The mesh: 32 opcode records per node beside the words', an opcode window, opcode transitions (L4); + at every retire decay at the opcodes' own slope, heat, temperature, the window, the transition; in + the cycle all 32 cooled, the opcode window's service, inference on its heat (their own width and + slope). L8 scores from the words; its gates govern both. The window refactored to one type for both. + Test first, three builds: as built (77 checks), pipelining on (86), the revert `OPCODE_PHYSICS=0` (74). +- [x] `OPCODE_PHYSICS` in `Kconfig.physics` (default y) and the kernel's flags; `OPCODE_PHYSICS=0` checked + to reach the build (the revert's kernel has none of the opcode code). +- [x] Measured, back to back on amd64: 219 s on, 196 s off -- about +12% under TCG. Kept, as ruled; the + revert is the one switch. ### Task 4: the words that read and set them (`FREEZE-WORD`, the inference words, ...), and their POST ### Task 5: the StarForth words (Phase 3's: `init.4th`, `starforth.4th`), and their POST ### Task 6: the ACL (Phase 4's: `ACL.4th`, the word card, the countdown), and its POST diff --git a/experiments/bare_metal/runs/doe-amd64-20261114-002054.csv b/experiments/bare_metal/runs/doe-amd64-20261114-002054.csv new file mode 100644 index 00000000..e69de29b diff --git a/experiments/bare_metal/runs/doe-amd64-20261114-002519.csv b/experiments/bare_metal/runs/doe-amd64-20261114-002519.csv new file mode 100644 index 00000000..e69de29b diff --git a/kernel/Makefile b/kernel/Makefile index d40cb7e8..6f3942e0 100644 --- a/kernel/Makefile +++ b/kernel/Makefile @@ -471,6 +471,7 @@ ifeq ($(KCONFIG_ACTIVE),1) $(eval $(call kconfig_bool,STRICT_PTR,1)) $(eval $(call kconfig_bool,ENABLE_HOTWORDS_CACHE,0)) $(eval $(call kconfig_bool,ENABLE_PIPELINING,0)) +$(eval $(call kconfig_bool,OPCODE_PHYSICS,1)) $(eval $(call kconfig_int,ROLLING_WINDOW_SIZE,4096)) $(eval $(call kconfig_int,TRANSITION_WINDOW_SIZE,8)) $(eval $(call kconfig_int,ADAPTIVE_SHRINK_RATE,50)) @@ -524,7 +525,7 @@ $(eval $(call kconfig_bool,SK_PARITY_DEBUG,0)) endif VM_FEATURE_FLAG_VARS := \ - STRICT_PTR ENABLE_HOTWORDS_CACHE ENABLE_PIPELINING \ + STRICT_PTR ENABLE_HOTWORDS_CACHE ENABLE_PIPELINING OPCODE_PHYSICS \ ROLLING_WINDOW_SIZE TRANSITION_WINDOW_SIZE \ ADAPTIVE_SHRINK_RATE ADAPTIVE_MIN_WINDOW_SIZE \ ADAPTIVE_CHECK_FREQUENCY ADAPTIVE_GROWTH_THRESHOLD \ diff --git a/logs/20261114-002054/amd64/README.md b/logs/20261114-002054/amd64/README.md new file mode 100644 index 00000000..a7e848c5 --- /dev/null +++ b/logs/20261114-002054/amd64/README.md @@ -0,0 +1 @@ +Development boot, not acceptance evidence (2026-11-14): amd64 with #189 Phase 7 Task 3b -- compudynamics on the 32 opcodes at every retired instruction (OPCODE_PHYSICS on). RESULT: PASS; POST 539/539; CONSERVED; 219 s (196 s with it off, booted straight after). diff --git a/logs/20261114-002054/amd64/qemu-amd64-20261114-002054.log b/logs/20261114-002054/amd64/qemu-amd64-20261114-002054.log new file mode 100644 index 00000000..4a9ca20b --- /dev/null +++ b/logs/20261114-002054/amd64/qemu-amd64-20261114-002054.log @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:764ea831e2ae1ccdb67e8fb84b7153a9578f4b0a1a03189556963c5e1ba13775 +size 9473 diff --git a/logs/20261114-002519/amd64/README.md b/logs/20261114-002519/amd64/README.md new file mode 100644 index 00000000..3bfa0e87 --- /dev/null +++ b/logs/20261114-002519/amd64/README.md @@ -0,0 +1 @@ +Timing comparison, not acceptance evidence (2026-11-14): amd64 with OPCODE_PHYSICS=0 (the revert, #189 Phase 7 ruling 7), booted straight after the same tree with it on. RESULT: PASS in 196 s, against 219 s with opcode compudynamics on: about +12% for the loops at every retired instruction. diff --git a/logs/20261114-002519/amd64/qemu-amd64-20261114-002519.log b/logs/20261114-002519/amd64/qemu-amd64-20261114-002519.log new file mode 100644 index 00000000..35a74e13 --- /dev/null +++ b/logs/20261114-002519/amd64/qemu-amd64-20261114-002519.log @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:917b214ac580c76b3b5c1016b66709fe61ec5e799623ff6f5a8241497ab89243 +size 9473 diff --git a/v4/Makefile b/v4/Makefile index ef577337..95e8101d 100644 --- a/v4/Makefile +++ b/v4/Makefile @@ -379,6 +379,16 @@ mesh-boot-on: $(BINDIR)/test_mesh_boot_on @echo " [mesh boot, pipelining and the hot-words cache on]" @$(BINDIR)/test_mesh_boot_on +# And without compudynamics on the opcodes: OPCODE_PHYSICS 0, the revert (#189 Phase 7, ruling 7). +$(BINDIR)/test_mesh_boot_noop: $(BINDIR)/test_mesh_boot + $(CC) $(CFLAGS) -DOPCODE_PHYSICS=0 -D_POSIX_C_SOURCE=200809L -I$(HERE)/include $(V3_INC) -DV4_CELL_BITS=64 $(HOST_DEFS) -c $(HERE)/tests/test_mesh_boot.c -o $(BINDIR)/test_mesh_boot_noop.o + $(CC) $(SYSTEM_CFLAGS) -DOPCODE_PHYSICS=0 $(BINDIR)/test_mesh_boot_noop.o $(HERE)/system/mesh.c $(HERE)/system/inference.c $(HERE)/src/manage.c $(BINDIR)/mesh-rom-64.c $(MACHINE_64) $(CAPSULE_DIR_C) $(ENGINE_SRCS) $(SYSTEM_SRCS) $(BINDIR)/v3o-mesh/*.o -o $@ + +.PHONY: mesh-boot-noop +mesh-boot-noop: $(BINDIR)/test_mesh_boot_noop + @echo " [mesh boot, without compudynamics on the opcodes]" + @$(BINDIR)/test_mesh_boot_noop + # The same, under ASan+UBSan (Captain Bob, 2026-11-13: "Yes, add it to v4-checks"): run by `sanitize`. MESH_SAN := -O1 -g -fno-omit-frame-pointer -fsanitize=address,undefined -fno-sanitize-recover=all $(BINDIR)/san-test_mesh_boot: $(BINDIR)/test_mesh_boot @@ -403,7 +413,7 @@ $(BINDIR)/test_inference: $(HERE)/tests/test_inference.c $(HERE)/system/inferenc inference: $(BINDIR)/test_inference @$(BINDIR)/test_inference -test: $(addprefix test-,$(WIDTHS)) mesh-boot mesh-boot-on inference +test: $(addprefix test-,$(WIDTHS)) mesh-boot mesh-boot-on mesh-boot-noop inference @echo "all v4 tests passed" clean: diff --git a/v4/include/v4/exec.h b/v4/include/v4/exec.h index 62439f98..f5ad86eb 100644 --- a/v4/include/v4/exec.h +++ b/v4/include/v4/exec.h @@ -44,10 +44,16 @@ * target (so P is still the return address). NULL: no hook. */ typedef void (*v4_call_hook)(void *ctx, v4_node *n, v4_cell target); +/* Called when an instruction retires, after HEAT-OP counts it, with its opcode (0..31): compudynamics on the + * opcodes (#189 Phase 7, ruling 7). NULL: no hook. */ +typedef void (*v4_retire_hook)(void *ctx, v4_node *n, unsigned op); + typedef struct { v4_uheat_t anticlock; v4_call_hook on_call; /* reset leaves NULL */ void *on_call_ctx; + v4_retire_hook on_retire; /* reset leaves NULL */ + void *on_retire_ctx; /* A node blocked at its port in the middle of an instruction word * (node.h) goes on, when it has been served, from the slot after the diff --git a/v4/include/v4/mesh.h b/v4/include/v4/mesh.h index 7cc94647..7a658f88 100644 --- a/v4/include/v4/mesh.h +++ b/v4/include/v4/mesh.h @@ -144,14 +144,26 @@ typedef struct { uint8_t hold; /* a card forced (v3's ssm_l8_force_config): L8 does not move from it */ uint64_t changes; /* how many times the card has changed */ } v4_mesh_l8; +/* COMPUDYNAMICS ON THE OPCODES (#189 Phase 7, ruling 7; Captain Bob, 2026-11-14: every loop on the 32 F18 + * opcodes as on the composed words, "and an easy revert if opcodes are too granular"): this is the revert -- + * 0, and the opcodes are counted and charged to the Stadium as in Phase 1, and nothing more. */ +#ifndef OPCODE_PHYSICS +#define OPCODE_PHYSICS 1 +#endif +#define V4_MESH_OPCODES 32u +typedef struct { + v4_uheat_t heat; /* +1 a retirement, decayed by the ticks since (Loop #3) */ + uint64_t last_decay_tick; + uint16_t temperature_q8; + uint8_t state_flags; /* V4_MESH_STATE_FROZEN */ + uint8_t most_likely_next; /* pipelining: the opcode that most often comes next */ + uint64_t most_likely_count, total_transitions; + uint64_t transitions[V4_MESH_OPCODES]; +} v4_mesh_opcode; + +/* a Rolling Window of Truth (v3's RollingWindowOfTruth): IDs in the order they came, 4096 of them, warm at + * 1024, its effective size tuned (Loop #5) */ typedef struct { - uint64_t tick; /* the node's heartbeat cycles (v3's tick_count): by execution, as v3 */ - uint32_t check_counter; /* word calls since the last cycle */ - uint32_t cycle_every; /* V4_MESH_CHECK_FREQUENCY */ - uint64_t decay_slope_q48; - uint8_t l3_decay; /* L8's gate on Loop #3 */ - uint8_t l2_window; /* L8's gate on Loop #2 */ - /* the Rolling Window of Truth (Loop #2; v3's RollingWindowOfTruth): the IDs of the words called, in order */ uint32_t history[V4_MESH_WINDOW]; uint32_t window_pos; /* where the next is written */ uint64_t total_executions; @@ -160,6 +172,17 @@ typedef struct { uint8_t is_warm; uint8_t adaptive_pending; /* the heartbeat should check the window's diversity */ uint64_t last_pattern_diversity, diversity_checks; +} v4_mesh_window; + +typedef struct { + uint64_t tick; /* the node's heartbeat cycles (v3's tick_count): by execution, as v3 */ + uint32_t check_counter; /* word calls since the last cycle */ + uint32_t cycle_every; /* V4_MESH_CHECK_FREQUENCY */ + uint64_t decay_slope_q48; + uint8_t l3_decay; /* L8's gate on Loop #3 */ + uint8_t l2_window; /* L8's gate on Loop #2 */ + /* the Rolling Window of Truth (Loop #2): the words called, in order; and the opcodes retired (ruling 7) */ + v4_mesh_window win, opwin; v4_cell decay_cursor; /* background decay: the next word's xt, down the dictionary; 0: from LATEST */ uint64_t decay_cycles; /* pipelining (Loop #4): the word called before, L8's gate, and the node's counts */ @@ -179,6 +202,12 @@ typedef struct { uint8_t inference_early_exited; uint64_t inference_runs, inference_early_exits; uint64_t tick_target_ns; /* v3's tick_target_ns: HEARTBEAT_TICK_NS, a quarter to four times it */ + /* the opcodes' physics (ruling 7): their records, the one before, their decay slope, their inference */ + v4_mesh_opcode op[V4_MESH_OPCODES]; + uint32_t prev_op; /* V4_MESH_NO_WORD: none yet */ + uint64_t op_decay_slope_q48; + v4_inference_out op_inference; + uint64_t op_inference_runs; /* the dictionary as inference last counted it (v3's *_at_check): words over heat 50, words cooling (heat * 1..9), the heat in all; and the snapshot each cycle publishes (v3's HeartbeatSnapshot) */ uint64_t hot_at_check, stale_at_check, total_heat_at_check; diff --git a/v4/src/exec.c b/v4/src/exec.c index 3f76d0d6..02f828d6 100644 --- a/v4/src/exec.c +++ b/v4/src/exec.c @@ -100,6 +100,8 @@ void v4_exec_reset(v4_exec_state *es) es->anticlock = 0; es->on_call = 0; es->on_call_ctx = 0; + es->on_retire = 0; + es->on_retire_ctx = 0; es->resume = 0; es->resume_iw = 0; es->resume_slot = 0; @@ -275,6 +277,7 @@ void v4_exec_op(v4_node *n, v4_exec_state *es, v4_heat *h, } v4_heat_on_retire(h, op, &es->anticlock); + if (es->on_retire) es->on_retire(es->on_retire_ctx, n, op); } unsigned v4_exec_step_word(v4_node *n, v4_exec_state *es, v4_heat *h) diff --git a/v4/system/mesh.c b/v4/system/mesh.c index bcaf145c..13f099fd 100644 --- a/v4/system/mesh.c +++ b/v4/system/mesh.c @@ -220,15 +220,15 @@ static uint16_t temperature(v4_uheat_t heat, uint16_t prior) } /* Loop #2 (v3's rolling_window_record_execution): the word's ID into the window */ -static void window_record(v4_mesh_physics *p, uint32_t id) +static void window_record(v4_mesh_window *w, uint32_t id) { - p->history[p->window_pos] = id; - p->window_pos = (p->window_pos + 1u) % V4_MESH_WINDOW; - p->total_executions++; - if (p->total_executions >= V4_MESH_WINDOW_WARM && !p->is_warm) p->is_warm = 1; - if (p->is_warm && ++p->adaptive_check_accumulator >= V4_MESH_ADAPTIVE_CHECK) { - p->adaptive_check_accumulator = 0; - p->adaptive_pending = 1; + w->history[w->window_pos] = id; + w->window_pos = (w->window_pos + 1u) % V4_MESH_WINDOW; + w->total_executions++; + if (w->total_executions >= V4_MESH_WINDOW_WARM && !w->is_warm) w->is_warm = 1; + if (w->is_warm && ++w->adaptive_check_accumulator >= V4_MESH_ADAPTIVE_CHECK) { + w->adaptive_check_accumulator = 0; + w->adaptive_pending = 1; } } @@ -366,38 +366,38 @@ static void background_decay(unsigned place, v4_mesh_physics *p) /* the window's adaptive pass (v3's rolling_window_run_adaptive_pass), when due: its diversity -- how often a * word differs from the one before, over the window -- against the last; little growth and it halves, * to no less than 256; growth and it doubles, to no more than 4096 */ -static void window_service(v4_mesh_physics *p) +static void window_service(v4_mesh_window *w) { uint64_t diversity = 0, delta, growth_q48, threshold_q48; uint32_t limit, i, prev = 0; int first = 1; - if (!p->adaptive_pending) return; - p->adaptive_pending = 0; - if (!p->is_warm || p->total_executions < 2) return; - limit = p->effective_window_size; + if (!w->adaptive_pending) return; + w->adaptive_pending = 0; + if (!w->is_warm || w->total_executions < 2) return; + limit = w->effective_window_size; for (i = 0; i < limit; i++) { - uint32_t cur = p->history[(p->window_pos + V4_MESH_WINDOW - limit + i) % V4_MESH_WINDOW]; + uint32_t cur = w->history[(w->window_pos + V4_MESH_WINDOW - limit + i) % V4_MESH_WINDOW]; if (!first && cur != prev) diversity++; prev = cur; first = 0; } - p->diversity_checks++; - if (p->last_pattern_diversity == 0) { p->last_pattern_diversity = diversity; return; } - delta = diversity > p->last_pattern_diversity ? diversity - p->last_pattern_diversity : 0; - growth_q48 = (delta << 16) / p->last_pattern_diversity; + w->diversity_checks++; + if (w->last_pattern_diversity == 0) { w->last_pattern_diversity = diversity; return; } + delta = diversity > w->last_pattern_diversity ? diversity - w->last_pattern_diversity : 0; + growth_q48 = (delta << 16) / w->last_pattern_diversity; threshold_q48 = ((uint64_t)V4_MESH_GROWTH_THRESHOLD << 16) / 100u; if (growth_q48 < threshold_q48) { - if (p->effective_window_size > V4_MESH_WINDOW_MIN) { - uint32_t n = (p->effective_window_size * V4_MESH_SHRINK_RATE) / 100u; + if (w->effective_window_size > V4_MESH_WINDOW_MIN) { + uint32_t n = (w->effective_window_size * V4_MESH_SHRINK_RATE) / 100u; if (n < V4_MESH_WINDOW_MIN) n = V4_MESH_WINDOW_MIN; - if (n < p->effective_window_size) p->effective_window_size = n; + if (n < w->effective_window_size) w->effective_window_size = n; } - } else if (p->effective_window_size < V4_MESH_WINDOW) { - uint32_t n = (p->effective_window_size * ((100u * 100u) / V4_MESH_SHRINK_RATE)) / 100u; + } else if (w->effective_window_size < V4_MESH_WINDOW) { + uint32_t n = (w->effective_window_size * ((100u * 100u) / V4_MESH_SHRINK_RATE)) / 100u; if (n > V4_MESH_WINDOW) n = V4_MESH_WINDOW; - if (n > p->effective_window_size) p->effective_window_size = n; + if (n > w->effective_window_size) w->effective_window_size = n; } - p->last_pattern_diversity = diversity; + w->last_pattern_diversity = diversity; } /* ---- L8 (v3's vm_heartbeat_update_l8 with its table: ssm_jacquard.c, compudynamics.c) ---- */ @@ -457,10 +457,10 @@ static void l8_seed(v4_mesh_l8 *t) static uint32_t l8_locality(const v4_mesh_physics *p) { uint32_t ids[32], n = 32, i, j, matches = 0; - uint64_t have = p->total_executions < V4_MESH_WINDOW ? p->total_executions : V4_MESH_WINDOW; + uint64_t have = p->win.total_executions < V4_MESH_WINDOW ? p->win.total_executions : V4_MESH_WINDOW; if (have < n) n = (uint32_t)have; if (n <= 8) return 0; - for (i = 0; i < n; i++) ids[i] = p->history[(p->window_pos + V4_MESH_WINDOW - n + i) % V4_MESH_WINDOW]; + for (i = 0; i < n; i++) ids[i] = p->win.history[(p->win.window_pos + V4_MESH_WINDOW - n + i) % V4_MESH_WINDOW]; for (i = 8; i < n; i++) for (j = i - 8; j < i; j++) if (ids[j] == ids[i]) { matches++; break; } return (uint32_t)(((uint64_t)matches * 65536u) / (n - 8)); @@ -470,7 +470,7 @@ static uint32_t l8_locality(const v4_mesh_physics *p) static void l8_update(v4_mesh_physics *p) { v4_mesh_l8 *t = &p->l8; - uint32_t locality = l8_locality(p), window = p->effective_window_size, anova, combined, r, c, ci, total, lnt, best = 0, best_ucb = 0; + uint32_t locality = l8_locality(p), window = p->win.effective_window_size, anova, combined, r, c, ci, total, lnt, best = 0, best_ucb = 0; int entropy_high = (uint64_t)window * 100u >= 75u * (uint64_t)V4_MESH_WINDOW; /* >= 0.75 */ int cv_high = p->prefetch_attempts == 0 ? 1 /* v3: 0.5 when no data */ : 100u * (p->prefetch_attempts - p->prefetch_hits) >= 15u * p->prefetch_attempts; /* 1 - accuracy >= 0.15 */ @@ -524,17 +524,92 @@ void v4_mesh_l8_force(v4_mesh *ms, unsigned place, unsigned card, int hold) l8_apply(&ms->physics[place]); } +static uint64_t trajectory[V4_MESH_WINDOW]; + +/* ---- the opcodes (#189 Phase 7, ruling 7): every loop, as on the words ---- */ + +#if OPCODE_PHYSICS +/* at every retired instruction (exec.h): as a word's call -- decay by the ticks since at the opcodes' slope, heat + * +1, the temperature, the opcode window (L2), the transition from the one before (L4) */ +static void on_retire(void *ctx, v4_node *n, unsigned op) +{ + unsigned place = place_of_device(ctx); + v4_mesh_physics *p; + v4_mesh_opcode *o; + uint64_t elapsed, amount; + (void)n; + if (!the->physics || op >= V4_MESH_OPCODES) return; + p = &the->physics[place]; + o = &p->op[op]; + elapsed = p->tick - o->last_decay_tick; + if (!(o->state_flags & V4_MESH_STATE_FROZEN) && elapsed && p->l3_decay) { + amount = (elapsed * p->op_decay_slope_q48) >> 16; + o->heat = amount >= o->heat ? 0 : o->heat - amount; + } + o->last_decay_tick = p->tick; + o->heat++; + o->temperature_q8 = temperature(o->heat, o->temperature_q8); + if (p->l2_window) window_record(&p->opwin, op); +#if ENABLE_PIPELINING + if (p->l4_pipelining && p->prev_op < V4_MESH_OPCODES) { + v4_mesh_opcode *pr = &p->op[p->prev_op]; + pr->transitions[op]++; + pr->total_transitions++; + if (pr->transitions[op] > pr->most_likely_count || (pr->transitions[op] == pr->most_likely_count && op < pr->most_likely_next)) { + pr->most_likely_count = pr->transitions[op]; + pr->most_likely_next = (uint8_t)op; + } + } +#endif + p->prev_op = op; +} + +/* in the cycle: all 32 cooled (background decay), the opcode window's service */ +static void opcodes_cycle(v4_mesh_physics *p) +{ + unsigned k; + for (k = 0; k < V4_MESH_OPCODES; k++) { + v4_mesh_opcode *o = &p->op[k]; + uint64_t elapsed = p->tick - o->last_decay_tick, amount; + if (!(o->state_flags & V4_MESH_STATE_FROZEN) && elapsed && p->l3_decay) { + amount = (elapsed * p->op_decay_slope_q48) >> 16; + o->heat = amount >= o->heat ? 0 : o->heat - amount; + } + o->last_decay_tick = p->tick; + } + window_service(&p->opwin); +} + +/* with the words' inference: the opcode window's heat, its width and the opcodes' slope */ +static void opcodes_inference(v4_mesh_physics *p) +{ + uint64_t have, span, i; + if (!p->opwin.is_warm) return; + p->op_inference_runs++; + have = p->opwin.total_executions < V4_MESH_WINDOW ? p->opwin.total_executions : V4_MESH_WINDOW; + span = p->opwin.effective_window_size < have ? p->opwin.effective_window_size : have; + for (i = 0; i < span; i++) { + uint32_t op = p->opwin.history[(p->opwin.window_pos + V4_MESH_WINDOW - span + i) % V4_MESH_WINDOW]; + trajectory[i] = op < V4_MESH_OPCODES ? (uint64_t)p->op[op].heat : 0; + } + v4_inference_run(trajectory, span, p->l5_window_inference, p->l6_decay_inference, &p->op_inference); + if (!p->op_inference.early_exited) { + if (p->op_inference.adaptive_window_width > 0) p->opwin.effective_window_size = p->op_inference.adaptive_window_width; + if (p->op_inference.adaptive_decay_slope > 0) p->op_decay_slope_q48 = p->op_inference.adaptive_decay_slope; + } +} +#endif + /* ---- inference (v3's vm_tick_inference_engine) and Loop #7 ---- */ -static uint64_t trajectory[V4_MESH_WINDOW]; static void inference_tick(unsigned place, v4_mesh_physics *p) { uint64_t have, span, i; v4_fabric_node *x = v4_fabric_node_at(&the->f, place); - if (!p->is_warm || !x) return; + if (!p->win.is_warm || !x) return; p->inference_runs++; - window_service(p); + window_service(&p->win); { /* the dictionary's heat, counted (as v3's) */ uint64_t hot = 0, stale = 0, total = 0; unsigned k; @@ -548,21 +623,24 @@ static void inference_tick(unsigned place, v4_mesh_physics *p) p->hot_at_check = hot; p->stale_at_check = stale; p->total_heat_at_check = total; } /* the heat of each word in the window, the newest `span` of them, oldest first (v3's extract_heat_trajectory) */ - have = p->total_executions < V4_MESH_WINDOW ? p->total_executions : V4_MESH_WINDOW; - span = p->effective_window_size < have ? p->effective_window_size : have; + have = p->win.total_executions < V4_MESH_WINDOW ? p->win.total_executions : V4_MESH_WINDOW; + span = p->win.effective_window_size < have ? p->win.effective_window_size : have; for (i = 0; i < span; i++) { - uint32_t id = p->history[(p->window_pos + V4_MESH_WINDOW - span + i) % V4_MESH_WINDOW]; + uint32_t id = p->win.history[(p->win.window_pos + V4_MESH_WINDOW - span + i) % V4_MESH_WINDOW]; trajectory[i] = (id < V4_MESH_WORD_IDS && accounts_of(place)[id].xt) ? (uint64_t)accounts_of(place)[id].heat : 0; } v4_inference_run(trajectory, span, p->l5_window_inference, p->l6_decay_inference, &p->inference); if (!p->inference.early_exited) { - if (p->inference.adaptive_window_width > 0 && p->inference.adaptive_window_width != p->effective_window_size) - p->effective_window_size = p->inference.adaptive_window_width; + if (p->inference.adaptive_window_width > 0 && p->inference.adaptive_window_width != p->win.effective_window_size) + p->win.effective_window_size = p->inference.adaptive_window_width; if (p->inference.adaptive_decay_slope > 0 && p->inference.adaptive_decay_slope != p->decay_slope_q48) p->decay_slope_q48 = p->inference.adaptive_decay_slope; p->last_inference_tick = p->tick; } else p->inference_early_exits++; p->inference_early_exited = p->inference.early_exited; +#if OPCODE_PHYSICS + opcodes_inference(p); +#endif /* Loop #7, the adaptive heartrate: stable, 25% slower; else 20% faster -- within 1/4 to 4 times the nominal */ { uint64_t cur = p->tick_target_ns, n; @@ -581,12 +659,15 @@ static void heartbeat_cycle(unsigned place, v4_mesh_physics *p) p->tick++; if (p->tick - p->last_inference_tick >= V4_MESH_INFERENCE_EVERY) inference_tick(place, p); background_decay(place, p); - window_service(p); + window_service(&p->win); +#if OPCODE_PHYSICS + opcodes_cycle(p); +#endif l8_update(p); /* the snapshot (v3's heartbeat_publish_snapshot) */ p->snapshot.tick = p->tick; - p->snapshot.window_width = p->effective_window_size; - p->snapshot.fill = (uint32_t)(p->total_executions < V4_MESH_WINDOW ? p->total_executions : V4_MESH_WINDOW); + p->snapshot.window_width = p->win.effective_window_size; + p->snapshot.fill = (uint32_t)(p->win.total_executions < V4_MESH_WINDOW ? p->win.total_executions : V4_MESH_WINDOW); p->snapshot.decay_slope_q48 = p->decay_slope_q48; p->snapshot.hot = p->hot_at_check; p->snapshot.stale = p->stale_at_check; @@ -611,7 +692,7 @@ static void on_call(void *ctx, v4_node *n, v4_cell target) a->last_decay_tick = p->tick; a->heat++; a->temperature_q8 = temperature(a->heat, a->temperature_q8); - if (p->l2_window) window_record(p, (uint32_t)id); + if (p->l2_window) window_record(&p->win, (uint32_t)id); #if ENABLE_PIPELINING if (p->l4_pipelining) pipeline(place, p, (uint32_t)id); #endif @@ -684,7 +765,10 @@ static void words_attach(unsigned place, v4_fabric_node *x) p->tick = tick; p->cycle_every = V4_MESH_CHECK_FREQUENCY; p->decay_slope_q48 = V4_MESH_INITIAL_DECAY_SLOPE_Q48; - p->effective_window_size = V4_MESH_WINDOW; + p->win.effective_window_size = V4_MESH_WINDOW; + p->opwin.effective_window_size = V4_MESH_WINDOW; + p->prev_op = V4_MESH_NO_WORD; + p->op_decay_slope_q48 = V4_MESH_INITIAL_DECAY_SLOPE_Q48; p->prev_id = V4_MESH_NO_WORD; /* every gate off, and L8's table on card 8, as v3's kernel boot (vm_bootstrap.c): the first cycle's L8 * applies its card */ @@ -694,6 +778,10 @@ static void words_attach(unsigned place, v4_fabric_node *x) } x->es.on_call = on_call; x->es.on_call_ctx = &the->kernel[place]; +#if OPCODE_PHYSICS + x->es.on_retire = on_retire; + x->es.on_retire_ctx = &the->kernel[place]; +#endif } unsigned v4_mesh_state_flags(const v4_mesh *ms, unsigned place, v4_cell xt) diff --git a/v4/tests/test_exec.c b/v4/tests/test_exec.c index 34bcef18..c677b145 100644 --- a/v4/tests/test_exec.c +++ b/v4/tests/test_exec.c @@ -622,6 +622,34 @@ static void test_call_hook_sees_every_call(void) CHECK(es.on_call == 0, "v4_exec_reset leaves no hook"); } +/* the hook at retire (#189 Phase 7, ruling 7: compudynamics on the opcodes): every retired instruction, its opcode, + * in order, as HEAT-OP counts it */ +static unsigned retired_seen, retire_bad, op_seen[32]; +static void record_retire(void *ctx, v4_node *nd, unsigned op) +{ + if (ctx != (void *)&retired_seen || nd != &n || op >= 32) { retire_bad++; return; } + retired_seen++; + op_seen[op]++; +} + +static void test_retire_hook_sees_every_instruction(void) +{ + unsigned i, retired = 0, k, same = 1; + v4_uheat_t before[32]; + call_programme(); + for (k = 0; k < 32; k++) { before[k] = h.op[k]; op_seen[k] = 0; } + es.on_retire = record_retire; es.on_retire_ctx = (void *)&retired_seen; + retired_seen = 0; retire_bad = 0; + for (i = 0; i < 9; i++) retired += v4_exec_step_word(&n, &es, &h); + for (k = 0; k < 32; k++) if (op_seen[k] != (unsigned)(h.op[k] - before[k])) same = 0; + CHECK(retire_bad == 0 && retired_seen == retired && retired > 0, + "the retire hook sees every retired instruction (%u of %u), with its context and node", retired_seen, retired); + CHECK(same, "and each opcode as often as HEAT-OP counts it"); + es.on_retire = 0; es.on_retire_ctx = 0; + v4_exec_reset(&es); + CHECK(es.on_retire == 0, "v4_exec_reset leaves no retire hook"); +} + int main(void) { printf("v4 exec tests: V4_CELL_BITS=%d\n", V4_CELL_BITS); @@ -634,6 +662,7 @@ int main(void) test_return_and_ex(); test_branches(); test_call_hook_sees_every_call(); + test_retire_hook_sees_every_instruction(); test_unext(); test_retirement(); #if V4_CELL_BITS == 64 diff --git a/v4/tests/test_mesh_boot.c b/v4/tests/test_mesh_boot.c index 97463e0a..c8a1b6c2 100644 --- a/v4/tests/test_mesh_boot.c +++ b/v4/tests/test_mesh_boot.c @@ -218,12 +218,44 @@ int main(void) r = line(V4_MESH_HERA, "FORGET L8T", &how); } + /* ---- the opcodes (#189 Phase 7, ruling 7: every loop on the 32 F18 opcodes too; revert: OPCODE_PHYSICS 0) ---- */ + { + v4_mesh_physics *p = &ms.physics[0]; + uint64_t heat_add, total0 = p->opwin.total_executions; + unsigned k, hot = 0; + v4_mesh_l8_force(&ms, 0, 60u, 1); /* L1..L4 on: the window, decay, pipelining */ + r = line(V4_MESH_HERA, ": OPS 0 1000 0 DO 3 + LOOP DROP ; OPS", &how); + for (k = 0; k < V4_MESH_OPCODES; k++) if (p->op[k].heat > 0) hot++; + heat_add = p->op[20].heat; /* + is opcode 20 */ +#if OPCODE_PHYSICS + CHECK(how == V4_TEXT_COMPLETED && hot > 5 && heat_add > 0 && p->op[20].temperature_q8 > 0, + "the opcodes have heat of their own: %u of 32 warm, + at %lu", hot, (unsigned long)heat_add); + CHECK(p->opwin.total_executions > total0 + 1000u, "the opcode window took every retirement (%lu)", + (unsigned long)(p->opwin.total_executions - total0)); + CHECK(p->opwin.is_warm && p->op_decay_slope_q48 > 0, "it is warm, and the opcodes have a decay slope of their own"); +#if ENABLE_PIPELINING + CHECK(p->op[20].total_transitions > 0, "pipelining on: + has transitions, its likeliest next opcode %u", p->op[20].most_likely_next); +#endif + { + uint64_t runs = p->op_inference_runs; + r = line(V4_MESH_HERA, ": LOPS 400 0 DO OPS LOOP ; LOPS", &how); + CHECK(p->op_inference_runs > runs || p->inference_runs == 0, "inference runs on the opcodes' window too (%lu)", + (unsigned long)p->op_inference_runs); + } +#else + CHECK(how == V4_TEXT_COMPLETED && hot == 0 && heat_add == 0 && p->opwin.total_executions == total0, + "built without OPCODE_PHYSICS (the revert): no opcode records, as Phase 1"); +#endif + v4_mesh_l8_force(&ms, 0, p->l8.current_card, 0); + r = line(V4_MESH_HERA, "FORGET OPS", &how); + } + /* ---- inference every 1000 ticks, and Loop #7 (#189 Phase 7, Task 3) ---- */ { v4_mesh_physics *p = &ms.physics[0]; uint64_t t0 = p->tick, runs0 = p->inference_runs; r = line(V4_MESH_HERA, ": IW ; : LIW 30 0 DO 10000 0 DO IW LOOP LOOP ; LIW", &how); /* 300000 calls: over 1000 ticks */ - CHECK(how == V4_TEXT_COMPLETED && p->tick - t0 > 1000u && p->is_warm, "a long run: over 1000 ticks (%lu), the window warm", + CHECK(how == V4_TEXT_COMPLETED && p->tick - t0 > 1000u && p->win.is_warm, "a long run: over 1000 ticks (%lu), the window warm", (unsigned long)(p->tick - t0)); CHECK(p->inference_runs > runs0, "inference ran (%lu times)", (unsigned long)p->inference_runs); CHECK(p->tick_target_ns != HEARTBEAT_TICK_NS && p->tick_target_ns >= HEARTBEAT_TICK_NS / 4u @@ -232,7 +264,7 @@ int main(void) CHECK(heartrates > 0 && heartrate_ns == (p->tick_target_ns * 10000000ULL) / HEARTBEAT_TICK_NS, "and on Hera re-armed the kernel's heartbeat: %lu ns", (unsigned long)heartrate_ns); CHECK(!p->inference.early_exited ? p->last_inference_tick > 0 : 1, "a full run marks its tick"); - CHECK(p->snapshot.tick == p->tick && p->snapshot.window_width == p->effective_window_size + CHECK(p->snapshot.tick == p->tick && p->snapshot.window_width == p->win.effective_window_size && p->snapshot.decay_slope_q48 == p->decay_slope_q48 && p->snapshot.fill == V4_MESH_WINDOW && p->snapshot.total_heat == p->total_heat_at_check && p->total_heat_at_check > 0, "the cycle's snapshot: its tick, window, slope, and the dictionary's heat as inference counted it"); @@ -283,12 +315,12 @@ int main(void) CHECK(p->l2_window && p->l3_decay && !p->l4_pipelining && !p->l1_cache && p->l7_adaptive_heartrate, "card 24's gates: the window, decay, and L7, which is always on"); v4_mesh_set_tick(&ms, 100); - total0 = p->total_executions; + total0 = p->win.total_executions; r = line(V4_MESH_HERA, "2 P7 3 P7 4 P7 + + .", &how); CHECK(strcmp(r, "29 ") == 0 && a && a->heat == 3 && a->last_decay_tick == 100, "three calls: heat 3, decayed at tick 100 (%lu)", a ? (unsigned long)a->heat : 0ul); - CHECK(p->total_executions >= total0 + 3 && p->history[(p->window_pos + V4_MESH_WINDOW - 1u) % V4_MESH_WINDOW] - != (uint32_t)-1, "the rolling window took them (%lu)", (unsigned long)(p->total_executions - total0)); + CHECK(p->win.total_executions >= total0 + 3 && p->win.history[(p->win.window_pos + V4_MESH_WINDOW - 1u) % V4_MESH_WINDOW] + != (uint32_t)-1, "the rolling window took them (%lu)", (unsigned long)(p->win.total_executions - total0)); CHECK(a && a->temperature_q8 > 0, "and the temperature rose: %u", a ? a->temperature_q8 : 0u); v4_mesh_set_tick(&ms, 106); /* six ticks at 21845 (1/3 a tick, Q48.16): 1 heat */ r = line(V4_MESH_HERA, "2 P7 .", &how); @@ -336,9 +368,9 @@ int main(void) r = line(V4_MESH_HERA, ": NOP ; : LZ 20000 0 DO NOP LOOP ; LZ", &how); /* many cycles of calls, none to Q7 */ CHECK(qa && qa->heat < Q7_HOT && qa->last_decay_tick > 0, "Q7, never called again, has cooled in the background: %lu", qa ? (unsigned long)qa->heat : 0ul); - CHECK(p->diversity_checks > 0 && p->effective_window_size >= 256u && p->effective_window_size <= V4_MESH_WINDOW, + CHECK(p->win.diversity_checks > 0 && p->win.effective_window_size >= 256u && p->win.effective_window_size <= V4_MESH_WINDOW, "the window, warm, checked its diversity (%lu checks), its size within 256..4096: %u", - (unsigned long)p->diversity_checks, p->effective_window_size); + (unsigned long)p->win.diversity_checks, p->win.effective_window_size); r = line(V4_MESH_HERA, "FORGET Q7", &how); } v4_mesh_l8_force(&ms, 0, ms.physics[0].l8.current_card, 0); /* L8 chooses again */