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