bug fix for global variables
[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 innerProduct = 0;
15                 int i, j;
16                 int xx0,xx1,yy0,yy1;
17
18                 atomic {
19                         xx0 = x0;
20                         xx1 = x1;
21                         yy0 = y0;
22                         yy1 = y1;
23                 }
24
25                 for(i = xx0; i<= xx1; i++){
26                         for (j = yy0; j <= yy1; j++) {
27                                 atomic {
28                                         innerProduct = mmul.multiply(i,j);
29                                 }
30                                 atomic {
31                                         mmul.c[i][j] = innerProduct;
32                                 }
33                         }
34                 }
35         }
36
37         public static void main(String[] args) {
38                 int mid = (128<<24)|(195<<16)|(175<<8)|70;
39                 int NUM_THREADS = 4;
40                 int i, j, p, q, r, val;
41                 MatrixMultiply[] mm;
42                 MatrixMultiply tmp;
43                 MMul matrix;
44
45                 atomic {
46                         matrix = global new MMul(4, 4, 4);
47                         matrix.setValues();
48                 }
49
50                 atomic{
51                         mm = global new MatrixMultiply[NUM_THREADS];
52                         mm.mmul = global new MMul(0, 0, 0);
53                 }
54
55                 // Currently it is a 4 X 4 matrix divided into 4 blocks 
56                 atomic {
57                         mm[0] = global new MatrixMultiply(matrix,0,0,1,1);
58                         mm[1] = global new MatrixMultiply(matrix,0,2,1,3);
59                         mm[2] = global new MatrixMultiply(matrix,2,0,3,1);
60                         mm[3] = global new MatrixMultiply(matrix,2,2,3,3);
61                 }
62                 atomic {
63                         p = matrix.L;
64                         q = matrix.M;
65                         r = matrix.N;
66                 }
67
68                 // print out the matrices to be multiplied
69                 System.printString("MatrixMultiply: L=");
70                 System.printInt(p);
71                 System.printString("\t");
72                 System.printString("M=");
73                 System.printInt(q);
74                 System.printString("\t");
75                 System.printString("N=");
76                 System.printInt(r);
77                 System.printString("\n");
78
79                 //Print Matrices to be multiplied
80                 System.printString("\n");
81                 System.printString("a =");
82                 for (i = 0; i < p; i++) {
83                         for (j = 0; j < q; j++) {
84                                 atomic {
85                                         val = matrix.a[i][j];
86                                 }
87                                 System.printString(" " + val);
88                                 System.printString("\t");
89                         }
90                 }
91                 System.printString("\n");
92
93                 System.printString("b =");
94                 for (i = 0; i < q; i++) {
95                         for (j = 0; j < r; j++) {
96                                 atomic {
97                                         val = matrix.b[i][j];
98                                 }
99                                 System.printString(" " + val);
100                                 System.printString("\t");
101                         }
102                 }
103                 System.printString("\n");
104
105                 // start a thread to compute each c[l,n]
106                 for (i = 0; i < NUM_THREADS; i++) {
107                         atomic {
108                                 tmp = mm[i];
109                         }
110                         tmp.start(mid);
111                 }
112
113                 // wait for them to finish
114                 for (i = 0; i < NUM_THREADS; i++) {
115                         atomic {
116                                 tmp = mm[i];
117                         }
118                         tmp.join();
119                 }
120
121                 // print out the result of the matrix multiply
122                 System.printString("Starting\n");
123                 System.printString("Matrix Product c =");
124                 for (i = 0; i < p; i++) {
125                         for (j = 0; j < r; j++) {
126                                 atomic {
127                                         val = matrix.c[i][j];
128                                 }
129                                 System.printInt(val);
130                                 System.printString("\t");
131                         }
132                 }
133                 System.printString("Finished\n");
134         }
135 }
136
137 public class MMul{
138
139         public int L, M, N;
140         public int[][] a;
141         public int[][] b;
142         public int[][] c;
143
144         public MMul(int L, int M, int N) {
145                 this.L = L;
146                 this.M = M;
147                 this.N = N;
148                 a = global new int[L][M];  
149                 b = global new int[M][N]; 
150                 c = global new int[L][N]; 
151         }
152
153         public void setValues() {
154                 int i;
155                 int j;
156                 for(i = 0; i < L; i++) {
157                         for(j = 0; j < M; j++) {
158                                 a[i][j] = j+1;
159                         }
160                 }
161
162                 for(i = 0; i < M; i++) {
163                         for(j = 0; j < N; j++) {
164                                 b[i][j] = j+1;
165                         }
166                 }
167
168                 for(i = 0; i < L; i++) {
169                         for(j = 0; j < N; j++) {
170                                 c[i][j] = 0;
171                         }
172                 }
173         }
174
175         public int multiply(int x, int y) {
176                 int i;
177                 int prod = 0;
178                 for(i = 0; i < M; i++) {
179                         prod+= a[x][i] * b[i][y];
180                 }
181                 return prod;
182         }
183
184 }