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