From 238116c1cf08b93335abd493df110a669786553e Mon Sep 17 00:00:00 2001 From: Kimplul Date: Sun, 23 Feb 2025 22:21:50 +0200 Subject: 3d mesh --- src/grid/node3d.c | 205 +++++++++++++++++++++++++++++++++++++++++++++++++++++ src/grid/source.mk | 2 +- src/root.c | 3 - src/torus3d/node.c | 43 +++++------ 4 files changed, 228 insertions(+), 25 deletions(-) create mode 100644 src/grid/node3d.c (limited to 'src') diff --git a/src/grid/node3d.c b/src/grid/node3d.c new file mode 100644 index 0000000..f404ea0 --- /dev/null +++ b/src/grid/node3d.c @@ -0,0 +1,205 @@ +#include + +struct reg { + struct packet pkt; + bool busy; +}; + +struct node3d { + struct component component; + uint8_t x, y, z; + + uint64_t timestamp; + + struct component *n, *s, *w, *e, *u, *d, *l; + + struct reg n_in, s_in, w_in, e_in, u_in, d_in, l_in; +}; + +static stat reg_receive(struct reg *r, struct packet pkt) +{ + if (r->busy) + return EBUSY; + + r->pkt = pkt; + r->busy = true; + return OK; +} + +/* LX = node.x > pkt.x, etc */ +enum match { + LX = (1 << 0), + LY = (1 << 1), + LZ = (1 << 2), + GX = (1 << 3), + GY = (1 << 4), + GZ = (1 << 5), + NX = (1 << 6), + NY = (1 << 7), + NZ = (1 << 8), +}; + +static void maybe_route_reg(struct node3d *node3d, struct reg *reg, enum match m, uint64_t *oldest, struct packet **pkt, bool **busy) +{ + if (!reg->busy) + return; + + uint8_t x, y, z; + addr_grid3d(reg->pkt.to, &x, &y, &z, NULL); + + if ((m & LX) && x < node3d->x) + return; + + if ((m & LY) && y < node3d->y) + return; + + if ((m & LZ) && z < node3d->z) + return; + + if ((m & GX) && x > node3d->x) + return; + + if ((m & GY) && y > node3d->y) + return; + + if ((m & GZ) && z > node3d->z) + return; + + if ((m & NX) && x == node3d->x) + return; + + if ((m & NY) && y == node3d->y) + return; + + if ((m & NZ) && z == node3d->z) + return; + + if (reg->pkt.timestamp < *oldest) { + *oldest = reg->pkt.timestamp; + *busy = ®->busy; + *pkt = ®->pkt; + } +} + +static stat route(struct node3d *node3d, struct component *next, enum match m) +{ + uint64_t oldest = -1; struct packet *pkt = NULL; bool *busy = NULL; + maybe_route_reg(node3d, &node3d->l_in, m, &oldest, &pkt, &busy); + maybe_route_reg(node3d, &node3d->n_in, m, &oldest, &pkt, &busy); + maybe_route_reg(node3d, &node3d->s_in, m, &oldest, &pkt, &busy); + maybe_route_reg(node3d, &node3d->e_in, m, &oldest, &pkt, &busy); + maybe_route_reg(node3d, &node3d->w_in, m, &oldest, &pkt, &busy); + maybe_route_reg(node3d, &node3d->u_in, m, &oldest, &pkt, &busy); + maybe_route_reg(node3d, &node3d->d_in, m, &oldest, &pkt, &busy); + + /* no suitable match */ + if (pkt == NULL) + return OK; + + assert(busy); + + if (!next) { + abort(); /* for now, eventually should probably return to sender */ + return OK; + } + + stat ret = SEND(node3d, next, *pkt); + if (ret == EBUSY) + return OK; + + *busy = false; + return OK; +} + +static stat node3d_clock(struct node3d *node3d) +{ + node3d->timestamp++; + + stat ret = OK; + if ((ret = route(node3d, node3d->e, LX | NX))) + return ret; + + if ((ret = route(node3d, node3d->w, GX | NX))) + return ret; + + if ((ret = route(node3d, node3d->n, LX | GX | LY | NY))) + return ret; + + if ((ret = route(node3d, node3d->s, LX | GX | GY | NY))) + return ret; + + if ((ret = route(node3d, node3d->u, LX | GX | LY | GY | LZ | NZ))) + return ret; + + if ((ret = route(node3d, node3d->d, LX | GX | LY | GY | GZ | NZ))) + return ret; + + if ((ret = route(node3d, node3d->l, LX | LY | LZ | GX | GY | GZ))) + return ret; + + return OK; +} + +static stat node3d_receive(struct node3d *node3d, struct component *from, struct packet pkt) +{ + if (from == node3d->l) { + /* add time when packet entered network */ + pkt.timestamp = node3d->timestamp; + return reg_receive(&node3d->l_in, pkt); + } + + if (from == node3d->n) + return reg_receive(&node3d->n_in, pkt); + + if (from == node3d->s) + return reg_receive(&node3d->s_in, pkt); + + if (from == node3d->w) + return reg_receive(&node3d->w_in, pkt); + + if (from == node3d->e) + return reg_receive(&node3d->e_in, pkt); + + if (from == node3d->u) + return reg_receive(&node3d->u_in, pkt); + + if (from == node3d->d) + return reg_receive(&node3d->d_in, pkt); + + abort(); + return OK; +} + +struct component *create_grid_node3d(uint8_t x, uint8_t y, uint8_t z) +{ + struct node3d *node = calloc(1, sizeof(struct node3d)); + if (!node) + return NULL; + + node->component.receive = (receive_callback)node3d_receive; + node->component.clock = (clock_callback)node3d_clock; + node->x = x; + node->y = y; + node->z = z; + return (struct component *)node; +} + +stat grid_node3d_connect(struct component *node, + struct component *n, + struct component *s, + struct component *w, + struct component *e, + struct component *u, + struct component *d, + struct component *l) +{ + struct node3d *node3d = (struct node3d *)node; + node3d->n = n; + node3d->s = s; + node3d->w = w; + node3d->e = e; + node3d->u = u; + node3d->d = d; + node3d->l = l; + return OK; +} diff --git a/src/grid/source.mk b/src/grid/source.mk index e23d7db..79fe708 100644 --- a/src/grid/source.mk +++ b/src/grid/source.mk @@ -1 +1 @@ -SOURCES += src/grid/node.c +SOURCES += src/grid/node.c src/grid/node3d.c diff --git a/src/root.c b/src/root.c index 366822e..172728e 100644 --- a/src/root.c +++ b/src/root.c @@ -8,9 +8,6 @@ #define MAX_DOMAINS 512 -/** @todo ugly global, time values should be taken from local clock domain */ -uint64_t ticker = 0; - struct gran_root { size_t num_domains; struct clock_domain *(domains[MAX_DOMAINS]); diff --git a/src/torus3d/node.c b/src/torus3d/node.c index 15cc7ee..4111c48 100644 --- a/src/torus3d/node.c +++ b/src/torus3d/node.c @@ -20,6 +20,8 @@ struct torus3d_node { struct port port_x, port_y, port_z; struct reg x_in, y_in, z_in, child_in; + + bool prio; }; static stat port_receive(struct torus3d_node *torus3d, struct port *port, struct reg *reg) @@ -37,23 +39,15 @@ static stat port_receive(struct torus3d_node *torus3d, struct port *port, struct /* Dally/spiral routing though I'm a bit unsure if this works for 2D/3D * toruses (1D seems to work, 2D not so much atm) */ - int chan = 0; - if (port == &torus3d->port_x) - chan = sx < dx; - else if (port == &torus3d->port_y) - chan = sy < dy; - else if (port == &torus3d->port_z) - chan = sz < dz; - else - abort(); - + int chan = is_set(®->pkt, PACKET_DONE); if (port->r[chan].busy) return OK; - printf("(%d, %d, %d) to (%d, %d, %d) via (%d, %d, %d)\n", + printf("(%d, %d, %d) to (%d, %d, %d) via (%d, %d, %d) c %d\n", sx, sy, sz, dx, dy, dz, - torus3d->x, torus3d->y, torus3d->z); + torus3d->x, torus3d->y, torus3d->z, + chan); port->r[chan].pkt = reg->pkt; port->r[chan].busy = true; @@ -113,18 +107,23 @@ static void maybe_route_reg(struct torus3d_node *torus3d, struct reg *reg, enum } } -static void maybe_route_port(struct torus3d_node *torus3d, struct port *port, enum match m, uint64_t *oldest, struct packet **pkt, bool **busy) -{ - maybe_route_reg(torus3d, &port->r[0], m, oldest, pkt, busy); - maybe_route_reg(torus3d, &port->r[1], m, oldest, pkt, busy); -} - static stat route(struct torus3d_node *torus3d, struct component *next, enum match m) { + bool prio = torus3d->prio; uint64_t oldest = -1; struct packet *pkt = NULL; bool *busy = NULL; - maybe_route_port(torus3d, &torus3d->port_x, m, &oldest, &pkt, &busy); - maybe_route_port(torus3d, &torus3d->port_y, m, &oldest, &pkt, &busy); - maybe_route_port(torus3d, &torus3d->port_z, m, &oldest, &pkt, &busy); + + /* prioritise current priority port */ + maybe_route_reg(torus3d, &torus3d->port_x.r[prio], m, &oldest, &pkt, &busy); + maybe_route_reg(torus3d, &torus3d->port_y.r[prio], m, &oldest, &pkt, &busy); + maybe_route_reg(torus3d, &torus3d->port_z.r[prio], m, &oldest, &pkt, &busy); + + /* if no suitable match found, check other ports as well */ + if (pkt == NULL) { + maybe_route_reg(torus3d, &torus3d->port_x.r[!prio], m, &oldest, &pkt, &busy); + maybe_route_reg(torus3d, &torus3d->port_y.r[!prio], m, &oldest, &pkt, &busy); + maybe_route_reg(torus3d, &torus3d->port_z.r[!prio], m, &oldest, &pkt, &busy); + } + maybe_route_reg(torus3d, &torus3d->child_in, m, &oldest, &pkt, &busy); /* no suitable match */ @@ -143,6 +142,8 @@ static stat route(struct torus3d_node *torus3d, struct component *next, enum mat static stat torus3d_clock(struct torus3d_node *torus3d) { + torus3d->prio = !torus3d->prio; + stat ret = OK; if ((ret = port_receive(torus3d, &torus3d->port_x, &torus3d->x_in))) return ret; -- cgit v1.3