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