v4 fabric: each pass of a step walks only the places that hold a node (mechanism A)
The fabric keeps its occupied places as a list in ascending order, and the step's nine passes walk that list in the same order as before. A place removed mid-walk keeps its link, so the walk goes on to the next node. Test first: test_fabric_walk.c, 13 checks. Every existing fabric and mesh test passes with the same checks, and so do test-32 and ASan/UBSan. The host mesh boot runs POST 990/990. Over five alternating runs its median falls from 11.695 s to 7.999 s. Logs are in logs/20261117-mechanism-a. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_017RzZgrf7V7Zk2iczBXDRbm
This commit is contained in:
+44
-11
@@ -29,7 +29,38 @@ void v4_fabric_init(v4_fabric *f, v4_place *places, unsigned capacity)
|
||||
f->queue_free = 0;
|
||||
f->queue_first = 0;
|
||||
f->gone_error = 0;
|
||||
for (i = 0; i < capacity; i++) clear_place(&places[i]);
|
||||
f->first = 0;
|
||||
for (i = 0; i < capacity; i++) { clear_place(&places[i]); places[i].next = 0; }
|
||||
}
|
||||
|
||||
unsigned v4_fabric_first(const v4_fabric *f)
|
||||
{
|
||||
return f->first ? f->first - 1u : f->capacity;
|
||||
}
|
||||
|
||||
unsigned v4_fabric_next(const v4_fabric *f, unsigned id)
|
||||
{
|
||||
if (id >= f->capacity) return f->capacity;
|
||||
if (f->place[id].node) return f->place[id].next ? f->place[id].next - 1u : f->capacity;
|
||||
for (id++; id < f->capacity && !f->place[id].node; id++) { } /* removed while walked: the next that holds one */
|
||||
return id;
|
||||
}
|
||||
|
||||
/* a node is now at `id`: into the walk, in order */
|
||||
static void walk_in(v4_fabric *f, unsigned id)
|
||||
{
|
||||
unsigned *at = &f->first;
|
||||
while (*at && *at - 1u < id) at = &f->place[*at - 1u].next;
|
||||
f->place[id].next = *at;
|
||||
*at = id + 1u;
|
||||
}
|
||||
|
||||
/* the node at `id` is going: out of the walk. Its own link is kept, so a walk at it goes on. */
|
||||
static void walk_out(v4_fabric *f, unsigned id)
|
||||
{
|
||||
unsigned *at = &f->first;
|
||||
while (*at && *at - 1u != id) at = &f->place[*at - 1u].next;
|
||||
if (*at) *at = f->place[id].next;
|
||||
}
|
||||
|
||||
void v4_fabric_gone_error(v4_fabric *f, v4_cell code)
|
||||
@@ -42,7 +73,7 @@ void v4_fabric_grow(v4_fabric *f, v4_place *places, unsigned capacity)
|
||||
unsigned i;
|
||||
if (capacity < f->capacity) return;
|
||||
for (i = 0; i < f->capacity; i++) places[i] = f->place[i];
|
||||
for (; i < capacity; i++) clear_place(&places[i]);
|
||||
for (; i < capacity; i++) { clear_place(&places[i]); places[i].next = 0; }
|
||||
f->place = places;
|
||||
f->capacity = capacity;
|
||||
}
|
||||
@@ -54,6 +85,7 @@ int v4_fabric_add(v4_fabric *f, v4_fabric_node *storage, v4_cell port_base)
|
||||
if (i == f->capacity || !storage) return -1;
|
||||
clear_place(&f->place[i]);
|
||||
f->place[i].node = storage;
|
||||
walk_in(f, i);
|
||||
v4_node_born(&storage->n, port_base);
|
||||
v4_exec_reset(&storage->es);
|
||||
v4_heat_reset(&storage->h);
|
||||
@@ -213,6 +245,7 @@ v4_fabric_node *v4_fabric_remove(v4_fabric *f, unsigned id)
|
||||
if (id >= f->capacity || !f->place[id].node) return 0;
|
||||
for (k = 0; k < V4_PORTS; k++) cut(f, id, k);
|
||||
storage = f->place[id].node;
|
||||
walk_out(f, id);
|
||||
clear_place(&f->place[id]);
|
||||
return storage;
|
||||
}
|
||||
@@ -425,7 +458,7 @@ unsigned v4_fabric_step(v4_fabric *f)
|
||||
unsigned i, k, done = 0;
|
||||
|
||||
/* which of its wires have a message waiting, for each node to fetch */
|
||||
for (i = 0; i < f->capacity; i++)
|
||||
for (i = v4_fabric_first(f); i < f->capacity; i = v4_fabric_next(f, i))
|
||||
if (awake(f, i)) {
|
||||
unsigned have = 0;
|
||||
for (k = 0; k < V4_PORTS; k++) {
|
||||
@@ -437,17 +470,17 @@ unsigned v4_fabric_step(v4_fabric *f)
|
||||
|
||||
/* a node that is blocked fetches nothing, and is looked about for when
|
||||
* it is next to execute */
|
||||
for (i = 0; i < f->capacity; i++)
|
||||
for (i = v4_fabric_first(f); i < f->capacity; i = v4_fabric_next(f, i))
|
||||
if (awake(f, i)) {
|
||||
const v4_node *n = &f->place[i].node->n;
|
||||
if (!n->stopped && !n->asking && !n->doing && !(n->reading && !n->given)) look_about(f, i);
|
||||
}
|
||||
|
||||
for (i = 0; i < f->capacity; i++)
|
||||
for (i = v4_fabric_first(f); i < f->capacity; i = v4_fabric_next(f, i))
|
||||
if (awake(f, i)) done += v4_exec_step_word(&f->place[i].node->n, &f->place[i].node->es, &f->place[i].node->h);
|
||||
|
||||
/* a node that fetched which wires have a message has seen what had come */
|
||||
for (i = 0; i < f->capacity; i++)
|
||||
for (i = v4_fabric_first(f); i < f->capacity; i = v4_fabric_next(f, i))
|
||||
if (awake(f, i) && f->place[i].node->n.have_fetched) {
|
||||
for (k = 0; k < V4_PORTS; k++)
|
||||
if (f->place[i].wire[k].rx) f->place[i].wire[k].rx->arrived = 0;
|
||||
@@ -456,28 +489,28 @@ unsigned v4_fabric_step(v4_fabric *f)
|
||||
|
||||
/* the operations on the wires, each whole; the two that wait are looked at last, so that
|
||||
* what every other node has put or taken this step has been, whatever their numbers */
|
||||
for (i = 0; i < f->capacity; i++)
|
||||
for (i = v4_fabric_first(f); i < f->capacity; i = v4_fabric_next(f, i))
|
||||
if (awake(f, i) && f->place[i].node->n.doing && f->place[i].node->n.do_op != 8 && f->place[i].node->n.do_op != 9) {
|
||||
v4_cell how;
|
||||
(void)operate(f, i, &how);
|
||||
v4_node_done(&f->place[i].node->n, how);
|
||||
done++;
|
||||
}
|
||||
for (i = 0; i < f->capacity; i++)
|
||||
for (i = v4_fabric_first(f); i < f->capacity; i = v4_fabric_next(f, i))
|
||||
if (awake(f, i) && f->place[i].node->n.doing) {
|
||||
v4_cell how;
|
||||
if (operate(f, i, &how)) { v4_node_done(&f->place[i].node->n, how); done++; }
|
||||
}
|
||||
|
||||
for (i = 0; i < f->capacity; i++)
|
||||
for (i = v4_fabric_first(f); i < f->capacity; i = v4_fabric_next(f, i))
|
||||
if (awake(f, i) && f->place[i].node->n.asking) done += hand_over_write(f, i);
|
||||
for (i = 0; i < f->capacity; i++)
|
||||
for (i = v4_fabric_first(f); i < f->capacity; i = v4_fabric_next(f, i))
|
||||
if (awake(f, i) && f->place[i].node->n.reading && !f->place[i].node->n.given) done += hand_over_read(f, i);
|
||||
|
||||
/* a node blocked at one port that nothing is wired to; or blocked writing a
|
||||
* word to what will not read one (fabric.h, A WORD NO ONE WILL READ) */
|
||||
if (f->gone_error != 0)
|
||||
for (i = 0; i < f->capacity; i++) {
|
||||
for (i = v4_fabric_first(f); i < f->capacity; i = v4_fabric_next(f, i)) {
|
||||
v4_node *n;
|
||||
if (!awake(f, i)) continue;
|
||||
n = &f->place[i].node->n;
|
||||
|
||||
Reference in New Issue
Block a user