new makefile and rewritten benchmark
[IRC.git] / Robust / src / Benchmarks / Prefetch / MatrixMultiply / MatrixMultiplyN.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 x1, int y0, 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         atomic {
15             //Use btranspose for cache performance
16             for(int i = x0; i<= x1; i++){
17                 double a[]=mmul.a[i];
18                 double c[]=mmul.c[i];
19                 int M=mmul.M;
20                 for (int j = y0; j <= y1; j++) {
21                     double innerProduct=0;
22                     double b[] = mmul.btranspose[j];
23                     for(int k = 0; k < M; k++) {
24                         innerProduct += a[k] *b[k];
25                     }
26                     c[j]=innerProduct;
27                 }
28             }
29         }
30     }
31     
32     public static void main(String[] args) {
33         int NUM_THREADS = 4;
34         int SIZE=600;
35         if (args.length>0) {
36             NUM_THREADS=Integer.parseInt(args[0]);
37             if (args.length>1)
38                 SIZE=Integer.parseInt(args[1]);
39         }
40         
41         int[] mid = new int[NUM_THREADS];
42         mid[0] = (128<<24)|(195<<16)|(175<<8)|80;
43         mid[1] = (128<<24)|(195<<16)|(175<<8)|73;
44         mid[2] = (128<<24)|(195<<16)|(175<<8)|78;
45         mid[3] = (128<<24)|(195<<16)|(175<<8)|79;
46         int p, q, r;
47         MatrixMultiply[] mm;
48         MatrixMultiply tmp;
49         MMul matrix;
50         
51         atomic {
52             matrix = global new MMul(SIZE, SIZE, SIZE);
53             matrix.setValues();
54             matrix.transpose();
55             mm = global new MatrixMultiply[NUM_THREADS];
56             int increment=SIZE/NUM_THREADS;
57             int base=0;
58             for(int i=0;i<NUM_THREADS;i++) {
59                 if ((i+1)==NUM_THREADS)
60                     mm[i]=global new MatrixMultiply(matrix,base, SIZE-1, 0, SIZE-1);
61                 else
62                     mm[i]=global new MatrixMultiply(matrix,base, base+increment, 0, SIZE-1);
63                 base+=increment;
64             }
65             p = matrix.L;
66             q = matrix.M;
67             r = matrix.N;
68         }
69         
70         // print out the matrices to be multiplied
71         System.printString("\n");
72         System.printString("MatrixMultiply: L=");
73         System.printInt(p);
74         System.printString("\t");
75         System.printString("M=");
76         System.printInt(q);
77         System.printString("\t");
78         System.printString("N=");
79         System.printInt(r);
80         System.printString("\n");
81         
82         // start a thread to compute each c[l,n]
83         for (int i = 0; i < NUM_THREADS; i++) {
84             atomic {
85                 tmp = mm[i];
86             }
87             tmp.start(mid[i]);
88         }
89         
90         // wait for them to finish
91         for (int i = 0; i < NUM_THREADS; i++) {
92             atomic {
93                 tmp = mm[i];
94             }
95             tmp.join();
96         }
97         
98         // print out the result of the matrix multiply
99
100         System.printString("Finished\n");
101     }
102 }
103
104 public class MMul{
105
106         public int L, M, N;
107         public double[][] a;
108         public double[][] b;
109         public double[][] c;
110         public double[][] btranspose;
111
112         public MMul(int L, int M, int N) {
113                 this.L = L;
114                 this.M = M;
115                 this.N = N;
116                 a = global new double[L][M];  
117                 b = global new double[M][N]; 
118                 c = global new double[L][N]; 
119                 btranspose = global new double[N][M];
120         }
121
122         public void setValues() {
123                 for(int i = 0; i < L; i++) {
124             double ai[] = a[i];
125                         for(int j = 0; j < M; j++) {
126                                 ai[j] = j+1;
127                         }
128                 }
129
130                 for(int i = 0; i < M; i++) {
131             double bi[] = b[i];
132                         for(int j = 0; j < N; j++) {
133                                 bi[j] = j+1;
134                         }
135                 }
136
137                 for(int i = 0; i < L; i++) {
138             double ci[] = c[i];
139                         for(int j = 0; j < N; j++) {
140                                 ci[j] = 0;
141                         }
142                 }
143                 for(int i = 0; i < N; i++) {
144             double btransposei[] = btranspose[i];
145                         for(int j = 0; j < M; j++) {
146                                 btransposei[j] = 0;
147                         }
148                 }
149         }
150
151         public void transpose() {
152                 for(int row = 0; row < M; row++) {
153             double brow[] = b[row];
154                         for(int col = 0; col < N; col++) {
155                                 btranspose[col][row] = brow[col];
156                         }
157                 }
158         }
159 }