bug fixes for evil garbage collection bugs...
[IRC.git] / Robust / src / Benchmarks / Prefetch / MatrixMultiply / MatrixMultiply.java
1 public class MatrixMultiply extends Thread{
2         MMul mmul;
3         public int x0, y0, x1, y1;
4
5         public MatrixMultiply(MMul mmul, int x0, int y0, int x1, int y1) {
6                 this.mmul = mmul;
7                 this.x0 = x0;
8                 this.y0 = y0;
9                 this.x1 = x1;
10                 this.y1 = y1;
11         }
12
13         public void run() {
14                 int localresults[][];
15
16                 atomic {
17                     //compute the results
18                     localresults=new int[1+x1-x0][1+y1-y0];
19                     
20                     for(int i = x0; i<= x1; i++){
21                         int a[]=mmul.a[i];
22                         int b[][]=mmul.b;
23                         int M=mmul.M;
24                         for (int j = y0; j <= y1; j++) {
25                             int innerProduct=0;
26                             for(int k = 0; k < M; k++) {
27                                 innerProduct += a[k] * b[k][j];
28                             }
29                             localresults[i-x0][j-y0]=innerProduct;
30                         }
31                     }
32                 }
33                 atomic {
34                     //write the results
35                     for(int i=x0;i<=x1;i++) {
36                         int c[]=mmul.c[i];
37                         for(int j=y0;j<=y1;j++) {
38                             c[j]=localresults[i-x0][j-y0];
39                         }
40                     }
41                 }
42         }
43
44         public static void main(String[] args) {
45                 int mid1 = (172<<24)|(16<<16)|(9<<8)|129;
46                 int mid2 = (128<<24)|(195<<16)|(175<<8)|69;
47                 int mid3 = (128<<24)|(195<<16)|(175<<8)|71;
48                 int NUM_THREADS = 2;
49                 int i, j, p, q, r;
50                 MatrixMultiply[] mm;
51                 MatrixMultiply tmp;
52                 MMul matrix;
53
54                 atomic {
55                         matrix = global new MMul(70, 70, 70);
56                         matrix.setValues();
57                 }
58
59                 atomic{
60                         mm = global new MatrixMultiply[NUM_THREADS];
61                 }
62
63                 // Currently it is a 70 X 70 matrix divided into 4 blocks 
64                 atomic {
65                         mm[0] = global new MatrixMultiply(matrix,0,0,69,35);
66                         mm[1] = global new MatrixMultiply(matrix,0,36,69,69);
67                 }
68
69                 atomic {
70                         p = matrix.L;
71                         q = matrix.M;
72                         r = matrix.N;
73                 }
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                 //Print Matrices to be multiplied
88                 /*
89                 System.printString("a =\n");
90                 for (i = 0; i < p; i++) {
91                         for (j = 0; j < q; j++) {
92                                 atomic {
93                                         val = matrix.a[i][j];
94                                 }
95                                 System.printString(" " + val);
96                         }
97                         System.printString("\n");
98                 }
99                 System.printString("\n");
100
101                 System.printString("b =\n");
102                 for (i = 0; i < q; i++) {
103                         for (j = 0; j < r; j++) {
104                                 atomic {
105                                         val = matrix.b[i][j];
106                                 }
107                                 System.printString(" " + val);
108                         }
109                         System.printString("\n");
110                 }
111                 System.printString("\n");
112                 */
113
114                 // start a thread to compute each c[l,n]
115                 for (i = 0; i < NUM_THREADS; i++) {
116                         atomic {
117                                 tmp = mm[i];
118                         }
119                         tmp.start(mid1);
120                 }
121
122                 // wait for them to finish
123                 for (i = 0; i < NUM_THREADS; i++) {
124                         atomic {
125                                 tmp = mm[i];
126                         }
127                         tmp.join();
128                 }
129
130                 // print out the result of the matrix multiply
131                 System.printString("Starting\n");
132                 System.printString("Matrix Product c =\n");
133                 StringBuffer sb = new StringBuffer("");
134                 int val;
135                 atomic {
136                         for (i = 0; i < p; i++) {
137                                 int c[]=matrix.c[i];
138                                 for (j = 0; j < r; j++) {
139                                         val = c[j];
140                                         sb.append(" ");
141                                         sb.append((new Integer(val)).toString());
142                                 }
143                                 sb.append("\n");
144                         }
145                 }
146                 System.printString(sb.toString());
147                 System.printString("Finished\n");
148         }
149 }
150
151 public class MMul{
152
153         public int L, M, N;
154         public int[][] a;
155         public int[][] b;
156         public int[][] c;
157
158         public MMul(int L, int M, int N) {
159                 this.L = L;
160                 this.M = M;
161                 this.N = N;
162                 a = global new int[L][M];  
163                 b = global new int[M][N]; 
164                 c = global new int[L][N]; 
165         }
166
167         public void setValues() {
168                 int i;
169                 int j;
170                 for(i = 0; i < L; i++) {
171                         for(j = 0; j < M; j++) {
172                                 a[i][j] = j+1;
173                         }
174                 }
175
176                 for(i = 0; i < M; i++) {
177                         for(j = 0; j < N; j++) {
178                                 b[i][j] = j+1;
179                         }
180                 }
181
182                 for(i = 0; i < L; i++) {
183                         for(j = 0; j < N; j++) {
184                                 c[i][j] = 0;
185                         }
186                 }
187         }
188 }
189