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 name !" ) ;
74 /* Updating node information */
75 n.setFrequency( frequency ) ;
76 n.setMFlops( mflops ) ;
77 n.setMemory( memory ) ;
78 n.setNb_cores( nbCore ) ;
85 * Creation of the representation of the grid, according to clusters into sites.
86 * This representation may only fit on Grid'5000 like architectures (with computing
87 * nodes name based on the following pattern |cluster_name|id_of_node_into_cluster|.|site_of_cluster|.|organisation|.*|).
88 * @param _an the list of computing nodes connected
89 * @return the grid's architecture
91 public static Grid createGridG5k( ArrayList<GNode> _an )
93 Grid gr = new Grid() ;
98 String nom, cluster, site ;
101 ArrayList<Cluster> clusters = new ArrayList<Cluster>() ;
103 for( int i = 0 ; i < _an.size() ; i++ )
105 /* Variables edition */
106 String tab[] = new String[ 5 ] ;
108 temp = _an.get( i ).getName().trim().replace('.', '!' ) ;
110 tab = temp.split( "!" ) ;
115 tab = nom.split( "-" ) ;
123 //System.out.println( nom + " dans " + cluster + " du site " + site ) ;
126 /* Insertion du noeud dans son 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 ).setSite( clusters.get( j ).getSite() ) ;
135 _an.get( i ).setCluster( clusters.get( j ).getName() ) ;
136 clusters.get( j ).addGNode( _an.get( i ) ) ;
145 Cluster cl = new Cluster() ;
147 cl.setName( cluster ) ;
150 _an.get( i ).setInCluster( true ) ;
151 _an.get( i ).setSite( site ) ;
152 _an.get( i ).setCluster( cluster ) ;
154 cl.addGNode( _an.get( i ) ) ;
160 gr.addClusters( clusters ) ;
169 * Write the Grid object in an XML file.
170 * @param _gl Grid graph to be write
171 * @param _file File's name
172 * @param _path File's path
174 public static void writeGrid( Grid _gl, String _file, String _path )
176 if ( ! _file.equals( "" ) && ! _file.endsWith( ".xml" ) )
178 _file = _file + ".xml"; // On ajoute l'extension xml au nom du fichier
181 if( ! _file.equals( "" ) )
185 if( _path.length() != 0 )
187 path = _path+"/"+_file ;
189 path = new String( "./" + _file ) ;
192 XStream xstream = new XStream( new DomDriver() ) ;
194 String xml = xstream.toXML( _gl ) ;
196 PrintWriter ecrivain = null ;
199 ecrivain = new PrintWriter( new OutputStreamWriter( new FileOutputStream( path ), "UTF8" ) ) ;
201 ecrivain.println( "<?xml version=\"1.0\" encoding=\"UTF8\"?>" ) ;
202 ecrivain.println( xml ) ;
205 } catch( Exception e ) {
206 System.err.println( "\nError during the write opération !\n" ) ;
207 e.printStackTrace() ;
213 * Write an application Graph in a file.
214 * @param _gr Application Graph to be write
215 * @param _file File's name
216 * @param _path File's path
218 public static void writeGraph( Graph _gr, String _file, String _path )
220 if ( ! _file.equals( "" ) && ! _file.endsWith( ".xml" ) )
222 _file = _file + ".xml"; // On ajoute l'extension xml au nom du fichier
225 if( ! _file.equals( "" ) )
229 if( _path.length() != 0 )
231 path = _path+"/"+_file ;
233 path = new String( "./" + _file ) ;
236 XStream xstream = new XStream( new DomDriver() ) ;
238 String xml = xstream.toXML( _gr ) ;
240 PrintWriter ecrivain = null ;
243 ecrivain = new PrintWriter( new OutputStreamWriter( new FileOutputStream( path ), "UTF8" ) ) ;
245 ecrivain.println( "<?xml version=\"1.0\" encoding=\"UTF8\"?>" ) ;
246 ecrivain.println( xml ) ;
249 } catch( Exception e ) {
250 System.err.println( "\nError during the write opération !\n" ) ;
251 e.printStackTrace() ;
258 * Read an application Graph from a file.
259 * @param _file File's name
260 * @return The application Graph read
262 public static Graph readGraph( String _file )
264 if ( _file.equals( "" ) || ! _file.endsWith( ".xml" ) )
266 System.err.println( "Bad file !\n" ) ;
272 XStream xstream = new XStream( new DomDriver() ) ;
275 gr = (Graph) xstream.fromXML( new FileInputStream( _file ) ) ;
276 } catch( FileNotFoundException e ) {
277 System.err.println( "File not found !\n" ) ;
280 } catch( ClassCastException e ) {
281 System.err.println( "The file does not contain a Graph" ) ;
282 e.printStackTrace() ;
291 * Read a Grid graph from a file.
292 * @param _file File's name
293 * @return The Grid graph read
295 public static Grid readGrid( String _file )
297 if ( _file.equals( "" ) || ! _file.endsWith( ".xml" ) )
299 System.err.println( "Bad file !\n" ) ;
305 XStream xstream = new XStream( new DomDriver() ) ;
308 gr = (Grid) xstream.fromXML( new FileInputStream( _file ) ) ;
309 } catch( FileNotFoundException e ) {
310 System.err.println( "File not found !\n" ) ;
313 } catch( ClassCastException e ) {
314 System.err.println( "The file does not contain a Grid" ) ;
315 e.printStackTrace() ;
325 /** La programmation est un art, respectons ceux qui la pratiquent !! **/