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.setClusterName( names[1] ) ;
93 n.setSiteName( 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 cluster = _an.get( i ).getClusterName() ; //names[ 1 ] ;
122 site = _an.get( i ).getSiteName() ; //names[ 2 ] ;
124 /* Insertion of the node in its cluster */
125 boolean found = false ;
127 for( int j = 0 ; j < clusters.size() ; j++ )
129 if( clusters.get( j ).getName().equalsIgnoreCase( cluster )
130 && clusters.get( j ).getSite().equalsIgnoreCase( site ))
132 _an.get( i ).setInCluster( true ) ;
133 _an.get( i ).setMapped( false ) ;
134 _an.get( i ).setCluster( clusters.get( j ) ) ;
135 clusters.get( j ).addGNode( _an.get( i ) ) ;
144 Cluster cl = new Cluster() ;
146 cl.setName( cluster ) ;
149 _an.get( i ).setInCluster( true ) ;
150 _an.get( i ).setMapped( false ) ;
151 _an.get( i ).setCluster( cl ) ;
153 cl.addGNode( _an.get( i ) ) ;
159 gr.addClusters( clusters ) ;
168 * Return the three parts of the name of a node on Grid'5000.
169 * @param _name The full name of the G5K node
170 * @return The three parts of the name
172 public static String[] decodeG5Knames( String _name )
175 String tab[] = new String[ 5 ] ;
176 String ret[] = new String[ 3 ] ;
178 temp = _name.replace('.', '!' ) ;
180 tab = temp.split( "!" ) ;
182 ret[0] = tab[ 0 ] ; // node name
183 ret[2] = tab[ 1 ] ; // site name
185 tab = ret[0].split( "-" ) ;
187 ret[ 1 ] = tab[ 0 ] ; // cluster name
194 * Write the Grid object in an XML file.
195 * @param _gl Grid graph to be write
196 * @param _path File's path
197 * @param _file File's name
199 public static void writeGrid( Grid _gl, String _path, String _file )
201 if( _file.equals( "" ) )
203 System.err.println( "No file's name !\n" ) ;
207 if ( ! _file.endsWith( ".xml" ) )
209 _file = _file + ".xml"; // Adding xml extension to file
212 if( ! _file.equals( "" ) )
216 if( _path.length() != 0 )
218 path = _path+"/"+_file ;
220 path = new String( "./" + _file ) ;
223 XStream xstream = new XStream( new DomDriver() ) ;
225 String xml = xstream.toXML( _gl ) ;
227 PrintWriter ecrivain = null ;
230 ecrivain = new PrintWriter( new OutputStreamWriter( new FileOutputStream( path ), "UTF8" ) ) ;
232 ecrivain.println( "<?xml version=\"1.0\" encoding=\"UTF8\"?>" ) ;
233 ecrivain.println( xml ) ;
236 } catch( Exception e ) {
237 System.err.println( "\nError during the write opération !\n" ) ;
238 e.printStackTrace() ;
244 * Write an application Graph in a file.
245 * @param _gr Application Graph to be write
246 * @param _path File's path
247 * @param _file File's name
249 public static void writeGraph( Graph _gr, String _path, String _file )
251 if( _file.equals( "" ) )
253 System.err.println( "No file's name !\n" ) ;
257 if ( ! _file.endsWith( ".xml" ) )
259 _file = _file + ".xml"; // Adding xml extension to file
262 if( ! _file.equals( "" ) )
266 if( _path.length() != 0 )
268 path = _path+"/"+_file ;
270 path = new String( "./" + _file ) ;
273 XStream xstream = new XStream( new DomDriver() ) ;
275 String xml = xstream.toXML( _gr ) ;
277 PrintWriter ecrivain = null ;
280 ecrivain = new PrintWriter( new OutputStreamWriter( new FileOutputStream( path ), "UTF8" ) ) ;
282 ecrivain.println( "<?xml version=\"1.0\" encoding=\"UTF8\"?>" ) ;
283 ecrivain.println( xml ) ;
286 } catch( Exception e ) {
287 System.err.println( "\nError during the write opération !\n" ) ;
288 e.printStackTrace() ;
295 * Write a mapping done in a file.
296 * @param _mp The mapping done to be write
297 * @param _path File's path
298 * @param _file File's name
300 public static void writeMapping( Mapping _mp, String _path, String _file )
302 if( _file.equals( "" ) )
304 System.err.println( "No file's name !\n" ) ;
308 if ( ! _file.endsWith( ".xml" ) )
310 _file = _file + ".xml"; // Adding xml extension to file
313 if( ! _file.equals( "" ) )
317 if( _path.length() != 0 )
319 path = _path+"/"+_file ;
321 path = new String( "./" + _file ) ;
324 XStream xstream = new XStream( new DomDriver() ) ;
326 String xml = xstream.toXML( _mp ) ;
328 PrintWriter ecrivain = null ;
331 ecrivain = new PrintWriter( new OutputStreamWriter( new FileOutputStream( path ), "UTF8" ) ) ;
333 ecrivain.println( "<?xml version=\"1.0\" encoding=\"UTF8\"?>" ) ;
334 ecrivain.println( xml ) ;
337 } catch( Exception e ) {
338 System.err.println( "\nError during the write opération !\n" ) ;
339 e.printStackTrace() ;
346 * Read an application Graph from a file.
347 * @param _file File's name
348 * @param _path File's path
349 * @return The application Graph read
351 public static Graph readGraph( String _path, String _file )
353 if ( _file.equals( "" ) )
355 System.err.println( "No file's name !\n" ) ;
359 if ( ! _file.endsWith( ".xml" ) )
361 _file = _file + ".xml"; // Adding xml extension to file
366 if( _path.length() != 0 )
368 path = _path+"/"+_file ;
370 path = new String( "./" + _file ) ;
375 XStream xstream = new XStream( new DomDriver() ) ;
378 gr = (Graph) xstream.fromXML( new FileInputStream( path ) ) ;
379 } catch( FileNotFoundException e ) {
380 System.err.println( "File not found !\n" ) ;
383 } catch( ClassCastException e ) {
384 System.err.println( "The file does not contain a valid Graph" ) ;
385 e.printStackTrace() ;
394 * Read a Grid graph from a file.
395 * @param _file File's name
396 * @param _path File's path
397 * @return The Grid graph read
399 public static Grid readGrid( String _path, String _file )
401 if ( _file.equals( "" ) )
403 System.err.println( "No file's name !\n" ) ;
407 if ( ! _file.endsWith( ".xml" ) )
409 _file = _file + ".xml"; // Adding xml extension to file
414 if( _path.length() != 0 )
416 path = _path+"/"+_file ;
418 path = new String( "./" + _file ) ;
423 XStream xstream = new XStream( new DomDriver() ) ;
426 gr = (Grid) xstream.fromXML( new FileInputStream( path ) ) ;
427 } catch( FileNotFoundException e ) {
428 System.err.println( "File not found !\n" ) ;
431 } catch( ClassCastException e ) {
432 System.err.println( "The file does not contain a valid Grid" ) ;
433 e.printStackTrace() ;
442 * Read a Mapping done from a file.
443 * @param _file File's name
444 * @param _path File's path
445 * @return The Mapping read
447 public static Mapping readMapping( String _path, String _file )
449 if ( _file.equals( "" ) )
451 System.err.println( "No file's name !\n" ) ;
455 if ( ! _file.endsWith( ".xml" ) )
457 _file = _file + ".xml"; // Adding xml extension to file
462 if( _path.length() != 0 )
464 path = _path+"/"+_file ;
466 path = new String( "./" + _file ) ;
471 XStream xstream = new XStream( new DomDriver() ) ;
474 mp = (Mapping) xstream.fromXML( new FileInputStream( path ) ) ;
475 } catch( FileNotFoundException e ) {
476 System.err.println( "File not found !\n" ) ;
479 } catch( ClassCastException e ) {
480 System.err.println( "The file does not contain a valid Mapping" ) ;
481 e.printStackTrace() ;
491 /** La programmation est un art, respectons ceux qui la pratiquent !! **/