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