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