working version
[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                 }
53
54                 for(i = 0; i < NUM_THREADS; i++) {
55                         atomic{
56                                 mm[i].mmul = global new MMul(0, 0, 0);
57                         }
58                 }
59
60                 // Currently it is a 4 X 4 matrix divided into 4 blocks 
61                 atomic {
62                         mm[0] = global new MatrixMultiply(matrix,0,0,1,1);
63                         mm[1] = global new MatrixMultiply(matrix,0,2,1,3);
64                         mm[2] = global new MatrixMultiply(matrix,2,0,3,1);
65                         mm[3] = global new MatrixMultiply(matrix,2,2,3,3);
66                 }
67                 atomic {
68                         p = matrix.L;
69                         q = matrix.M;
70                         r = matrix.N;
71                 }
72
73                 // print out the matrices to be multiplied
74                 System.printString("MatrixMultiply: L=");
75                 System.printInt(p);
76                 System.printString("\t");
77                 System.printString("M=");
78                 System.printInt(q);
79                 System.printString("\t");
80                 System.printString("N=");
81                 System.printInt(r);
82                 System.printString("\n");
83
84                 //Print Matrices to be multiplied
85                 System.printString("\n");
86                 System.printString("a =");
87                 for (i = 0; i < p; i++) {
88                         for (j = 0; j < q; j++) {
89                                 atomic {
90                                         val = matrix.a[i][j];
91                                 }
92                                 System.printString(" " + val);
93                                 System.printString("\t");
94                         }
95                 }
96                 System.printString("\n");
97
98                 System.printString("b =");
99                 for (i = 0; i < q; i++) {
100                         for (j = 0; j < r; j++) {
101                                 atomic {
102                                         val = matrix.b[i][j];
103                                 }
104                                 System.printString(" " + val);
105                                 System.printString("\t");
106                         }
107                 }
108                 System.printString("\n");
109
110                 // start a thread to compute each c[l,n]
111                 for (i = 0; i < NUM_THREADS; i++) {
112                         atomic {
113                                 tmp = mm[i];
114                         }
115                         tmp.start(mid);
116                 }
117
118                 // wait for them to finish
119                 for (i = 0; i < NUM_THREADS; i++) {
120                         atomic {
121                                 tmp = mm[i];
122                         }
123                         tmp.join();
124                 }
125
126                 // print out the result of the matrix multiply
127                 System.printString("Starting\n");
128                 System.printString("Matrix Product c =");
129                 for (i = 0; i < p; i++) {
130                         for (j = 0; j < r; j++) {
131                                 atomic {
132                                         val = matrix.c[i][j];
133                                 }
134                                 System.printInt(val);
135                                 System.printString("\t");
136                         }
137                 }
138                 System.printString("Finished\n");
139         }
140 }
141
142 public class MMul{
143
144         public int L, M, N;
145         public int[][] a;
146         public int[][] b;
147         public int[][] c;
148
149         public MMul(int L, int M, int N) {
150                 this.L = L;
151                 this.M = M;
152                 this.N = N;
153                 a = global new int[L][M];  
154                 b = global new int[M][N]; 
155                 c = global new int[L][N]; 
156         }
157
158         public void setValues() {
159                 int i;
160                 int j;
161                 for(i = 0; i < L; i++) {
162                         for(j = 0; j < M; j++) {
163                                 a[i][j] = j+1;
164                         }
165                 }
166
167                 for(i = 0; i < M; i++) {
168                         for(j = 0; j < N; j++) {
169                                 b[i][j] = j+1;
170                         }
171                 }
172
173                 for(i = 0; i < L; i++) {
174                         for(j = 0; j < N; j++) {
175                                 c[i][j] = 0;
176                         }
177                 }
178         }
179
180         public int multiply(int x, int y) {
181                 int i;
182                 int prod = 0;
183                 for(i = 0; i < M; i++) {
184                         prod+= a[x][i] * b[i][y];
185                 }
186                 return prod;
187         }
188
189 }