From 3083284c797fc8fc267144b05c8a58395e4583e3 Mon Sep 17 00:00:00 2001 From: Kimplul Date: Sat, 9 Aug 2025 16:06:43 +0300 Subject: add 1d mesh node and refactor 2d mesh node + Seems to decrease performance a little bit, presumably due to extra register copy, but simplifies code a lot and opens up more genericism so I'll consider it an upgrade for now. Copying packets around is rather slow though, might in the future move to some kind of pointer based packet handling --- tests/simple_mesh1d/sim.c | 88 +++++++++++++++++++++++++++++++++++++++++++ tests/simple_mesh1d/source.mk | 19 ++++++++++ tests/simple_mesh1d/test.c | 66 ++++++++++++++++++++++++++++++++ 3 files changed, 173 insertions(+) create mode 100644 tests/simple_mesh1d/sim.c create mode 100644 tests/simple_mesh1d/source.mk create mode 100644 tests/simple_mesh1d/test.c (limited to 'tests/simple_mesh1d') diff --git a/tests/simple_mesh1d/sim.c b/tests/simple_mesh1d/sim.c new file mode 100644 index 0000000..c5f7290 --- /dev/null +++ b/tests/simple_mesh1d/sim.c @@ -0,0 +1,88 @@ +#include + +#include +#include +#include +#include +#include +#include + +#include "../build/tests/simple_mesh1d/test.inc" + +static stat build_node1d(struct clock_domain *clk, uint16_t x, uint16_t y) +{ + struct component **mesh = calloc(x + 1, sizeof(struct component *)); + assert(mesh); + + for (int i = 1; i < x + 1; ++i) { + /* 4 CPUs + 1 mem = 5 elems in total */ + struct component *node = create_mesh_node1d(i, y + 1); + clock_domain_add(clk, node); + mesh[i] = node; + + for (int j = 0; j < y; ++j) { + struct component *imem = create_simple_mem(4096); + init_simple_mem(imem, 0, + build_tests_simple_mesh1d_test_bin_len, + build_tests_simple_mesh1d_test_bin); + + uint64_t rcv = mesh1d_addr(i, j, 0); + struct component *rv64 = create_simple_riscv64(rcv, 0, imem, node); + simple_riscv64_set_reg(rv64, 10, i); /* a0 */ + simple_riscv64_set_reg(rv64, 11, j); /* a1 */ + simple_riscv64_set_reg(rv64, 12, x); /* a2 */ + simple_riscv64_set_reg(rv64, 13, y); /* a3 */ + + clock_domain_add(clk, rv64); + clock_domain_add(clk, imem); + + mesh_node1d_connect(node, rv64, j); + } + + struct component *dmem = create_simple_mem(4096); + clock_domain_add(clk, dmem); + mesh_node1d_connect(node, dmem, 4); + } + + /* extra I/O node (kind of?) */ + struct component *node = create_mesh_node1d(0, 2); + clock_domain_add(clk, node); + mesh[0] = node; + + struct component *uart = create_simple_uart(); + clock_domain_add(clk, uart); + mesh_node1d_connect(node, uart, 0); + + struct component *dmem = create_simple_mem(4096); + clock_domain_add(clk, dmem); + mesh_node1d_connect(node, dmem, 1); + + for (int i = 0; i < x + 1; ++i) { + if (i - 1 >= 0) + mesh_node1d_connect_right(mesh[i], mesh[i - 1]); + + if (i + 1 < x + 1) + mesh_node1d_connect_left(mesh[i], mesh[i + 1]); + } + + free(mesh); + return OK; +} + +int main() +{ + struct clock_domain *clk = create_clock_domain(NS(1)); + + /* one cluster with four cores + mem for each core (should + * instructions be fetched from cluster local mem?) */ + stat r = build_node1d(clk, 8, 8); + assert(r == OK); + + struct gran_root *root = create_root(); + root_add_clock(root, clk); + + r = root_run(root); + assert(r == OK); + + destroy_root(root); +} diff --git a/tests/simple_mesh1d/source.mk b/tests/simple_mesh1d/source.mk new file mode 100644 index 0000000..5013c6e --- /dev/null +++ b/tests/simple_mesh1d/source.mk @@ -0,0 +1,19 @@ +MESH1D_TEST_OBJ != ./scripts/gen-deps --sources "tests/simple_mesh1d/sim.c" +TEST_PROGS += build/tests/simple_mesh1d/sim + +build/tests/simple_mesh1d/test.inc: tests/simple_mesh1d/test.c + riscv64-unknown-elf-gcc -O2 -Wall -Wextra -ffreestanding -nostdlib \ + -march=rv64i -mabi=lp64 \ + -fno-delete-null-pointer-checks \ + -o build/tests/simple_mesh1d/test \ + tests/simple_mesh1d/test.c + riscv64-unknown-elf-objcopy -Obinary \ + build/tests/simple_mesh1d/test \ + build/tests/simple_mesh1d/test.bin + xxd -i build/tests/simple_mesh1d/test.bin \ + > build/tests/simple_mesh1d/test.inc + +build/tests/simple_mesh1d/sim.o: build/tests/simple_mesh1d/test.inc + +build/tests/simple_mesh1d/sim: $(MESH1D_TEST_OBJ) $(OBJS) + $(COMPILE) $(MESH1D_TEST_OBJ) $(OBJS) -o $@ diff --git a/tests/simple_mesh1d/test.c b/tests/simple_mesh1d/test.c new file mode 100644 index 0000000..b3de347 --- /dev/null +++ b/tests/simple_mesh1d/test.c @@ -0,0 +1,66 @@ +#include + +__attribute__((always_inline)) +static inline uint64_t extreme_numa_addr(uint16_t cluster, uint16_t elem, uint32_t off) +{ + return ((uint64_t)cluster << 48) | ((uint64_t)elem << 32) | off; +} + +__attribute__((always_inline)) +static inline void print_int8(volatile char *uart, unsigned x) +{ + char lo_nibble = (x >> 0) & 0xf; + char hi_nibble = (x >> 4) & 0xf; + + *uart = hi_nibble < 10 ? hi_nibble + '0' : (hi_nibble - 10) + 'a'; + *uart = lo_nibble < 10 ? lo_nibble + '0' : (lo_nibble - 10) + 'a'; +} + +__attribute__((always_inline)) +static inline void print_addr(volatile char *uart, unsigned x, unsigned y) +{ + *uart = '('; + print_int8(uart, x); + *uart = ','; + *uart = ' '; + print_int8(uart, y); + *uart = ')'; + *uart = '\n'; +} + +__attribute__((always_inline)) +static inline uint64_t wrap(unsigned x, unsigned X) +{ + return x + 1 >= X ? 0 : x + 1; +} + +__attribute__((always_inline)) +static inline uint64_t next_idx(unsigned x, unsigned y, unsigned X, unsigned Y) +{ + unsigned yi = wrap(y, Y); + unsigned xi = yi < y ? wrap(x, X) : x; + + return extreme_numa_addr(xi, yi, 0); +} + +void _start(unsigned x, unsigned y, unsigned X, unsigned Y) +{ + volatile char *uart = (char *)extreme_numa_addr(0, 0, 0); + volatile uint64_t *control = (uint64_t *)extreme_numa_addr(0, 1, 0); + + if (x == 1 && y == 0) { + goto do_work; + } else { + while (*control != extreme_numa_addr(x, y, 0)) {} + } + +do_work: + print_addr(uart, x, y); + *control = next_idx(x, y, X, Y); + + if (x == X - 1 && y == Y - 1) + asm("ebreak"); + + /* otherwise just loop */ + while (1) {} +} -- cgit v1.3