diff --git a/matmul/build.sh b/matmul/build.sh index 5551dbfb..b2e6179c 100644 --- a/matmul/build.sh +++ b/matmul/build.sh @@ -14,6 +14,7 @@ javac matmul.java kotlinc matmul.kt -include-runtime -d matmul-kt.jar mcs -debug- -optimize+ matmul.cs dotnet build -c Release +valac -X -O3 -o matmul_vala matmul.vala # numpy for matrix mult in python # brew install numpy diff --git a/matmul/matmul.vala b/matmul/matmul.vala new file mode 100644 index 00000000..02b32b9d --- /dev/null +++ b/matmul/matmul.vala @@ -0,0 +1,45 @@ +void main (string[] args) { + int n = 100; + if (args.length > 1) { + n = int.parse(args[1]); + } + n = n / 2 * 2; + double[,] a = matgen(n); + double[,] b = matgen(n); + double[,] x = matmul(a, b); + print("%f\n", x[n/2,n/2]); +} + +double[,] matgen (int n) { + double[,] arr = new double[n,n]; + double tmp = 1.0 / n / n; + for (int i = 0; i < n; ++i) { + for (int j = 0; j < n; ++j) { + arr[i,j] = tmp * (i - j) * (i + j); + } + } + return arr; +} + +double[,] matmul (double[,] a, double[,] b) { + int m = a.length[0]; + int n = a.length[1]; + int p = b.length[1]; + double[,] x = new double[m,p]; + double[,] c = new double[p,n]; + for (int i = 0; i < n; ++i) { // transpose + for (int j = 0; j < p; ++j) { + c[j,i] = b[i,j]; + } + } + for (int i = 0; i < m; ++i) { + for (int j = 0; j < p; ++j) { + double s = 0.0; + for (int k = 0; k < n; ++k) { + s += a[i,k] * c[j,k]; + } + x[i,j] = s; + } + } + return x; +} diff --git a/matmul/run.sh b/matmul/run.sh index 5aaf5954..b319ad34 100644 --- a/matmul/run.sh +++ b/matmul/run.sh @@ -56,3 +56,5 @@ echo Tcl ../xtime.rb tclsh matmul.tcl 1500 echo Swift ../xtime.rb swift -O matmul.swift 1500 +echo Vala +../xtime.rb ./matmul_vala 1500