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