(original) (raw)

import java.util.Random; //import java.lang.Math; //import java.util.stream.Stream; import java.util.stream.IntStream; public class MatrixMultiply { static int ITER = 1000; //10000; //600000 static int INIT = 1000; static int SIZE = 64 * 4; //8; //16; //32; //64; //static double[] in, out; static Boolean Transpose = false; static double[] x; public static void main(String[] args) { long start = 0; //used for clock long elapsedTimeMillis; for (int i = 0; i < args.length; i++) { String arg = args[i]; if (arg.length() > 1 && arg.charAt(0) == '-') { arg = arg.substring(1); if (arg.contentEquals("Transpose")) { Transpose = true; } } }//for double[] A = new double[SIZE * SIZE]; double[] B = new double[SIZE * SIZE]; double[] AxB = new double[SIZE * SIZE]; double[] AxB_T = new double[SIZE * SIZE]; double[] T = new double[SIZE * SIZE]; x = new double[SIZE]; Random randA = new Random(435437646); Random randB = new Random(376464354); for (int i = 0; i < A.length; i++) { A[i] = randA.nextDouble(); B[i] = randB.nextDouble(); } //for (int j = 0; j < INIT; j++) //{ // multiply(AxB, A, B, SIZE, T); //} // System.out.println("Warmup done..."); Transpose = true; start = System.currentTimeMillis(); for (int j = 0; j < ITER; j++) { multiply_transpose(AxB_T, A, B, SIZE, T); } elapsedTimeMillis = System.currentTimeMillis() - start; System.out.println("Multiply with Transpose: iterations Per milli Second:" + (60000.0) / elapsedTimeMillis + " ipms"); Transpose = false; start = System.currentTimeMillis(); for (int j = 0; j < ITER; j++) { multiply(AxB, A, B, SIZE, T); } elapsedTimeMillis = System.currentTimeMillis() - start; System.out.println("Multiply without Transpose: iterations Per milli Second:" + (60000.0) / elapsedTimeMillis + " ipms"); matrix_cmp(AxB_T, AxB, SIZE); double sum = 0; for (int i = 0; i < AxB.length; i++) { //sum += AxB[i]; } System.out.println("Sum:" + sum); }//main static void matrix_cmp(double[] left, double[] right, int cols) { for (int i = 0; i < cols; i++) { for (int j = 0; j < cols; j++) { if (left[i * cols + j] != right[i * cols + j]) { System.out.println("matrix_cmp: miscompare for element [" + i + "," + j + "]: " + left[i * cols + j] + " != " + right[i * cols + j]); } } } } static void multiply(double[] result, double[] left, double[] right, int cols, double[] T) { assert (left.length == right.length); assert (left.length == cols * cols); assert (result.length == cols * cols); //IntStream.range(0, cols * cols).parallel().forEach(id -> { // int i = id / cols; // int j = id % cols; // double sum = aXb(left, right, cols, i, j); // result[i * cols + j] = sum; //}); for (int id = 0; id < cols * cols; id++) { int i = id / cols; int j = id % cols; double sum = aXb(left, T, cols, i, j); result[i * cols + j] = sum; } } static void multiply_transpose(double[] result, double[] left, double[] right, int cols, double[] T) { assert (left.length == right.length); assert (left.length == cols * cols); assert (result.length == cols * cols); transpose(right, T, cols); //IntStream.range(0, cols * cols).parallel().forEach(id -> { // int i = id / cols; // int j = id % cols; // double sum = aYb(left, T, cols, i, j); // result[i * cols + j] = sum; //}); for (int id = 0; id < cols * cols; id++) { int i = id / cols; int j = id % cols; double sum = aYb(left, T, cols, i, j); result[i * cols + j] = sum; } } static void transpose(double[] X, double[] temp, int cols) { for (int i = 0; i < cols; i++) { for (int j = 0; j < cols; j++) { temp[i * cols + j] = X[i + cols * j]; } } X = temp; } static double aXb(double[] left, double[] right, int cols, int i, int j) { double sum = 0; for (int k = 0; k < cols; k++) { //sum += left[i * cols + k] * right[k * cols + j]; sum += left[k + i * cols] * right[k + j * cols]; } return sum; } static double aYb(double[] left, double[] right, int cols, int i, int j) { double sum = 0; for (int k = 0; k < cols; k++) { //x[k] = left[k + i * cols] * right[k + j * cols]; //sum += x[k]; sum += left[k + i * cols] * right[k + j * cols]; } //for (int k = 0; k < cols; k++) { // sum += x[k]; //} return sum; } }//public class MatrixMultiply {