KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > proactive > examples > matrix > Matrix


1 package org.objectweb.proactive.examples.matrix;
2
3
4 import org.apache.log4j.Logger;
5 import org.objectweb.proactive.ActiveObjectCreationException;
6 import org.objectweb.proactive.ProActive;
7 import org.objectweb.proactive.core.body.future.FutureProxy;
8 import org.objectweb.proactive.core.group.ProActiveGroup;
9 import org.objectweb.proactive.core.node.Node;
10 import org.objectweb.proactive.core.node.NodeException;
11
12
13 public class Matrix implements java.io.Serializable JavaDoc {
14
15         static Logger logger = Logger.getLogger(Matrix.class.getName());
16     private int width;
17     private int height;
18     private double[][] tab;
19         boolean migration = true;
20
21
22     // -----------------------------//
23
// CONSTRUCTORS //
24
//------------------------------//
25

26     public Matrix () {}
27
28     public Matrix (int w, int h) {
29     width = w;
30     height = h;
31     tab = new double[w][h];
32     }
33
34
35     public Matrix (double[][] table) {
36     width = table.length;
37     height = table[0].length;
38     tab = new double[width][];
39     for (int i=0; i < width ; i++)
40         tab[i] = table[i];
41     }
42
43     //Udab: ERREUR ICI, LE WIDTH NE DEVRAIT PAS ETRE MODIFIE
44
// EN FAIT IL Y A CONFUSION ENTRE LE WIDTH ATTRIBUT DE L'OBJET ET LE WIDTH VARIABLE LOCALE
45
//C'EST UN PEU LE BORDEL EN FAIT ICI
46
public Matrix(Matrix mr, int w) {
47
48     int index = 0;
49     int width = 0;
50     Matrix result = null;
51
52     int size = ProActiveGroup.size(mr) ;
53 /* for (int i = 0 ; i < size ; i++) {
54         System.out.println("MATRIX : INIT :: " + ((FutureProxy)ProActiveGroup.get(mr,i)));
55         ((FutureProxy)ProActiveGroup.get(mr,i)).waitFor();
56     }
57         System.out.println("nombre de matrices dans le groupe : " + size);
58 */

59
60
61     this.width = w;
62     this.height = w; // ((Matrix)((FutureProxy)ProActiveGroup.get(mr,0)).getResult()).getHeight();
63

64 /*
65     startTime = System.currentTimeMillis();
66     FutureProxy fp = (FutureProxy)ProActiveGroup.get(mr,0);
67     endTime = System.currentTimeMillis() - startTime;
68     System.out.println(" Initialisation de reconstruction 1 : " + endTime + " millisecondes\n");
69
70     startTime = System.currentTimeMillis();
71     Matrix m = (Matrix)fp.getResult();
72     endTime = System.currentTimeMillis() - startTime;
73     System.out.println(" Initialisation de reconstruction 2 : " + endTime + " millisecondes\n");
74
75     startTime = System.currentTimeMillis();
76     this.height = m.getHeight();
77     endTime = System.currentTimeMillis() - startTime;
78     System.out.println(" Initialisation de reconstruction 3 : " + endTime + " millisecondes\n");
79 */

80     
81     tab = new double[this.width][];
82
83     for (int i=0 ; i < size ; i++) {
84         result = ((Matrix)ProActiveGroup.get(mr,i));
85
86 // result = ((Matrix)
87
// ((FutureProxy)ProActiveGroup.get(mr,i))
88
// .getResult());
89

90         int widthTmp = result.getWidth();
91         for (int j=0 ; j < widthTmp ; j++) {
92         // Recopie de la colonne "index"
93
tab[index] = result.getColumn(j);
94         index++;
95         }
96     }
97     }
98
99     public Matrix (Matrix mr) {
100     int index = 0;
101     int width = 0;
102     Matrix result = null;
103
104     int size = ProActiveGroup.size(mr) ;
105     
106     for (int i=0 ; i < size ; i++) {
107         width += ((Matrix)((FutureProxy)ProActiveGroup.get(mr,i)).getResult()).getWidth();
108     }
109
110     height = ((Matrix)((FutureProxy)ProActiveGroup.get(mr,0)).getResult()).getHeight();
111     tab = new double[width][];
112     for (int i=0 ; i < size ; i++) {
113         result = ((Matrix)((FutureProxy)ProActiveGroup.get(mr,i)).getResult());
114         width = result.getWidth();
115         for (int j=0 ; j < width ; j++) {
116         tab[index] = result.getColumn(j);
117         index++;
118         }
119     }
120     }
121
122
123 // public Matrix (Matrix mr) {
124

125 // int w = 0;
126

127
128
129
130
131 // // System.out.println(mr.getClass());
132
// // org.objectweb.proactive.core.mop.Proxy theProxy = ((StubObject)mr).getProxy();
133
// // System.out.println(theProxy.getClass());
134
// // System.out.println(((FutureProxy)theProxy).getResult().getClass());
135
// // if (!(theProxy instanceof org.objectweb.proactive.core.group.ProxyForGroup))
136
// // System.out.println("m2group est bien un group !!!!!!!");
137

138
139
140
141
142
143
144 // for (int i=0 ; i < ProActiveGroup.size(mr) ; i++) {
145
// // System.out.println(" taille width : " + ((Matrix)((FutureProxy)ProActiveGroup.get(mr,i)).getResult()).getWidth() +
146
// // " taille height : " + ((Matrix)((FutureProxy)ProActiveGroup.get(mr,i)).getResult()).getHeight() );
147
// w += ((Matrix)((FutureProxy)ProActiveGroup.get(mr,i)).getResult()).getWidth();
148
// }
149

150 // width = w;
151
// height = ((Matrix)((FutureProxy)ProActiveGroup.get(mr,0)).getResult()).getHeight();
152

153 // // System.out.println("Largeur de la matrix resultat : "+ width);
154

155 // tab = new double[width][];
156

157 // int index = 0;
158
// for (int i=0 ; i < ProActiveGroup.size(mr) ; i++) {
159
// // System.out.println("Matrix resultat : " + i);
160
// for (int j=0 ; j < ((Matrix)((FutureProxy)ProActiveGroup.get(mr,i)).getResult()).getWidth() ; j++) {
161
// // System.out.println("Je m'occupe de la colonne : " + index);
162
// tab[index] = ((Matrix)((FutureProxy)ProActiveGroup.get(mr,i)).getResult()).getColumn(j);
163
// index++;
164
// }
165
// }
166
// }
167

168
169     public Matrix (Matrix[] mr) {
170     int w = 0;
171     for (int i=0 ; i < mr.length ; i++)
172         w += mr[i].getWidth();
173     
174     width = w;
175     height = mr[0].getHeight();
176
177     tab = new double[width][];
178     
179     int index = 0;
180     for (int i=0 ; i < mr.length ; i++) {
181         for (int j=0 ; j < mr[i].getWidth() ; j++) {
182         tab[index] = mr[i].getColumn(j);
183         index++;
184         }
185     }
186     }
187
188         
189     
190
191     
192     //----------------------------//
193
// ACCESSORS //
194
//----------------------------//
195

196     public int getWidth() {
197     return width;
198     }
199
200     public void setWidth(int w) {
201     width = w;
202     }
203
204     public int getHeight() {
205     return height;
206     }
207
208     public void setHeight(int h) {
209     height = h;
210     }
211
212     public double getWH(int w, int h) {
213     return tab[w][h];
214     }
215
216     public void setWH(int w, int h, double val) {
217     tab[w][h] = val;
218     }
219
220     public double[][] getTab() {
221     return tab;
222     }
223
224     public double[] getColumn(int w) {
225     return tab[w];
226     }
227
228
229     //-----------------------------------//
230
// METHODS //
231
//-----------------------------------//
232

233     static int k=0;
234
235     public void initializeWithRandomValues() {
236
237     //int k= 0;
238
for (int i = 0 ; i < tab.length ; i++)
239         for (int j = 0 ; j < tab[i].length ; j++)
240             setWH(i,j,k++);
241
242 /* for (int i = 0 ; i < tab.length ; i++)
243         for (int j = 0 ; j < tab[i].length ; j++)
244         tab[i][j] = Math.round(Math.random()*10);
245 */
}
246
247     //Udab: Modifie pour tests
248
public String JavaDoc toString() {
249     //System.out.println("Methode Matrix::toString");
250
String JavaDoc s = new String JavaDoc("");
251     int height = this.getHeight();
252     int width = this.getWidth();
253     for (int i = 0 ; i < height ; i++) {
254         for (int j = 0 ; j < width ; j++) {
255         // System.out.println(" i = " + i + "; j = " + j + ";");
256
s += Double.toString(getWH(j,i));
257         s += " ";
258         }
259         s += "\n";
260     }
261     return s;
262     }
263     
264
265
266     public Matrix getVerticalSubMatrix (int widthStart, int widthStop) {
267     double[][] d = new double[widthStop-widthStart][];
268     for (int i=0 ; i < widthStop-widthStart ; i++)
269         d[i] = tab[widthStart+i];
270     return new Matrix(d);
271     }
272     
273
274     public Matrix getActiveVerticalSubMatrix (int widthStart, int widthStop, Node node) {
275     Matrix vsm = null;
276
277     double[][] d = new double[widthStop-widthStart][];
278     for (int i=0 ; i < widthStop-widthStart ; i++)
279         d[i] = tab[widthStart+i];
280     
281     Object JavaDoc [] params = new Object JavaDoc[1];
282     params[0] = d;
283
284     try {
285         vsm = (Matrix) ProActive.newActive("org.objectweb.proactive.examples.matrix.Matrix", params, node); }
286     catch (ActiveObjectCreationException e) {
287         logger.error("Error create Active Vertical Sub Matrix : ActiveObjectCreationException\n"); }
288     catch (NodeException e) {
289         logger.error("Error create Active Vertical Sub Matrix : NodeException\n"); }
290     return vsm;
291     }
292
293
294     public Matrix transformIntoActiveVerticalSubMatrixGroup(Node[] nodeList) {
295     // if (getWidth() >= nodeList.length)
296
Matrix result = null;
297     int widthSubMatrix;
298     int more;
299     boolean pile;
300     if ((getWidth() % nodeList.length) == 0) {
301         widthSubMatrix = getWidth() / nodeList.length;
302         more = 0;
303         pile = true;
304     }
305     else {
306         widthSubMatrix = (getWidth() / nodeList.length)+1;
307         more = getWidth() % widthSubMatrix;
308         pile = false;
309     }
310
311     Object JavaDoc[][] params = new Object JavaDoc[nodeList.length][];
312     
313     for (int i=0 ; i < nodeList.length ; i++) {
314         Object JavaDoc[] po = new Object JavaDoc[1];
315         double[][] d;
316         if ((!pile) && (i == nodeList.length-1))
317         d = new double[more][];
318         else
319         d = new double[widthSubMatrix][];
320         
321         for (int j=0 ; j < d.length ; j++)
322         d[j] = tab[(i*widthSubMatrix)+j];
323         
324         po[0] = d;
325         
326         params[i] = po;
327
328
329
330
331
332 /* Debugging
333
334         System.out.println("SubMatrix " + i + " d.length = " + d.length);
335         String s = new String("");
336         for (int h=0 ; h < d[0].length ; h++) {
337         for (int l=0 ; l < d.length ; l++) {
338             s += d[l][h];
339             s += " ";
340         }
341         s += "\n";
342         }
343         System.out.println(s);
344         
345 */

346
347
348
349
350
351
352
353     }
354
355     try {
356         result = (Matrix) ProActiveGroup.newGroup("org.objectweb.proactive.examples.matrix.Matrix",params,nodeList);
357     }
358     catch (Exception JavaDoc e) { e.printStackTrace();}
359     
360     return result;
361     }
362     
363
364     public Matrix localMultiplyForGroup (Matrix m) {
365     //long startTime;
366
//long endTime;
367
//startTime= System.currentTimeMillis();
368

369
370     Matrix res = new Matrix(getWidth(),m.getHeight());
371     int height=res.getHeight() ;
372         int width = res.getWidth();
373     for (int line=0 ; line < height ; line++) {
374         for (int column=0 ; column < width ; column++) {
375         double val = 0;
376         for (int index=0 ; index < height ; index++)
377             {
378
379
380             //System.out.println("(" + m.getWH(index,line) + " * " + getWH(column,index) + ") + ");
381

382
383             val += m.getWH(index,line) * getWH(column,index);
384             }
385         //System.out.println(" == " + val + "\n");
386
res.setWH(column,line,val);
387         }
388     }
389
390
391     //endTime = System.currentTimeMillis() - startTime;
392
//System.out.println(" Local multiply for group : " + endTime + " millisecondes\n\n");
393

394     
395     return res;
396     }
397
398
399     
400     public Matrix distributedMultiply (Matrix m, Node[] nodeList) {
401     if (getWidth() != m.getHeight()) {
402         logger.error("Error : no compatible Matrix");
403         return null;
404     }
405         else {
406         Matrix verticalSubMatrixGroup = null;
407
408         verticalSubMatrixGroup = m.transformIntoActiveVerticalSubMatrixGroup(nodeList);
409
410         Matrix mr = verticalSubMatrixGroup.localMultiplyForGroup(this);
411         return new Matrix(mr);
412     }
413     }
414
415
416     public Matrix distributedMultiply (Matrix m) {
417     Node[] nodeList = new Node[1];
418     nodeList[0] = null;
419     return distributedMultiply(m,nodeList);
420     }
421
422
423
424     public Matrix localMultiply (Matrix m) {
425     return m.localMultiplyForGroup(this);
426     }
427
428
429
430
431
432     public Matrix[] transformIntoActiveMatrixTable(Node[] nodeList) {
433     // if (getWidth() >= nodeList.length)
434
Matrix[] result = new Matrix[nodeList.length];
435     int widthSubMatrix;
436     int more;
437     boolean pile;
438     if ((getWidth() % nodeList.length) == 0) {
439         widthSubMatrix = getWidth() / nodeList.length;
440         more = 0;
441         pile = true;
442     }
443     else {
444         widthSubMatrix = (getWidth() / nodeList.length)+1;
445         more = getWidth() % widthSubMatrix;
446         pile = false;
447     }
448
449     for (int i=0 ; i < nodeList.length ; i++) {
450         Object JavaDoc[] po = new Object JavaDoc[1];
451         double[][] d;
452         if ((!pile) && (i == nodeList.length-1))
453         d = new double[more][];
454         else
455         d = new double[widthSubMatrix][];
456         
457         for (int j=0 ; j < d.length ; j++)
458         d[j] = tab[(i*widthSubMatrix)+j];
459         
460         po[0] = d;
461
462         try {
463         result[i] = (Matrix) ProActive.newActive("org.objectweb.proactive.examples.matrix.Matrix",po,nodeList[i]);
464         }
465         catch (Exception JavaDoc e) { e.printStackTrace();}
466     }
467     
468     return result;
469     }
470
471
472     public Matrix distributedMultiplyWithOutGroup (Matrix m, Node[] nodeList) {
473     if (getWidth() != m.getHeight()) {
474         logger.error("Error : no compatible Matrix");
475         return null;
476     }
477         else {
478         Matrix[] verticalSubMatrixTable;
479
480         verticalSubMatrixTable = m.transformIntoActiveMatrixTable(nodeList);
481
482         Matrix[] mr = new Matrix[verticalSubMatrixTable.length];
483
484         for (int i=0 ; i < verticalSubMatrixTable.length ; i++)
485         mr[i] = verticalSubMatrixTable[i].localMultiplyForGroup(this);
486
487         return new Matrix(mr);
488     }
489     }
490
491
492
493     // -- PRIVATE METHODS FOR SERIALIZATION -----------------------------------------------
494

495     private void writeObject(java.io.ObjectOutputStream JavaDoc out) throws java.io.IOException JavaDoc {
496     //long startTime;
497
//long endTime;
498

499     //startTime = System.currentTimeMillis();
500

501     out.defaultWriteObject();
502
503     //endTime = System.currentTimeMillis() - startTime;
504
// System.out.println(" Serialization : " + endTime + " millisecondes\n");
505
}
506
507
508 // private void readObject(java.io.ObjectInputStream in) throws java.io.IOException, ClassNotFoundException {
509
// in.defaultReadObject();
510
// }
511

512
513
514
515
516     
517 }
518
Popular Tags