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