Recovery
[IRC.git] / Robust / src / Benchmarks / Recovery / MatrixMultiply / MatrixMultiplyN.java
1 public class MatrixMultiply extends Task {
2         MMul mmul;
3         int SIZE;
4         int increment;
5         
6         public MatrixMultiply(MMul mmul, int num_threads, int size) {
7                 this.mmul = mmul;
8
9                 SIZE = size;
10     increment = 80;
11
12     init();
13         }
14
15         public void init() {
16                 todoList = global new Queue();
17                 doneList = global new Queue();
18
19                 fillTodoList();
20         }
21
22   // fill up the Work Pool
23         public void fillTodoList() {
24     Segment seg;
25     int i;
26
27     for(i = 0; i < SIZE; i +=increment) {
28
29       if(i+increment > SIZE) {
30         seg = global new Segment(i,SIZE);
31       }
32       else {
33         seg = global new Segment(i, i + increment);
34       }
35                         todoList.push(seg);
36     }
37         }
38
39         public void execute() {
40     double la[][];
41     double lc[][];
42     double lb[][];
43     double rowA[];
44     double colB[];
45     Segment seg;
46                 
47     double innerproduct;
48     int i,j;
49     int x0;
50     int x1;
51                 int size;
52
53     // get matrix 
54     atomic {
55                         seg = (Segment)myWork;
56                         x0 = seg.x0;  // x start row
57                         x1 = seg.x1;  // x end row
58       la = mmul.a;          //  first mat
59       lb = mmul.btranspose; // second mat
60 //      lc = mmul.c;          // destination mat
61                         size = SIZE;
62     }
63
64                 lc = new double[size][size];
65                 System.out.println("Seg x0 = " + x0 + " - x1 = " + x1);
66                 
67                 for(i = x0; i < x1 ; i++) {
68                         System.printString("i = " + i + "\n");
69                   atomic {
70         rowA = la[i];   // grab first mat's row
71
72                                 for(j = 0; j < size ; j++) {
73           colB = lb[j]; // grab second mat's col
74
75                                         innerproduct = computeProduct(rowA,colB, size); // computes the value
76
77           lc[i][j] = innerproduct;  // store in dest mat
78                                 } // end of for j
79                         } 
80                 }       // end for i 
81 //              }
82                 System.out.println("Finished comutation");
83
84                 atomic {
85                         for (i = x0; i < x1; i++) {
86                                 for (j = 0; j < size; j++) {
87                                         mmul.c[i][j] = lc[i][j];
88                                 }
89                         }
90                 }
91   }
92
93   public double computeProduct(double[] rowA,double[] colB, int size)
94   {
95     int i;
96     double sum = 0;
97
98     for(i = 0 ;i < size; i++) {
99 //      atomic {
100         sum += rowA[i] * colB[i];
101 //      }
102     }
103
104     return sum;
105   }
106
107         public void done(Object work) {
108                 atomic {
109                         ((Queue)doneList).push(work);
110                 }
111         }
112
113   public static void main(String[] args) {
114                 int NUM_THREADS = 4;
115                 int SIZE = 1600;
116     int i,j;
117                 Work[] works;
118                 MMul matrix;
119                 MatrixMultiply mm;
120     Segment[] currentWorkList;
121
122                 if (args.length > 0) {
123                         NUM_THREADS = Integer.parseInt(args[0]);
124                 }
125
126                 int[] mid = new int[NUM_THREADS];
127                 mid[0] = (128<<24)|(195<<16)|(180<<8)|21; //dc1
128                 mid[1] = (128<<24)|(195<<16)|(180<<8)|24; //dc2
129                 mid[2] = (128<<24)|(195<<16)|(180<<8)|26; //dc3
130
131                 atomic {
132                         matrix = global new MMul(SIZE, SIZE, SIZE);
133                         matrix.setValues();
134                         matrix.transpose();
135                         mm = global new MatrixMultiply(matrix, NUM_THREADS, SIZE);
136
137                         works = global new Work[NUM_THREADS];
138       currentWorkList = global new Segment[NUM_THREADS];
139
140                         for(i = 0; i < NUM_THREADS; i++) {
141                                 works[i] = global new Work(mm, NUM_THREADS, i,currentWorkList);
142                         }
143                 }
144     System.out.println("Finished to createObjects");
145
146                 Work tmp;
147                 for (i = 0; i < NUM_THREADS; i++) {
148                         atomic {
149                                 tmp = works[i];
150                         }
151                         Thread.myStart(tmp,mid[i]);
152                 }
153
154                 for (i = 0; i < NUM_THREADS; i++) {
155                         atomic {
156                                 tmp = works[i];
157                         }
158                         tmp.join();
159                 }
160     
161     System.printString("Finished\n");
162         }
163 }
164
165 public class MMul{
166         public int L, M, N;
167         public double[][] a;
168         public double[][] b;
169         public double[][] c;
170         public double[][] btranspose;
171
172         public MMul(int L, int M, int N) {
173                 this.L = L;
174                 this.M = M;
175                 this.N = N;
176                 a = global new double[L][M];  
177                 b = global new double[M][N]; 
178                 c = global new double[L][N]; 
179                 btranspose = global new double[N][M];
180         }
181
182         public void setValues() {
183                 for(int i = 0; i < L; i++) {
184                         double ai[] = a[i];
185                         for(int j = 0; j < M; j++) {
186                                 ai[j] = j+1;
187                         }
188                 }
189
190                 for(int i = 0; i < M; i++) {
191                         double bi[] = b[i];
192                         for(int j = 0; j < N; j++) {
193                                 bi[j] = j+1;
194                         }
195                 }
196
197                 for(int i = 0; i < L; i++) {
198                         double ci[] = c[i];
199                         for(int j = 0; j < N; j++) {
200                                 ci[j] = 0;
201                         }
202                 }
203                 for(int i = 0; i < N; i++) {
204                         double btransposei[] = btranspose[i];
205                         for(int j = 0; j < M; j++) {
206                                 btransposei[j] = 0;
207                         }
208                 }
209         }
210
211         public void transpose() {
212                 for(int row = 0; row < M; row++) {
213                         double brow[] = b[row];
214                         for(int col = 0; col < N; col++) {
215                                 btranspose[col][row] = brow[col];
216                         }
217                 }
218         }
219 }
220
221 public class Segment {
222         int x0;
223         int x1;
224
225         Segment (int x0, int x1) {
226                 this.x0 = x0;
227                 this.x1 = x1;
228         }
229 }
230