Skip to content

Commit

Permalink
Merge pull request #285 from jiahanxie353/main
Browse files Browse the repository at this point in the history
Benchmark task: bril program for finding the majority element
  • Loading branch information
sampsyo committed Sep 7, 2023
2 parents 459482c + 262ea10 commit 0301e69
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 0 deletions.
73 changes: 73 additions & 0 deletions benchmarks/mem/major-elm.bril
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
# Return the majority element (appears more than floor(n/2) times) of an array, assuming that the majority element is guaranteed to exist.
# Inputs: an array of size 3 (fixed);
# Output: the majority element of the array.
# Not a big example in terms of the array size and the total dynamic instructions; but I found Boyer-Moore voting algorithm very interesting!
# Acknowledgement: brought idea from the pack function in Pat-Lafon's binary-search.bril to my create_arr.
@main() {
arr_size: int = const 3;
e1: int = const 3;
e2: int = const 2;
e3: int = const 3;
nums: ptr<int> = call @create_arr arr_size e1 e2 e3;

zero: int = const 0;
one: int = const 1;
first_elm_ptr: ptr<int> = ptradd nums zero;
major_elm: int = load first_elm_ptr;
count: int = const 1;

i: int = const 1;
.check_bound:
end_cond: bool = ge i arr_size;
br end_cond .end .body;

.body:
cur_ptr: ptr<int> = ptradd nums i;
cur_val: int = load cur_ptr;
cur_major_cond: bool = eq cur_val major_elm;
br cur_major_cond .incr_count .body.else;

.incr_count:
count: int = add count one;
i: int = add i one;
jmp .check_bound;

.body.else:
cnt_eq_0: bool = eq count zero;
br cnt_eq_0 .eq_zero_if .eq_zero_else;

.eq_zero_if:
cur_ptr: ptr<int> = ptradd nums i;
major_elm: int = load cur_ptr;
count: int = add count one;
i: int = add i one;
jmp .check_bound;

.eq_zero_else:
count: int = sub count one;
i: int = add i one;
jmp .check_bound;

.end:
free nums;
print major_elm;
}

@create_arr(size: int, e1: int, e2: int, e3: int): ptr<int> {
one: int = const 1;
i: int = const 0;
array: ptr<int> = alloc size;

loc: ptr<int> = ptradd array i;
store loc e1;

i: int = add i one;
loc: ptr<int> = ptradd array i;
store loc e2;

i: int = add i one;
loc: ptr<int> = ptradd array i;
store loc e3;

ret array;
}
1 change: 1 addition & 0 deletions benchmarks/mem/major-elm.out
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
3
1 change: 1 addition & 0 deletions benchmarks/mem/major-elm.prof
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
total_dyn_inst: 47
1 change: 1 addition & 0 deletions docs/tools/bench.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ The current benchmarks are:
* `hanoi`: Print the solution to the *n*-disk [Tower of Hanoi][hanoi] puzzle.
* `lcm`: Compute LCM for two numbers using a very inefficient loop.
* `loopfact`: Compute *n!* imperatively using a loop.
* `major-elm`: Find the majority element in an array using [a linear time voting algorithm](https://www.cs.utexas.edu/~moore/best-ideas/mjrty/).
* `mandelbrot`: Generates a really low resolution, ascii, [mandelbrot set][mandelbrot].
* `mat-inv` : Calculates the inverse of a 3x3 matrix and prints it out.
* `mat-mul`: Multiplies two `nxn` matrices using the [naive][matmul] matrix multiplication algorithm. The matrices are randomly generated using a [linear congruential generator][rng].
Expand Down

0 comments on commit 0301e69

Please sign in to comment.