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