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