Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
7e05ca215422677326e564167eea0e7f8c371863
[mapping.git] / src / and / Mapping / Utils.java
1 package and.Mapping ;
2
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;
10
11 import com.thoughtworks.xstream.XStream;
12 import com.thoughtworks.xstream.io.xml.DomDriver;
13
14
15
16 /**
17  * Class providing some tools to the library
18  * @author Séastien Miquée
19  */
20 public class Utils 
21 {
22         /**
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
25          * algorithms.
26          * @return A node from the Mapping library
27          */
28         public static GNode createGNode()
29         {
30                 /** Using the Linpack class **/
31                 Linpack linpack = new Linpack() ;
32                 
33                 /**  **/
34                 GNode n = new GNode() ;
35                 int nbCore = 0 ;
36                 int frequency = 0 ;
37                 int mflops = 0 ;
38                 int memory = 0 ;
39                 String name = "" ;
40                 
41                 
42                 /** CPU information **/
43                 
44                 /* Amount of available computing cores */
45                 nbCore = Runtime.getRuntime().availableProcessors() ;
46                 
47                 /* Average MegaFlops of each core */
48                 double tmp_mflops = 0 ;
49                 int loop = 10 ;
50                 
51                 for( int i = 0 ; i < loop ; i++ )
52                 {
53                         tmp_mflops += linpack.getMFlops() ;
54                         System.gc() ;
55                 }
56                 mflops = (int) tmp_mflops / loop ;
57                 
58                 
59                 /** Memory information **/
60                 memory = (int) Runtime.getRuntime().maxMemory() / 1024 / 1024 ;
61                 
62                 
63                 /* Host name */
64                 try {
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 !" ) ;
69                         e.printStackTrace();
70                         System.exit( 1 ) ;
71                 } 
72
73                 
74                 /* Updating node information */
75                 n.setFrequency( frequency ) ;
76                 n.setMFlops( mflops ) ;
77                 n.setMemory( memory ) ;
78                 n.setNb_cores( nbCore ) ;
79                 n.setName( name ) ;
80                 
81                 return n ;
82         }
83
84         /**
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
90          */
91         public static Grid createGridG5k( ArrayList<GNode> _an )
92         {
93                 Grid gr = new Grid() ;
94                 
95                 if( _an != null )
96                 {
97                         String cluster = "", site = "" ;                
98                         
99                         ArrayList<Cluster> clusters = new ArrayList<Cluster>() ;
100                         
101                         for( int i = 0 ; i < _an.size() ; i++ )
102                         {
103                                 /* Variables edition */
104                                 
105                                 String names[] = decodeG5Knames( _an.get( i ).getName() ) ;
106                                 
107                                 cluster = names[ 1 ] ;
108                                 site = names[ 2 ] ;
109                                 
110                                 /* Insertion of the node in its cluster */
111                                 boolean trouve = false ;
112                                 
113                                 for( int j = 0 ; j < clusters.size() ; j++ )
114                                 {
115                                         if( clusters.get( j ).getName().equals( cluster ) && clusters.get( j ).getSite().equals( site ))
116                                         {
117                                                 _an.get( i ).setInCluster( true ) ;
118                                                 _an.get( i ).setMapped( false ) ;
119                                                 _an.get( i ).setSite( clusters.get( j ).getSite() ) ;
120                                                 _an.get( i ).setCluster( clusters.get( j ).getName() ) ;
121                                                 clusters.get( j ).addGNode( _an.get( i ) ) ;
122                                                 trouve  = true ;
123                                                 
124                                                 break ;
125                                         }
126                                 }
127                                 
128                                 if( ! trouve )
129                                 {
130                                         Cluster cl = new Cluster() ;
131                                         
132                                         cl.setName( cluster ) ;
133                                         cl.setSite( site ) ;
134                                         
135                                         _an.get( i ).setInCluster( true ) ;
136                                         _an.get( i ).setMapped( false ) ;
137                                         _an.get( i ).setSite( site ) ;
138                                         _an.get( i ).setCluster( cluster ) ;
139                                         
140                                         cl.addGNode( _an.get( i ) ) ;
141                                         
142                                         clusters.add( cl ) ;
143                                 }       
144                         }
145                         
146                         gr.addClusters( clusters ) ;
147                         
148                 }
149                 
150                 return gr ;
151         }
152
153         
154         /**
155          * Return the three parts of the name of a node on Grid'5000.
156          * @param _name The full name of the G5K node
157          * @return The three parts of the name
158          */
159         protected static String[] decodeG5Knames( String _name ) 
160         {
161                 String temp = "" ;
162                 String tab[] = new String[ 5 ] ;        
163                 String ret[] = new String[ 3 ] ;
164                 
165                 temp = _name.replace('.', '!' ) ;
166                 
167                 tab = temp.split( "!" ) ;
168                 
169                 ret[0] = tab[ 0 ] ;
170                 ret[2] = tab[ 1 ] ;
171                 
172                 tab = ret[0].split( "-" ) ;
173                 
174                 ret[ 1 ] = tab[ 0 ] ;
175                 
176                 return ret ;
177         }
178
179         
180         /**
181          * Write the Grid object in an XML file.
182          * @param _gl Grid graph to be write
183          * @param _file File's name 
184          * @param _path File's path
185          */
186         public static void writeGrid( Grid _gl, String _file, String _path )
187         {
188                 if( _file.equals( "" ) )
189                 {
190                         System.err.println( "No file's name !\n" ) ;
191                         return ;
192                 }
193                 
194                 if ( ! _file.endsWith( ".xml" ) )
195                 {
196                         _file = _file + ".xml"; // On ajoute l'extension xml au nom du fichier
197                 }
198                 
199                 if( ! _file.equals( "" ) )
200                 {
201                         String path = "" ;
202                         
203                         if( _path.length() != 0 )
204                         {
205                                 path = _path+"/"+_file ;
206                         } else {
207                                 path = new String( "./" + _file ) ;
208                         }
209                         
210                         XStream xstream = new XStream( new DomDriver() ) ;
211                         
212                         String xml = xstream.toXML( _gl ) ;
213                         
214                         PrintWriter ecrivain = null ;
215
216                         try {
217                                 ecrivain = new PrintWriter( new OutputStreamWriter( new FileOutputStream( path ), "UTF8" ) ) ;
218
219                                 ecrivain.println( "<?xml version=\"1.0\" encoding=\"UTF8\"?>" ) ;
220                                 ecrivain.println( xml ) ;
221
222                                 ecrivain.close() ;
223                         } catch( Exception e ) {
224                                 System.err.println( "\nError during the write opération !\n" ) ; 
225                                 e.printStackTrace() ;
226                         }
227                 }
228         }
229         
230         /**
231          * Write an application Graph in a file.
232          * @param _gr Application Graph to be write
233          * @param _file File's name
234          * @param _path File's path
235          */
236         public static void writeGraph( Graph _gr, String _file, String _path )
237         {
238                 if( _file.equals( "" ) )
239                 {
240                         System.err.println( "No file's name !\n" ) ;
241                         return ;
242                 }
243                 
244                 if ( ! _file.endsWith( ".xml" ) )
245                 {
246                         _file = _file + ".xml"; // On ajoute l'extension xml au nom du fichier
247                 }
248                 
249                 if( ! _file.equals( "" ) )
250                 {
251                         String path = "" ;
252                         
253                         if( _path.length() != 0 )
254                         {
255                                 path = _path+"/"+_file ;
256                         } else {
257                                 path = new String( "./" + _file ) ;
258                         }
259                         
260                         XStream xstream = new XStream( new DomDriver() ) ;
261                         
262                         String xml = xstream.toXML( _gr ) ;
263                         
264                         PrintWriter ecrivain = null ;
265
266                         try {
267                                 ecrivain = new PrintWriter( new OutputStreamWriter( new FileOutputStream( path ), "UTF8" ) ) ;
268
269                                 ecrivain.println( "<?xml version=\"1.0\" encoding=\"UTF8\"?>" ) ;
270                                 ecrivain.println( xml ) ;
271
272                                 ecrivain.close() ;
273                         } catch( Exception e ) {
274                                 System.err.println( "\nError during the write opération !\n" ) ;
275                                 e.printStackTrace() ;
276                         }
277                 }
278         }
279         
280         
281         /**
282          * Read an application Graph from a file.
283          * @param _file File's name
284          * @param _path File's path
285          * @return The application Graph read
286          */
287         public static Graph readGraph( String _path, String _file )
288         {
289                 if ( _file.equals( "" ) )
290                 {
291                         System.err.println( "No file's name !\n" ) ;
292                         return null ;
293                 }       
294                 
295                 if ( ! _file.endsWith( ".xml" ) )
296                 {
297                         _file = _file + ".xml"; // On ajoute l'extension xml au nom du fichier
298                 }
299                 
300                 String path = "" ;
301                 
302                 if( _path.length() != 0 )
303                 {
304                         path = _path+"/"+_file ;
305                 } else {
306                         path = new String( "./" + _file ) ;
307                 }       
308                         
309                 Graph gr = null ;
310                 
311                 XStream xstream = new XStream( new DomDriver() ) ;
312                 
313                 try {
314                         gr = (Graph) xstream.fromXML( new FileInputStream( path ) ) ;
315                 } catch( FileNotFoundException e ) {
316                         System.err.println( "File not found !\n" ) ;
317                         e.printStackTrace();
318                         return null ;
319                 } catch( ClassCastException e ) {
320                         System.err.println( "The file does not contain a valid Graph" ) ;
321                         e.printStackTrace() ;
322                         return null ;
323                 }
324                 
325                 return gr ;
326         }
327
328         
329         /**
330          * Read a Grid graph from a file.
331          * @param _file File's name
332          * @param _path File's path
333          * @return The Grid graph read
334          */
335         public static Grid readGrid( String _path, String _file )
336         {       
337                 if ( _file.equals( "" ) )
338                 {
339                         System.err.println( "No file's name !\n" ) ;
340                         return null ;
341                 }       
342                 
343                 if ( ! _file.endsWith( ".xml" ) )
344                 {
345                         _file = _file + ".xml"; // On ajoute l'extension xml au nom du fichier
346                 }
347
348                 String path = "" ;
349                 
350                 if( _path.length() != 0 )
351                 {
352                         path = _path+"/"+_file ;
353                 } else {
354                         path = new String( "./" + _file ) ;
355                 }       
356                 
357                 Grid gr = null ;
358                 
359                 XStream xstream = new XStream( new DomDriver() ) ;
360                 
361                 try {
362                         gr = (Grid) xstream.fromXML( new FileInputStream( path ) ) ;
363                 } catch( FileNotFoundException e ) {
364                         System.err.println( "File not found !\n" ) ;
365                         e.printStackTrace();
366                         return null ;
367                 } catch( ClassCastException e ) {
368                         System.err.println( "The file does not contain a valid Grid" ) ;
369                         e.printStackTrace() ;
370                         return null ;
371                 }
372                 
373                 return gr ;
374         }
375         
376 }
377
378
379 /** La programmation est un art, respectons ceux qui la pratiquent !! **/