1 public class MatrixMultiply {
3 public int x0, y0, x1, y1;
4 public MatrixMultiply(MMul mmul, int x0, int x1, int y0, int y1) {
15 double lb[][]=mmul.btranspose;
18 //Use btranspose for cache performance
19 for(int i = x0; i< x1; i++){
22 for (int j = y0; j < y1; j++) {
23 double innerProduct=0;
25 for(int k = 0; k < M; k++) {
26 innerProduct += a[k] *b[k];
33 public static void main(String[] args) {
37 NUM_THREADS=Integer.parseInt(args[0]);
39 SIZE=Integer.parseInt(args[1]);
43 int[] mid = new int[8];
44 mid[0] = (128<<24)|(195<<16)|(175<<8)|84; //dw-10
45 mid[1] = (128<<24)|(195<<16)|(175<<8)|85; //dw-11
46 mid[2] = (128<<24)|(195<<16)|(175<<8)|86; //dw-12
47 mid[3] = (128<<24)|(195<<16)|(175<<8)|87; //dw-13
48 mid[4] = (128<<24)|(195<<16)|(175<<8)|88; //dw-14
49 mid[5] = (128<<24)|(195<<16)|(175<<8)|89; //dw-15
50 mid[6] = (128<<24)|(195<<16)|(175<<8)|90; //dw-16
51 mid[7] = (128<<24)|(195<<16)|(175<<8)|91; //dw-17
58 matrix = new MMul(SIZE, SIZE, SIZE);
61 mm = new MatrixMultiply[NUM_THREADS];
62 int increment=SIZE/NUM_THREADS;
64 for(int i=0;i<NUM_THREADS;i++) {
65 if ((i+1)==NUM_THREADS)
66 mm[i]= new MatrixMultiply(matrix,base, SIZE, 0, SIZE);
68 mm[i]= new MatrixMultiply(matrix,base, base+increment, 0, SIZE);
75 // print out the matrices to be multiplied
76 System.printString("\n");
77 System.printString("MatrixMultiply: L=");
79 System.printString("\t");
80 System.printString("M=");
82 System.printString("\t");
83 System.printString("N=");
85 System.printString("\n");
87 for(int i=0;i<NUM_THREADS;i++) {
91 // print out the result of the matrix multiply
92 System.printString("Finished\n");
102 public double[][] btranspose;
104 public MMul(int L, int M, int N) {
108 a = new double[L][M];
109 b = new double[M][N];
110 c = new double[L][N];
111 btranspose = new double[N][M];
114 public void setValues() {
115 for(int i = 0; i < L; i++) {
117 for(int j = 0; j < M; j++) {
122 for(int i = 0; i < M; i++) {
124 for(int j = 0; j < N; j++) {
129 for(int i = 0; i < L; i++) {
131 for(int j = 0; j < N; j++) {
135 for(int i = 0; i < N; i++) {
136 double btransposei[] = btranspose[i];
137 for(int j = 0; j < M; j++) {
143 public void transpose() {
144 for(int row = 0; row < M; row++) {
145 double brow[] = b[row];
146 for(int col = 0; col < N; col++) {
147 btranspose[col][row] = brow[col];