From c6ada00d96b5f4158e3fe3760ca49b8a0d468f64 Mon Sep 17 00:00:00 2001 From: Gerardo Teruel Date: Wed, 29 Jan 2025 18:31:00 -0500 Subject: [PATCH 1/2] [benchmark] add conway's game of life --- benchmarks/mixed/gol.bril | 280 ++++++++++++++++++++++++++++++++++++++ benchmarks/mixed/gol.out | 19 +++ benchmarks/mixed/gol.prof | 1 + docs/tools/bench.md | 1 + 4 files changed, 301 insertions(+) create mode 100644 benchmarks/mixed/gol.bril create mode 100644 benchmarks/mixed/gol.out create mode 100644 benchmarks/mixed/gol.prof diff --git a/benchmarks/mixed/gol.bril b/benchmarks/mixed/gol.bril new file mode 100644 index 000000000..ba2db5c78 --- /dev/null +++ b/benchmarks/mixed/gol.bril @@ -0,0 +1,280 @@ +# Given n and a seed. +# Will generate an nxn matrix with random values using seed. +# Will print the randomly generated matrix. (13, index, value) +# Print the number 3 as a separator. +# And print the the next matrix in Conway's game of life. (13, index, value) +# ARGS: 3 4 +@main(n: int, seed: int) { + id: int = const 3; + one: int = const 1; + rng: ptr = alloc one; + store rng seed; + + msize: int = mul n n; + arr: ptr = call @rand_array msize rng; + call @print_array msize arr; + + print id; + + #call @test_neighbors msize n arr; + dest: ptr = alloc msize; + call @next_board msize n arr dest; + call @print_array msize dest; + + free rng; + free arr; + free dest; +} + +@next_board(msize: int, n: int, board: ptr, dest: ptr) { + zero: int = const 0; + one: int = const 1; + i: int = const 0; +.compare: + exit: bool = ge i msize; + br exit .exit .continue; +.continue: + curr: ptr = ptradd board i; + cval: int = load curr; + neighbors: int = call @alive msize n i board; + nval: int = call @next_cell cval neighbors; + nptr: ptr = ptradd dest i; + store nptr nval; + i: int = add i one; + jmp .compare; +.exit: +} + +@next_cell(x: int, neighbors: int): int { + one: int = const 1; + two: int = const 2; + three: int = const 3; + four: int = const 4; + x_alive: bool = eq x one; + br x_alive .alive .dead; + res: int = const 0; + +.alive: + dies: bool = lt neighbors two; + br dies .should_die .alive1; + +.alive1: + lives: bool = lt neighbors four; + br lives .should_live .should_die; + +.dead: + lives: bool = eq neighbors three; + br lives .should_live .should_die; + +.should_live: + res: int = const 1; + jmp .exit; + +.should_die: + res: int = const 0; + jmp .exit; + +.exit: + ret res; +} + +@test_neighbors(msize: int, n: int, board: ptr) { + id: int = const 223; + i: int = const 0; + one: int = const 1; +.comparison: + exit: bool = ge i msize; + br exit .exit .body; + +.body: + alive: int = call @alive msize n i board; + print id i alive; + i: int = add i one; + jmp .comparison; + +.exit: + +} + +@alive(msize: int, n: int, i: int, board: ptr): int { + id: int = const 7; + zero: int = const 0; + one: int = const 1; + sum: int = const 0; + skip: bool = lt i n; + br skip .d .a; +.a: + pos: int = sub i n; + loc: int = const 1; + #print id loc pos; + curr: ptr = ptradd board pos; + cval: int = load curr; + sum = add sum cval; + + modulo: int = call @mod pos n; + lw: bool = eq modulo zero; + br lw .a.bis .b; +.a.bis: + pos: int = sub i n; + modulo: int = call @mod pos n; + modulo: int = add modulo one; + rw: bool = eq modulo n; + br rw .d .c; +.b: + pos: int = sub i n; + pos: int = sub pos one; + loc: int = const 2; + #print id loc pos; + curr: ptr = ptradd board pos; + cval: int = load curr; + sum: int = add sum cval; + jmp .a.bis; + +.c: + pos: int = sub i n; + pos: int = add pos one; + loc: int = const 3; + #print id loc pos; + curr: ptr = ptradd board pos; + cval: int = load curr; + sum: int = add sum cval; + jmp .d; + +.d: + modulo: int = call @mod i n; + lw: bool = eq modulo zero; + br lw .d.bis .e; + +.d.bis: + modulo: int = call @mod i n; + modulo: int = add modulo one; + rw: bool = eq modulo n; + br rw .g .f; + +.e: + pos: int = sub i one; + loc: int = const 4; + #print id loc pos; + curr: ptr = ptradd board pos; + cval: int = load curr; + sum: int = add sum cval; + jmp .d.bis; + +.f: + pos: int = add i one; + loc: int = const 5; + #print id loc pos; + curr: ptr = ptradd board pos; + cval: int = load curr; + sum: int = add sum cval; + jmp .g; + +.g: + next_pos: int = add i n; + exit: bool = ge next_pos msize; + br exit .exit .g.bis; + +.g.bis: + modulo: int = call @mod i n; + lw: bool = eq modulo zero; + br lw .h .g.bis2; + +.g.bis2: + pos: int = add i n; + pos: int = sub pos one; + loc: int = const 6; + #print id loc pos; + curr: ptr = ptradd board pos; + cval: int = load curr; + sum: int = add sum cval; + jmp .h; + +.h: + pos: int = add i n; + loc: int = const 7; + curr: ptr = ptradd board pos; + cval: int = load curr; + sum: int = add sum cval; + + modulo: int = call @mod i n; + modulo: int = add modulo one; + rw: bool = eq modulo n; + br rw .exit .i; + +.i: + pos: int = add i n; + pos: int = add pos one; + loc: int = const 8; + curr: ptr = ptradd board pos; + cval: int = load curr; + sum: int = add sum cval; + jmp .exit; +.exit: + ret sum; + +} + +# from mat-mul.bril +# adjusted modulo operation to prevent negative numbers +# adjusted a, c, m - with values from L'Ecuyer 1999. +@rand(seq: ptr, max: int) : int { + a: int = const 1588635695; + c: int = const 0; + m: int = const 4294967291; + x: int = load seq; + ax: int = mul a x; + axpc: int = add ax c; + next: int = call @mod axpc m; + store seq next; + val: int = call @mod next max; + ret val; +} + +@rand_array(n: int, rng: ptr): ptr { + one: int = const 1; + two: int = const 2; + i: int = const 0; + arr: ptr = alloc n; +.compare: + done: bool = ge i n; + br done .exit .continue; +.continue: + r: int = call @rand rng two; + curr: ptr = ptradd arr i; + store curr r; + i: int = add i one; + jmp .compare; +.exit: + ret arr; +} + +@print_array(n: int, arr: ptr) { + zero: int = const 0; + one: int = const 1; + i: int = const 0; + last: int = sub n one; + id: int = const 13; +.compare: + exit: bool = ge i n; + br exit .exit .continue; +.continue: + curr: ptr = ptradd arr i; + val: int = load curr; + print id i val; + i: int = add i one; + jmp .compare; +.exit: +} + +@mod(a: int, b: int): int { + quotient: int = div a b; + whole: int = mul quotient b; + remainder: int = sub a whole; + zero: int = const 0; + exit: bool = ge remainder zero; + br exit .exit .adjust; +.adjust: + remainder: int = add remainder b; +.exit: + ret remainder; +} diff --git a/benchmarks/mixed/gol.out b/benchmarks/mixed/gol.out new file mode 100644 index 000000000..6754a8ae9 --- /dev/null +++ b/benchmarks/mixed/gol.out @@ -0,0 +1,19 @@ +13 0 1 +13 1 1 +13 2 1 +13 3 1 +13 4 0 +13 5 1 +13 6 1 +13 7 0 +13 8 0 +3 +13 0 1 +13 1 0 +13 2 1 +13 3 1 +13 4 0 +13 5 1 +13 6 0 +13 7 1 +13 8 0 diff --git a/benchmarks/mixed/gol.prof b/benchmarks/mixed/gol.prof new file mode 100644 index 000000000..7bde05458 --- /dev/null +++ b/benchmarks/mixed/gol.prof @@ -0,0 +1 @@ +total_dyn_inst: 1425 diff --git a/docs/tools/bench.md b/docs/tools/bench.md index 1634619d3..bfbca3545 100644 --- a/docs/tools/bench.md +++ b/docs/tools/bench.md @@ -35,6 +35,7 @@ The current benchmarks are: * `fizz-buzz`: The infamous [programming test][fizzbuzz]. * `function_call`: For benchmarking the overhead of simple function calls. * `gcd`: Calculate Greatest Common Divisor (GCD) of two input positive integer using [Euclidean algorithm][euclidean_into]. +* `gol`: Print the next iteration for a matrix in [Conway's Game of Life](https://en.wikipedia.org/wiki/Conway%27s_Game_of_Life). * `hanoi`: Print the solution to the *n*-disk [Tower of Hanoi][hanoi] puzzle. * `is-decreasing`: Print if a number contains strictly decreasing digits. * `lcm`: Compute LCM for two numbers using a very inefficient loop. From 39a31a7c46929d7bb9a23d45ee6b8747196dca57 Mon Sep 17 00:00:00 2001 From: Gerardo Teruel Date: Thu, 30 Jan 2025 12:02:24 -0500 Subject: [PATCH 2/2] fix mixing variable type --- benchmarks/mixed/gol.bril | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/benchmarks/mixed/gol.bril b/benchmarks/mixed/gol.bril index ba2db5c78..385a75cf2 100644 --- a/benchmarks/mixed/gol.bril +++ b/benchmarks/mixed/gol.bril @@ -109,7 +109,7 @@ #print id loc pos; curr: ptr = ptradd board pos; cval: int = load curr; - sum = add sum cval; + sum: int = add sum cval; modulo: int = call @mod pos n; lw: bool = eq modulo zero;