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