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 * Read an application Graph from a file.
284 * @param _file File's name
285 * @param _path File's path
286 * @return The application Graph read
288 public static Graph readGraph( 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
303 if( _path.length() != 0 )
305 path = _path+"/"+_file ;
307 path = new String( "./" + _file ) ;
312 XStream xstream = new XStream( new DomDriver() ) ;
315 gr = (Graph) xstream.fromXML( new FileInputStream( path ) ) ;
316 } catch( FileNotFoundException e ) {
317 System.err.println( "File not found !\n" ) ;
320 } catch( ClassCastException e ) {
321 System.err.println( "The file does not contain a valid Graph" ) ;
322 e.printStackTrace() ;
331 * Read a Grid graph from a file.
332 * @param _file File's name
333 * @param _path File's path
334 * @return The Grid graph read
336 public static Grid readGrid( String _path, String _file )
338 if ( _file.equals( "" ) )
340 System.err.println( "No file's name !\n" ) ;
344 if ( ! _file.endsWith( ".xml" ) )
346 _file = _file + ".xml"; // On ajoute l'extension xml au nom du fichier
351 if( _path.length() != 0 )
353 path = _path+"/"+_file ;
355 path = new String( "./" + _file ) ;
360 XStream xstream = new XStream( new DomDriver() ) ;
363 gr = (Grid) xstream.fromXML( new FileInputStream( path ) ) ;
364 } catch( FileNotFoundException e ) {
365 System.err.println( "File not found !\n" ) ;
368 } catch( ClassCastException e ) {
369 System.err.println( "The file does not contain a valid Grid" ) ;
370 e.printStackTrace() ;
380 /** La programmation est un art, respectons ceux qui la pratiquent !! **/