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