3 import java.io.FileInputStream;
4 import java.io.FileNotFoundException;
5 import java.io.FileOutputStream;
6 import java.io.OutputStreamWriter;
7 import java.io.PrintWriter;
8 import java.net.InetAddress;
9 import java.util.ArrayList;
11 import com.thoughtworks.xstream.XStream;
12 import com.thoughtworks.xstream.io.xml.DomDriver;
17 * Class providing some tools to the library
18 * @author Séastien Miquée
23 * Creation of the representation of the node in the Mapping point of view. It needs
24 * some information about the computing node, which will be exploited by mapping
26 * @return A node from the Mapping library
28 public static GNode createGNode()
30 /** Using the Linpack class **/
31 Linpack linpack = new Linpack() ;
34 GNode n = new GNode() ;
42 /** CPU information **/
44 /* Amount of available computing cores */
45 nbCore = Runtime.getRuntime().availableProcessors() ;
47 /* Average MegaFlops of each core */
48 double tmp_mflops = 0 ;
51 for( int i = 0 ; i < loop ; i++ )
53 tmp_mflops += linpack.getMFlops() ;
56 mflops = (int) tmp_mflops / loop ;
59 /** Memory information **/
60 memory = (int) Runtime.getRuntime().maxMemory() / 1024 / 1024 ;
65 InetAddress addr = InetAddress.getLocalHost() ;
66 name = new String( addr.getCanonicalHostName() ) ;
67 } catch( final Exception e ) {
68 System.err.println( "Unalbe to retrieve host's name !" ) ;
76 InetAddress addr = InetAddress.getLocalHost() ;
77 ip = addr.getHostAddress() ;
78 } catch( final Exception e ) {
79 System.err.println( "Unalbe to retrieve host's name !" ) ;
84 String names[] = decodeG5Knames( name ) ;
86 /* Updating node information */
87 n.setFrequency( frequency ) ;
88 n.setMFlops( mflops ) ;
89 n.setMemory( memory ) ;
90 n.setNb_cores( nbCore ) ;
92 n.setCluster( names[1] ) ;
93 n.setSite( names[2] ) ;
95 n.setMapped( false ) ;
101 * Creation of the representation of the grid, according to clusters into sites.
102 * This representation may only fit on Grid'5000 like architectures (with computing
103 * nodes name based on the following pattern |cluster_name|id_of_node_into_cluster|.|site_of_cluster|.|organisation|.*|).
104 * @param _an the list of computing nodes connected
105 * @return the grid's architecture
107 public static Grid createGridG5k( ArrayList<GNode> _an )
109 Grid gr = new Grid() ;
113 String cluster = "", site = "" ;
115 ArrayList<Cluster> clusters = new ArrayList<Cluster>() ;
117 for( int i = 0 ; i < _an.size() ; i++ )
119 /* Variables edition */
121 String names[] = decodeG5Knames( _an.get( i ).getName() ) ;
123 cluster = names[ 1 ] ;
126 /* Insertion of the node in its cluster */
127 boolean trouve = false ;
129 for( int j = 0 ; j < clusters.size() ; j++ )
131 if( clusters.get( j ).getName().equals( cluster ) && clusters.get( j ).getSite().equals( site ))
133 _an.get( i ).setInCluster( true ) ;
134 _an.get( i ).setMapped( false ) ;
135 _an.get( i ).setSite( clusters.get( j ).getSite() ) ;
136 _an.get( i ).setCluster( clusters.get( j ).getName() ) ;
137 clusters.get( j ).addGNode( _an.get( i ) ) ;
146 Cluster cl = new Cluster() ;
148 cl.setName( cluster ) ;
151 _an.get( i ).setInCluster( true ) ;
152 _an.get( i ).setMapped( false ) ;
153 _an.get( i ).setSite( site ) ;
154 _an.get( i ).setCluster( cluster ) ;
156 cl.addGNode( _an.get( i ) ) ;
162 gr.addClusters( clusters ) ;
171 * Return the three parts of the name of a node on Grid'5000.
172 * @param _name The full name of the G5K node
173 * @return The three parts of the name
175 protected static String[] decodeG5Knames( String _name )
178 String tab[] = new String[ 5 ] ;
179 String ret[] = new String[ 3 ] ;
181 temp = _name.replace('.', '!' ) ;
183 tab = temp.split( "!" ) ;
188 tab = ret[0].split( "-" ) ;
190 ret[ 1 ] = tab[ 0 ] ;
197 * Write the Grid object in an XML file.
198 * @param _gl Grid graph to be write
199 * @param _path File's path
200 * @param _file File's name
202 public static void writeGrid( Grid _gl, String _path, String _file )
204 if( _file.equals( "" ) )
206 System.err.println( "No file's name !\n" ) ;
210 if ( ! _file.endsWith( ".xml" ) )
212 _file = _file + ".xml"; // Adding xml extension to file
215 if( ! _file.equals( "" ) )
219 if( _path.length() != 0 )
221 path = _path+"/"+_file ;
223 path = new String( "./" + _file ) ;
226 XStream xstream = new XStream( new DomDriver() ) ;
228 String xml = xstream.toXML( _gl ) ;
230 PrintWriter ecrivain = null ;
233 ecrivain = new PrintWriter( new OutputStreamWriter( new FileOutputStream( path ), "UTF8" ) ) ;
235 ecrivain.println( "<?xml version=\"1.0\" encoding=\"UTF8\"?>" ) ;
236 ecrivain.println( xml ) ;
239 } catch( Exception e ) {
240 System.err.println( "\nError during the write opération !\n" ) ;
241 e.printStackTrace() ;
247 * Write an application Graph in a file.
248 * @param _gr Application Graph to be write
249 * @param _path File's path
250 * @param _file File's name
252 public static void writeGraph( Graph _gr, String _path, String _file )
254 if( _file.equals( "" ) )
256 System.err.println( "No file's name !\n" ) ;
260 if ( ! _file.endsWith( ".xml" ) )
262 _file = _file + ".xml"; // Adding xml extension to file
265 if( ! _file.equals( "" ) )
269 if( _path.length() != 0 )
271 path = _path+"/"+_file ;
273 path = new String( "./" + _file ) ;
276 XStream xstream = new XStream( new DomDriver() ) ;
278 String xml = xstream.toXML( _gr ) ;
280 PrintWriter ecrivain = null ;
283 ecrivain = new PrintWriter( new OutputStreamWriter( new FileOutputStream( path ), "UTF8" ) ) ;
285 ecrivain.println( "<?xml version=\"1.0\" encoding=\"UTF8\"?>" ) ;
286 ecrivain.println( xml ) ;
289 } catch( Exception e ) {
290 System.err.println( "\nError during the write opération !\n" ) ;
291 e.printStackTrace() ;
298 * Write a mapping done in a file.
299 * @param _mp The mapping done to be write
300 * @param _path File's path
301 * @param _file File's name
303 public static void writeMapping( Mapping _mp, String _path, String _file )
305 if( _file.equals( "" ) )
307 System.err.println( "No file's name !\n" ) ;
311 if ( ! _file.endsWith( ".xml" ) )
313 _file = _file + ".xml"; // Adding xml extension to file
316 if( ! _file.equals( "" ) )
320 if( _path.length() != 0 )
322 path = _path+"/"+_file ;
324 path = new String( "./" + _file ) ;
327 XStream xstream = new XStream( new DomDriver() ) ;
329 String xml = xstream.toXML( _mp ) ;
331 PrintWriter ecrivain = null ;
334 ecrivain = new PrintWriter( new OutputStreamWriter( new FileOutputStream( path ), "UTF8" ) ) ;
336 ecrivain.println( "<?xml version=\"1.0\" encoding=\"UTF8\"?>" ) ;
337 ecrivain.println( xml ) ;
340 } catch( Exception e ) {
341 System.err.println( "\nError during the write opération !\n" ) ;
342 e.printStackTrace() ;
349 * Read an application Graph from a file.
350 * @param _file File's name
351 * @param _path File's path
352 * @return The application Graph read
354 public static Graph readGraph( String _path, String _file )
356 if ( _file.equals( "" ) )
358 System.err.println( "No file's name !\n" ) ;
362 if ( ! _file.endsWith( ".xml" ) )
364 _file = _file + ".xml"; // Adding xml extension to file
369 if( _path.length() != 0 )
371 path = _path+"/"+_file ;
373 path = new String( "./" + _file ) ;
378 XStream xstream = new XStream( new DomDriver() ) ;
381 gr = (Graph) xstream.fromXML( new FileInputStream( path ) ) ;
382 } catch( FileNotFoundException e ) {
383 System.err.println( "File not found !\n" ) ;
386 } catch( ClassCastException e ) {
387 System.err.println( "The file does not contain a valid Graph" ) ;
388 e.printStackTrace() ;
397 * Read a Grid graph from a file.
398 * @param _file File's name
399 * @param _path File's path
400 * @return The Grid graph read
402 public static Grid readGrid( String _path, String _file )
404 if ( _file.equals( "" ) )
406 System.err.println( "No file's name !\n" ) ;
410 if ( ! _file.endsWith( ".xml" ) )
412 _file = _file + ".xml"; // Adding xml extension to file
417 if( _path.length() != 0 )
419 path = _path+"/"+_file ;
421 path = new String( "./" + _file ) ;
426 XStream xstream = new XStream( new DomDriver() ) ;
429 gr = (Grid) xstream.fromXML( new FileInputStream( path ) ) ;
430 } catch( FileNotFoundException e ) {
431 System.err.println( "File not found !\n" ) ;
434 } catch( ClassCastException e ) {
435 System.err.println( "The file does not contain a valid Grid" ) ;
436 e.printStackTrace() ;
445 * Read a Mapping done from a file.
446 * @param _file File's name
447 * @param _path File's path
448 * @return The Mapping read
450 public static Mapping readMapping( String _path, String _file )
452 if ( _file.equals( "" ) )
454 System.err.println( "No file's name !\n" ) ;
458 if ( ! _file.endsWith( ".xml" ) )
460 _file = _file + ".xml"; // Adding xml extension to file
465 if( _path.length() != 0 )
467 path = _path+"/"+_file ;
469 path = new String( "./" + _file ) ;
474 XStream xstream = new XStream( new DomDriver() ) ;
477 mp = (Mapping) xstream.fromXML( new FileInputStream( path ) ) ;
478 } catch( FileNotFoundException e ) {
479 System.err.println( "File not found !\n" ) ;
482 } catch( ClassCastException e ) {
483 System.err.println( "The file does not contain a valid Grid" ) ;
484 e.printStackTrace() ;
494 /** La programmation est un art, respectons ceux qui la pratiquent !! **/