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 !" ) ;
74 /* Updating node information */
75 n.setFrequency( frequency ) ;
76 n.setMFlops( mflops ) ;
77 n.setMemory( memory ) ;
78 n.setNb_cores( nbCore ) ;
80 n.setMapped( false ) ;
86 * Creation of the representation of the grid, according to clusters into sites.
87 * This representation may only fit on Grid'5000 like architectures (with computing
88 * nodes name based on the following pattern |cluster_name|id_of_node_into_cluster|.|site_of_cluster|.|organisation|.*|).
89 * @param _an the list of computing nodes connected
90 * @return the grid's architecture
92 public static Grid createGridG5k( ArrayList<GNode> _an )
94 Grid gr = new Grid() ;
98 String cluster = "", site = "" ;
100 ArrayList<Cluster> clusters = new ArrayList<Cluster>() ;
102 for( int i = 0 ; i < _an.size() ; i++ )
104 /* Variables edition */
106 String names[] = decodeG5Knames( _an.get( i ).getName() ) ;
108 cluster = names[ 1 ] ;
111 /* Insertion of the node in its cluster */
112 boolean trouve = false ;
114 for( int j = 0 ; j < clusters.size() ; j++ )
116 if( clusters.get( j ).getName().equals( cluster ) && clusters.get( j ).getSite().equals( site ))
118 _an.get( i ).setInCluster( true ) ;
119 _an.get( i ).setMapped( false ) ;
120 _an.get( i ).setSite( clusters.get( j ).getSite() ) ;
121 _an.get( i ).setCluster( clusters.get( j ).getName() ) ;
122 clusters.get( j ).addGNode( _an.get( i ) ) ;
131 Cluster cl = new Cluster() ;
133 cl.setName( cluster ) ;
136 _an.get( i ).setInCluster( true ) ;
137 _an.get( i ).setMapped( false ) ;
138 _an.get( i ).setSite( site ) ;
139 _an.get( i ).setCluster( cluster ) ;
141 cl.addGNode( _an.get( i ) ) ;
147 gr.addClusters( clusters ) ;
156 * Return the three parts of the name of a node on Grid'5000.
157 * @param _name The full name of the G5K node
158 * @return The three parts of the name
160 protected static String[] decodeG5Knames( String _name )
163 String tab[] = new String[ 5 ] ;
164 String ret[] = new String[ 3 ] ;
166 temp = _name.replace('.', '!' ) ;
168 tab = temp.split( "!" ) ;
173 tab = ret[0].split( "-" ) ;
175 ret[ 1 ] = tab[ 0 ] ;
182 * Write the Grid object in an XML file.
183 * @param _gl Grid graph to be write
184 * @param _path File's path
185 * @param _file File's name
187 public static void writeGrid( Grid _gl, String _path, String _file )
189 if( _file.equals( "" ) )
191 System.err.println( "No file's name !\n" ) ;
195 if ( ! _file.endsWith( ".xml" ) )
197 _file = _file + ".xml"; // On ajoute l'extension xml au nom du fichier
200 if( ! _file.equals( "" ) )
204 if( _path.length() != 0 )
206 path = _path+"/"+_file ;
208 path = new String( "./" + _file ) ;
211 XStream xstream = new XStream( new DomDriver() ) ;
213 String xml = xstream.toXML( _gl ) ;
215 PrintWriter ecrivain = null ;
218 ecrivain = new PrintWriter( new OutputStreamWriter( new FileOutputStream( path ), "UTF8" ) ) ;
220 ecrivain.println( "<?xml version=\"1.0\" encoding=\"UTF8\"?>" ) ;
221 ecrivain.println( xml ) ;
224 } catch( Exception e ) {
225 System.err.println( "\nError during the write opération !\n" ) ;
226 e.printStackTrace() ;
232 * Write an application Graph in a file.
233 * @param _gr Application Graph to be write
234 * @param _path File's path
235 * @param _file File's name
237 public static void writeGraph( Graph _gr, String _path, String _file )
239 if( _file.equals( "" ) )
241 System.err.println( "No file's name !\n" ) ;
245 if ( ! _file.endsWith( ".xml" ) )
247 _file = _file + ".xml"; // On ajoute l'extension xml au nom du fichier
250 if( ! _file.equals( "" ) )
254 if( _path.length() != 0 )
256 path = _path+"/"+_file ;
258 path = new String( "./" + _file ) ;
261 XStream xstream = new XStream( new DomDriver() ) ;
263 String xml = xstream.toXML( _gr ) ;
265 PrintWriter ecrivain = null ;
268 ecrivain = new PrintWriter( new OutputStreamWriter( new FileOutputStream( path ), "UTF8" ) ) ;
270 ecrivain.println( "<?xml version=\"1.0\" encoding=\"UTF8\"?>" ) ;
271 ecrivain.println( xml ) ;
274 } catch( Exception e ) {
275 System.err.println( "\nError during the write opération !\n" ) ;
276 e.printStackTrace() ;
283 * Write a mapping done in a file.
284 * @param _mp The mapping done to be write
285 * @param _path File's path
286 * @param _file File's name
288 public static void writeMapping( Mapping _mp, String _path, String _file )
290 if( _file.equals( "" ) )
292 System.err.println( "No file's name !\n" ) ;
296 if ( ! _file.endsWith( ".xml" ) )
298 _file = _file + ".xml"; // On ajoute l'extension xml au nom du fichier
301 if( ! _file.equals( "" ) )
305 if( _path.length() != 0 )
307 path = _path+"/"+_file ;
309 path = new String( "./" + _file ) ;
312 XStream xstream = new XStream( new DomDriver() ) ;
314 String xml = xstream.toXML( _mp ) ;
316 PrintWriter ecrivain = null ;
319 ecrivain = new PrintWriter( new OutputStreamWriter( new FileOutputStream( path ), "UTF8" ) ) ;
321 ecrivain.println( "<?xml version=\"1.0\" encoding=\"UTF8\"?>" ) ;
322 ecrivain.println( xml ) ;
325 } catch( Exception e ) {
326 System.err.println( "\nError during the write opération !\n" ) ;
327 e.printStackTrace() ;
334 * Read an application Graph from a file.
335 * @param _file File's name
336 * @param _path File's path
337 * @return The application Graph read
339 public static Graph readGraph( String _path, String _file )
341 if ( _file.equals( "" ) )
343 System.err.println( "No file's name !\n" ) ;
347 if ( ! _file.endsWith( ".xml" ) )
349 _file = _file + ".xml"; // On ajoute l'extension xml au nom du fichier
354 if( _path.length() != 0 )
356 path = _path+"/"+_file ;
358 path = new String( "./" + _file ) ;
363 XStream xstream = new XStream( new DomDriver() ) ;
366 gr = (Graph) xstream.fromXML( new FileInputStream( path ) ) ;
367 } catch( FileNotFoundException e ) {
368 System.err.println( "File not found !\n" ) ;
371 } catch( ClassCastException e ) {
372 System.err.println( "The file does not contain a valid Graph" ) ;
373 e.printStackTrace() ;
382 * Read a Grid graph from a file.
383 * @param _file File's name
384 * @param _path File's path
385 * @return The Grid graph read
387 public static Grid readGrid( String _path, String _file )
389 if ( _file.equals( "" ) )
391 System.err.println( "No file's name !\n" ) ;
395 if ( ! _file.endsWith( ".xml" ) )
397 _file = _file + ".xml"; // On ajoute l'extension xml au nom du fichier
402 if( _path.length() != 0 )
404 path = _path+"/"+_file ;
406 path = new String( "./" + _file ) ;
411 XStream xstream = new XStream( new DomDriver() ) ;
414 gr = (Grid) xstream.fromXML( new FileInputStream( path ) ) ;
415 } catch( FileNotFoundException e ) {
416 System.err.println( "File not found !\n" ) ;
419 } catch( ClassCastException e ) {
420 System.err.println( "The file does not contain a valid Grid" ) ;
421 e.printStackTrace() ;
430 * Read a Mapping done from a file.
431 * @param _file File's name
432 * @param _path File's path
433 * @return The Mapping read
435 public static Mapping readMapping( String _path, String _file )
437 if ( _file.equals( "" ) )
439 System.err.println( "No file's name !\n" ) ;
443 if ( ! _file.endsWith( ".xml" ) )
445 _file = _file + ".xml"; // On ajoute l'extension xml au nom du fichier
450 if( _path.length() != 0 )
452 path = _path+"/"+_file ;
454 path = new String( "./" + _file ) ;
459 XStream xstream = new XStream( new DomDriver() ) ;
462 mp = (Mapping) xstream.fromXML( new FileInputStream( path ) ) ;
463 } catch( FileNotFoundException e ) {
464 System.err.println( "File not found !\n" ) ;
467 } catch( ClassCastException e ) {
468 System.err.println( "The file does not contain a valid Grid" ) ;
469 e.printStackTrace() ;
479 /** La programmation est un art, respectons ceux qui la pratiquent !! **/