bug fixes and changes to the matrixmultiply benchmark
[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                 
20                     //Use b transpose for cache performance
21                     for(int i = x0; i<= x1; i++){
22                         double a[]=mmul.a[i];
23                         int M=mmul.M;
24                         for (int j = y0; j <= y1; j++) {
25                             double innerProduct=0;
26                             double 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                         double 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)|69;
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 p, q, r;
52                 MatrixMultiply[] mm;
53                 MatrixMultiply tmp;
54                 MMul matrix;
55
56                 atomic {
57                         matrix = global new MMul(400, 400, 400);
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,399,200);
68                         mm[1] = global new MatrixMultiply(matrix,0,201,399,399);
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                 // start a thread to compute each c[l,n]
90                 for (int i = 0; i < NUM_THREADS; i++) {
91                         atomic {
92                                 tmp = mm[i];
93                         }
94                         tmp.start(mid1);
95                 }
96
97                 // wait for them to finish
98                 for (int i = 0; i < NUM_THREADS; i++) {
99                         atomic {
100                                 tmp = mm[i];
101                         }
102                         tmp.join();
103                 }
104
105                 // print out the result of the matrix multiply
106                 System.printString("Starting\n");
107                 System.printString("Matrix Product c =\n");
108                 double val;
109                 atomic {
110                         for (int i = 0; i < p; i++) {
111                                 double c[]=matrix.c[i];
112                                 for (int j = 0; j < r; j++) {
113                                         val = c[j];
114                                 }
115                         }
116                 }
117                 System.printString("Finished\n");
118         }
119 }
120
121 public class MMul{
122
123         public int L, M, N;
124         public double[][] a;
125         public double[][] b;
126         public double[][] c;
127         public double[][] btranspose;
128
129         public MMul(int L, int M, int N) {
130                 this.L = L;
131                 this.M = M;
132                 this.N = N;
133                 a = global new double[L][M];  
134                 b = global new double[M][N]; 
135                 c = global new double[L][N]; 
136                 btranspose = global new double[N][M];
137         }
138
139         public void setValues() {
140                 for(int i = 0; i < L; i++) {
141             double ai[] = a[i];
142                         for(int j = 0; j < M; j++) {
143                                 ai[j] = j+1;
144                         }
145                 }
146
147                 for(int i = 0; i < M; i++) {
148             double bi[] = b[i];
149                         for(int j = 0; j < N; j++) {
150                                 bi[j] = j+1;
151                         }
152                 }
153
154                 for(int i = 0; i < L; i++) {
155             double ci[] = c[i];
156                         for(int j = 0; j < N; j++) {
157                                 ci[j] = 0;
158                         }
159                 }
160                 for(int i = 0; i < N; i++) {
161             double btransposei[] = btranspose[i];
162                         for(int j = 0; j < M; j++) {
163                                 btransposei[j] = 0;
164                         }
165                 }
166         }
167
168         public void transpose() {
169                 for(int row = 0; row < M; row++) {
170             double brow[] = b[row];
171                         for(int col = 0; col < N; col++) {
172                                 btranspose[col][row] = brow[col];
173                         }
174                 }
175         }
176 }