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 args[]) { 16 if (args.length != 2) { 17 logger.error("missing argument : <MatrixSize>"); 18 System.exit(0); 19 } 20 21 ProActiveConfiguration.load(); 22 23 ProActiveDescriptor proActiveDescriptor=null; 27 String [] nodesList = null; 28 try{ 29 proActiveDescriptor=ProActive.getProactiveDescriptor("file:"+args[1]); 30 proActiveDescriptor.activateMappings(); 31 VirtualNode vn1 = proActiveDescriptor.getVirtualNode("matrixNode"); 32 nodesList = vn1.getNodesURL(); 34 }catch(Exception 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 [] {nodesList}); } catch (Exception 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 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 e) { e.printStackTrace();} 64 65 endTime = System.currentTimeMillis() - startTime; 66 logger.info(" Asynchronous Distribution : " + endTime + " millisecondes\n\n"); 67 68 69 int i=1; 71 while (true) { 72 m1 = new Matrix(matrixSize,matrixSize); 73 m1.initializeWithRandomValues(); 75 printMessageAndWait("Ready for distributed multiplication (" +i+ ")"); 76 78 81 l.start(m1,m2group,i); 82 83 logger.info(" Operation ("+i+") launched\n"); 84 85 i++; 88 } 89 } 90 91 92 private static void printMessageAndWait(String msg) { 93 java.io.BufferedReader d = new java.io.BufferedReader (new java.io.InputStreamReader (System.in)); 94 logger.info(msg); 95 logger.info(" --> Press <return> to continue"); 96 try { 97 d.readLine(); 98 } catch (Exception e) { 99 } 100 } 102 103 104 107 private static String [] readNodesList(String filename) { 108 try { 109 java.io.File f = new java.io.File (filename); 110 if (! f.canRead()) return null; 111 byte[] b = getBytesFromInputStream(new java.io.FileInputStream (f)); 112 java.util.StringTokenizer tokenizer = new java.util.StringTokenizer (new String (b)); 113 int n = tokenizer.countTokens(); 114 if (n == 0) return null; 115 String [] result = new String [n]; 116 int i=0; 117 while (tokenizer.hasMoreTokens()) { 118 result[i++] = tokenizer.nextToken(); 119 } 120 return result; 121 } catch (java.io.IOException e) { 122 return null; 123 } 124 } 125 126 127 134 private static byte[] getBytesFromInputStream(java.io.InputStream in) throws java.io.IOException { 135 java.io.DataInputStream din = new java.io.DataInputStream (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 |