Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add benchmark for computing euclidean norm #275

Merged
merged 4 commits into from
Aug 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
114 changes: 114 additions & 0 deletions benchmarks/float/norm.bril
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
@pow(x:float, k:int):float
{
xx: float = const 1.0;
one: int = const 1;
i: int = const 0;
.while:
b: bool = lt i k;
br b .continue .endwhile;
.continue:
xx: float = fmul xx x;
i: int = add i one;
jmp .while;
.endwhile:
ret xx;
}

@n_root(x:float, n:int):float
{
one: int = const 1;
two_f: float = const 2.0;
xxx: float = fdiv x two_f;
n_minus_one: int = sub n one;
i: int = const 0;
num_iter: int = const 20;
.while:
b: bool = lt i num_iter;
br b .continue .endwhile;
.continue:
pow_n_minus_one: float = call @pow xxx n_minus_one;
pow_n: float = fmul pow_n_minus_one xxx;
numerator: float = fsub pow_n x;
denominator: float = fmul x pow_n_minus_one;
frac: float = fdiv numerator denominator;
xxx: float = fsub xxx frac;
i: int = add i one;
jmp .while;
.endwhile:
ret xxx;
}

# Modified @pack to handle floats
@pack(size: int, n1: float, n2: float, n3: float, n4: float, n5: float): ptr<float> {
one: int = const 1;
i: int = const 0;
array: ptr<float> = alloc size;
loc: ptr<float> = ptradd array i;
store loc n1;
i: int = add i one;
loc: ptr<float> = ptradd array i;
store loc n2;
i: int = add i one;
loc: ptr<float> = ptradd array i;
store loc n3;
i: int = add i one;
loc: ptr<float> = ptradd array i;
store loc n4;
i: int = add i one;
loc: ptr<float> = ptradd array i;
store loc n5;
ret array;
}

# Borrowed from benchmark suite on GitHub
@print_array(array: ptr<float>, size: int) {
i: int = const 0;
one: int = const 1;
.loop:
cond: bool = lt i size;
br cond .body .done;
.body:
loc: ptr<float> = ptradd array i;
val: float = load loc;
print val;
.loop_end:
i: int = add i one;
jmp .loop;
.done:
ret;
}

@euclidean_norm(array: ptr<float>, size: int): float {
i: int = const 0;
sum: float = const 0.0;
one: int = const 1;
.loop:
cond: bool = lt i size;
br cond .body .done;
.body:
loc: ptr<float> = ptradd array i;
val: float = load loc;
square: float = fmul val val;
sum: float = fadd sum square;
.loop_end:
i: int = add i one;
jmp .loop;
.done:
two: int = const 2;
norm: float = call @n_root sum two;
ret norm;
}

@main()
{
size: int = const 5;
n1: float = const 3;
n2: float = const 10;
n3: float = const 1;
n4: float = const 9;
n5: float = const 7;
array: ptr<float> = call @pack size n1 n2 n3 n4 n5;
norm: float = call @euclidean_norm array size;
print norm;
free array;
}
1 change: 1 addition & 0 deletions benchmarks/float/norm.out
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
110.55285076250027032
1 change: 1 addition & 0 deletions benchmarks/float/norm.prof
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
total_dyn_inst: 505
2 changes: 2 additions & 0 deletions docs/tools/bench.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ The current benchmarks are:
* `max-subarray`: solution to the classic Maximum Subarray problem.
* `mod_inv`: Calculates the [modular inverse][modinv] of `n` under to a prime modulus p.
* `newton`: Calculate the square root of 99,999 using the [newton method][newton]
* `norm`: Calculate the [euclidean norm][euclidean] of a vector
* `n_root`: Calculate nth root of a float using newton's method.
* `orders`: Compute the order ord(u) for each u in a cyclic group [<Zn,+>][cgroup] of integers modulo *n* under the group operation + (modulo *n*). Set the second argument *is_lcm* to true if you would like to compute the orders using the lowest common multiple and otherwise the program will use the greatest common divisor.
* `pascals-row`: Computes a row in Pascal's Triangle.
Expand Down Expand Up @@ -93,6 +94,7 @@ Credit for several of these benchmarks goes to Alexa VanHattum and Gregory Yaune
[palindrome]: https://en.wikipedia.org/wiki/Palindrome
[hanoi]: https://en.wikipedia.org/wiki/Tower_of_Hanoi
[euler]: https://en.wikipedia.org/wiki/E_(mathematical_constant)
[euclidean]: https://en.wikipedia.org/wiki/Norm_(mathematics)
[qsort]: https://en.wikipedia.org/wiki/Quicksort#Lomuto_partition_scheme
[modinv]: https://en.wikipedia.org/wiki/Modular_multiplicative_inverse
[totient]: https://en.wikipedia.org/wiki/Euler's_totient_function