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