From c961822cb645238588a9893f2fead15c2ef313ff Mon Sep 17 00:00:00 2001 From: ehg54 Date: Wed, 29 May 2024 13:24:33 -0400 Subject: [PATCH] begin --- apps/blas/gemv/gemv.fil | 21 +++++++++++++ apps/blas/gemv/gemv.py | 23 ++++++++++++++ apps/blas/gemv/gemv_comb.fil | 58 ++++++++++++++++++++++++++++++++++++ 3 files changed, 102 insertions(+) create mode 100644 apps/blas/gemv/gemv.fil create mode 100644 apps/blas/gemv/gemv.py create mode 100644 apps/blas/gemv/gemv_comb.fil diff --git a/apps/blas/gemv/gemv.fil b/apps/blas/gemv/gemv.fil new file mode 100644 index 00000000..6740c2bc --- /dev/null +++ b/apps/blas/gemv/gemv.fil @@ -0,0 +1,21 @@ +import "apps/blas/util.fil"; + +comp Gemv[W, M, N, MultsN, AddsN]<'G:II>( + go: interface['G], + alpha: ['G, 'G+1] W, + beta: ['G, 'G+1] W, + A[M][N]: ['G, 'G+1] W + x[N]: ['G, 'G+1] W, + y[M]: ['G, 'G+1] W +) -> ( + y_out[M]: ['G+L, 'G+L+1] W +) with { + some L where L > 0; + some II where II > 0; +} where W > 0, + N > 0, + M > 0, + MultsN > 0, +{ + +} diff --git a/apps/blas/gemv/gemv.py b/apps/blas/gemv/gemv.py new file mode 100644 index 00000000..7ef7f81f --- /dev/null +++ b/apps/blas/gemv/gemv.py @@ -0,0 +1,23 @@ +from functools import reduce +from itertools import chain + +""" +For interop with fud, we represent matrices as integers. +The matrix [[1 2],[3 4]] gets flattened to [1,2,3,4]. Each int gets +converted to binary and concatenated, which is then interpreted as a +single integer. + +matrix: the matrix to convert to an integer +width: desired bitwidth of each integer in the matrix +""" +def matrix_to_int_repr(matrix, width): + flattened = list(chain.from_iterable(matrix)) + bin_str = reduce(lambda acc, i: acc + bin(i)[2:].zfill(width), flattened, "") + print(int(bin_str, 2)) + + +def main(): + matrix_to_int_repr([[1,2],[3,4]], 4) + +if __name__ == "__main__": + main() \ No newline at end of file diff --git a/apps/blas/gemv/gemv_comb.fil b/apps/blas/gemv/gemv_comb.fil new file mode 100644 index 00000000..38af962d --- /dev/null +++ b/apps/blas/gemv/gemv_comb.fil @@ -0,0 +1,58 @@ +import "apps/blas/util.fil"; + +// Combinational gemv kernel that uses M*N multipliers +comp Gemv_Comb[W, M, N]<'G:II>( + go: interface['G], + alpha: ['G, 'G+1] W, + beta: ['G, 'G+1] W, + A[M][N]: ['G, 'G+1] W + x[N]: ['G, 'G+1] W, + y[M]: ['G, 'G+1] W +) -> ( + y_out[M]: ['G, 'G+1] W +) with { + some L; + some II; +} where W > 0, + N > 0, + M > 0, + MultsN > 0, +{ + II := 1; + L := 0; + + bundle alpha_bundle[N]: ['G, 'G+1] W; + bundle alpha_A[M][N]: ['G, 'G+1] W; + + for i in 0..N { + alpha_bundle{i} = alpha; + } + + // do alpha * A + for i in 0..M { + Mults1 := new MultipliersComb[W, N]<'G>(alpha_bundle{0..N}, A{i}{0..N}) + alpha_A{i}{0..N} = Mults1.out{0..N}; + } + + bundle mv_prod[M][N]: ['G, 'G+1] W; + + // do alpha*A * x + for i in 0..M { + Mults2 := new MultipliersComb[W, N]<'G>(alpha_A{i}{0..N}, x{0..N}) + mv_prod{i}{0..N} = Mults2.out; + } + + // do adds for dot prods + + // do beta*y + bundle beta_bundle[M]: ['G, 'G+1] W; + bundle beta_y[M]: ['G, 'G+1] W: + + for i in 0..M { + beta_bundle{i} = beta; + } + + Mults3 := new MultipliersComb[W, M]<'G>(beta_bundle{0..M}, y{0..M}); + beta_y{0..M} = Mults3.out{0..M}; + +}