changes in benchmarks for the PPoPP paper
[IRC.git] / Robust / src / Benchmarks / Prefetch / MatrixMultiply / javasingle / MatrixMultiplyN.java
1 public class MatrixMultiply {
2   MMul mmul;
3   public int x0, y0, x1, y1;
4   public MatrixMultiply(MMul mmul, int x0, int x1, int y0, int y1) {
5     this.mmul = mmul;
6     this.x0 = x0;
7     this.y0 = y0;
8     this.x1 = x1;
9     this.y1 = y1;
10   }
11
12   public void run() {
13     double la[][]=mmul.a;
14     double lc[][]=mmul.c;
15     double lb[][]=mmul.btranspose;
16     int M=mmul.M;
17
18     //Use btranspose for cache performance
19     for(int i = x0; i< x1; i++){
20       double a[]=la[i];
21       double c[]=lc[i];
22       for (int j = y0; j < y1; j++) {
23         double innerProduct=0;
24         double b[] = lb[j];
25         for(int k = 0; k < M; k++) {
26           innerProduct += a[k] *b[k];
27         }
28         c[j]=innerProduct;
29       }
30     }
31   }
32
33   public static void main(String[] args) {
34     int NUM_THREADS = 4;
35     int SIZE=600;
36     if (args.length>0) {
37       NUM_THREADS=Integer.parseInt(args[0]);
38       if (args.length>1)
39         SIZE=Integer.parseInt(args[1]);
40     }
41
42     /*
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
52     */
53     int p, q, r;
54     MatrixMultiply[] mm;
55     MatrixMultiply tmp;
56     MMul matrix;
57
58     matrix = new MMul(SIZE, SIZE, SIZE);
59     matrix.setValues();
60     matrix.transpose();
61     mm = new MatrixMultiply[NUM_THREADS];
62     int increment=SIZE/NUM_THREADS;
63     int base=0;
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);
67       else
68         mm[i]= new MatrixMultiply(matrix,base, base+increment, 0, SIZE);
69       base+=increment;
70     }
71     p = matrix.L;
72     q = matrix.M;
73     r = matrix.N;
74
75     // print out the matrices to be multiplied
76     System.printString("\n");
77     System.printString("MatrixMultiply: L=");
78     System.printInt(p);
79     System.printString("\t");
80     System.printString("M=");
81     System.printInt(q);
82     System.printString("\t");
83     System.printString("N=");
84     System.printInt(r);
85     System.printString("\n");
86
87     for(int i=0;i<NUM_THREADS;i++) {
88       mm[i].run();
89     }
90
91     // print out the result of the matrix multiply
92     System.printString("Finished\n");
93   }
94 }
95
96 public class MMul{
97
98   public int L, M, N;
99   public double[][] a;
100   public double[][] b;
101   public double[][] c;
102   public double[][] btranspose;
103
104   public MMul(int L, int M, int N) {
105     this.L = L;
106     this.M = M;
107     this.N = 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];
112   }
113
114   public void setValues() {
115     for(int i = 0; i < L; i++) {
116       double ai[] = a[i];
117       for(int j = 0; j < M; j++) {
118         ai[j] = j+1;
119       }
120     }
121
122     for(int i = 0; i < M; i++) {
123       double bi[] = b[i];
124       for(int j = 0; j < N; j++) {
125         bi[j] = j+1;
126       }
127     }
128
129     for(int i = 0; i < L; i++) {
130       double ci[] = c[i];
131       for(int j = 0; j < N; j++) {
132         ci[j] = 0;
133       }
134     }
135     for(int i = 0; i < N; i++) {
136       double btransposei[] = btranspose[i];
137       for(int j = 0; j < M; j++) {
138         btransposei[j] = 0;
139       }
140     }
141   }
142
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];
148       }
149     }
150   }
151 }