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