59c837a43baa8cc93f9818d48f9dc90d275fad27
[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[4];
45         mid[0] = (128<<24)|(195<<16)|(175<<8)|84; //dw-10
46         mid[1] = (128<<24)|(195<<16)|(175<<8)|85; //dw-11
47         mid[2] = (128<<24)|(195<<16)|(175<<8)|86; //dw-12
48         mid[3] = (128<<24)|(195<<16)|(175<<8)|87; //dw-13
49         int p, q, r;
50         MatrixMultiply[] mm;
51         MatrixMultiply tmp;
52         MMul matrix;
53         
54         atomic {
55             matrix = global new MMul(SIZE, SIZE, SIZE);
56             matrix.setValues();
57             matrix.transpose();
58             mm = global new MatrixMultiply[NUM_THREADS];
59             int increment=SIZE/NUM_THREADS;
60             int base=0;
61             for(int i=0;i<NUM_THREADS;i++) {
62                 if ((i+1)==NUM_THREADS)
63                     mm[i]=global new MatrixMultiply(matrix,base, SIZE, 0, SIZE);
64                 else
65                     mm[i]=global new MatrixMultiply(matrix,base, base+increment, 0, SIZE);
66                 base+=increment;
67             }
68             p = matrix.L;
69             q = matrix.M;
70             r = matrix.N;
71         }
72         
73         // print out the matrices to be multiplied
74         System.printString("\n");
75         System.printString("MatrixMultiply: L=");
76         System.printInt(p);
77         System.printString("\t");
78         System.printString("M=");
79         System.printInt(q);
80         System.printString("\t");
81         System.printString("N=");
82         System.printInt(r);
83         System.printString("\n");
84         
85         // start a thread to compute each c[l,n]
86         for (int i = 0; i < NUM_THREADS; i++) {
87             atomic {
88                 tmp = mm[i];
89             }
90             tmp.start(mid[i]);
91         }
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 }