KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.objectweb.proactive.examples.matrix;
2
3 import org.apache.log4j.Logger;
4 import org.objectweb.proactive.ProActive;
5 import org.objectweb.proactive.core.config.ProActiveConfiguration;
6 import org.objectweb.proactive.core.descriptor.data.ProActiveDescriptor;
7 import org.objectweb.proactive.core.descriptor.data.VirtualNode;
8
9
10 public class Main {
11     
12     
13     static Logger logger = Logger.getLogger(Main.class.getName());
14     
15     public static void main (String JavaDoc args[]) {
16     if (args.length != 2) {
17        logger.error("missing argument : <MatrixSize>");
18         System.exit(0);
19     }
20
21     ProActiveConfiguration.load();
22
23 //
24
// String[] nodesList = readNodesList(args[0]);
25
// //String targetNode = nodesList[0].substring(0, nodesList[0].length()-1)+"2";
26
ProActiveDescriptor proActiveDescriptor=null;
27     String JavaDoc[] nodesList = null;
28         try{
29         proActiveDescriptor=ProActive.getProactiveDescriptor("file:"+args[1]);
30         proActiveDescriptor.activateMappings();
31         VirtualNode vn1 = proActiveDescriptor.getVirtualNode("matrixNode");
32         //Thread.sleep(15000);
33
nodesList = vn1.getNodesURL();
34         }catch(Exception JavaDoc e){
35     e.printStackTrace();
36     logger.error("Pb when reading descriptor");
37     }
38         
39             
40     Launcher l = null;
41
42     try {
43         l = (Launcher) ProActive.newActive(Launcher.class.getName(), new Object JavaDoc[] {nodesList});//,targetNode);
44
} catch (Exception JavaDoc e) {logger.error("\nUnable to create the Launcher !!!!!\n"); e.printStackTrace();}
45
46     int matrixSize=Integer.parseInt(args[0]);
47
48     Matrix m1;
49     Matrix m2 = new Matrix(matrixSize,matrixSize);
50     m2.initializeWithRandomValues();
51
52     long startTime;
53     long endTime;
54
55
56     // DISTRIBUTION
57
printMessageAndWait("\n\n\n\nReady for distribution");
58     startTime= System.currentTimeMillis();
59
60     Matrix m2group = null;
61     try {
62         m2group = l.distribute(m2); }
63     catch (Exception JavaDoc e) { e.printStackTrace();}
64
65     endTime = System.currentTimeMillis() - startTime;
66     logger.info(" Asynchronous Distribution : " + endTime + " millisecondes\n\n");
67
68
69     // MULTIPLICATION
70
int i=1;
71     while (true) {
72         m1 = new Matrix(matrixSize,matrixSize);
73         m1.initializeWithRandomValues();//l.createMatrix(matrixSize);
74

75         printMessageAndWait("Ready for distributed multiplication (" +i+ ")");
76         //startTime= System.currentTimeMillis();
77

78         // System.out.println(m1);
79
// System.out.println(m2);
80

81         l.start(m1,m2group,i);
82
83         logger.info(" Operation ("+i+") launched\n");
84
85         //endTime = System.currentTimeMillis() - startTime;
86
//System.out.println("Total Distributed Multiplication : " + endTime + " millisecondes\n\n");
87
i++;
88     }
89     }
90     
91     
92     private static void printMessageAndWait(String JavaDoc msg) {
93     java.io.BufferedReader JavaDoc d = new java.io.BufferedReader JavaDoc(new java.io.InputStreamReader JavaDoc(System.in));
94     logger.info(msg);
95     logger.info(" --> Press <return> to continue");
96     try {
97         d.readLine();
98     } catch (Exception JavaDoc e) {
99     }
100     // System.out.println("---- GO ----");
101
}
102     
103     
104     /**
105      * go
106      */

107     private static String JavaDoc[] readNodesList(String JavaDoc filename) {
108     try {
109         java.io.File JavaDoc f = new java.io.File JavaDoc(filename);
110         if (! f.canRead()) return null;
111         byte[] b = getBytesFromInputStream(new java.io.FileInputStream JavaDoc(f));
112         java.util.StringTokenizer JavaDoc tokenizer = new java.util.StringTokenizer JavaDoc(new String JavaDoc(b));
113         int n = tokenizer.countTokens();
114         if (n == 0) return null;
115         String JavaDoc[] result = new String JavaDoc[n];
116         int i=0;
117         while (tokenizer.hasMoreTokens()) {
118         result[i++] = tokenizer.nextToken();
119         }
120         return result;
121     } catch (java.io.IOException JavaDoc e) {
122         return null;
123     }
124     }
125
126
127     /**
128      * Returns an array of bytes containing the bytecodes for
129      * the class represented by the InputStream
130      * @param in the inputstream of the class file
131      * @return the bytecodes for the class
132      * @exception java.io.IOException if the class cannot be read
133      */

134     private static byte[] getBytesFromInputStream(java.io.InputStream JavaDoc in) throws java.io.IOException JavaDoc {
135     java.io.DataInputStream JavaDoc din = new java.io.DataInputStream JavaDoc(in);
136     byte[] bytecodes = new byte[in.available()];
137     try {
138         din.readFully(bytecodes);
139     } finally {
140         if (din != null) din.close();
141     }
142     return bytecodes;
143     }
144     
145 }
146
Popular Tags