Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
New version of MAHEVE plus corrections.
[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                 /* Host IP */
74                 String ip = null ;
75                 try {
76                         InetAddress addr = InetAddress.getLocalHost() ;
77                         ip = addr.getHostAddress() ;
78                 } catch( final Exception e ) {
79                         System.err.println( "Unalbe to retrieve host's name !" ) ;
80                         e.printStackTrace();
81                         System.exit( 1 ) ;
82                 } 
83
84                 String names[] = decodeG5Knames( name ) ;
85                 
86                 /* Updating node information */
87                 n.setFrequency( frequency ) ;
88                 n.setMFlops( mflops ) ;
89                 n.setMemory( memory ) ;
90                 n.setNb_cores( nbCore ) ;
91                 n.setName( name ) ;
92                 n.setClusterName( names[1] ) ;
93                 n.setSiteName( names[2] ) ;
94                 n.setIP( ip ) ;
95                 n.setMapped( false ) ;
96                 
97                 return n ;
98         }
99
100         /**
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
106          */
107         public static Grid createGridG5k( ArrayList<GNode> _an )
108         {
109                 Grid gr = new Grid() ;
110                 
111                 if( _an != null )
112                 {
113                         String cluster = "", site = "" ;                
114                         
115                         ArrayList<Cluster> clusters = new ArrayList<Cluster>() ;
116                         
117                         for( int i = 0 ; i < _an.size() ; i++ )
118                         {
119                                 /* Variables edition */         
120                                 
121                                 cluster = _an.get( i ).getClusterName() ; //names[ 1 ] ;
122                                 site = _an.get( i ).getSiteName() ; //names[ 2 ] ;
123                                 
124                                 /* Insertion of the node in its cluster */
125                                 boolean found = false ;
126                                 
127                                 for( int j = 0 ; j < clusters.size() ; j++ )
128                                 {
129                                         if( clusters.get( j ).getName().equalsIgnoreCase( cluster ) 
130                                                         && clusters.get( j ).getSite().equalsIgnoreCase( site ))
131                                         {
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 ) ) ;
136                                                 found  = true ;
137                                                 
138                                                 break ;
139                                         }
140                                 }
141                                 
142                                 if( ! found )
143                                 {
144                                         Cluster cl = new Cluster() ;
145                                         
146                                         cl.setName( cluster ) ;
147                                         cl.setSite( site ) ;
148                                         
149                                         _an.get( i ).setInCluster( true ) ;
150                                         _an.get( i ).setMapped( false ) ;
151                                         _an.get( i ).setCluster( cl ) ;
152                                         
153                                         cl.addGNode( _an.get( i ) ) ;
154                                         
155                                         clusters.add( cl ) ;
156                                 }       
157                         }
158                         
159                         gr.addClusters( clusters ) ;
160                         
161                 }
162                 
163                 return gr ;
164         }
165
166         
167         /**
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
171          */
172         public static String[] decodeG5Knames( String _name ) 
173         {
174                 String temp = "" ;
175                 String tab[] = new String[ 5 ] ;        
176                 String ret[] = new String[ 3 ] ;
177                 
178                 temp = _name.replace('.', '!' ) ;
179                 
180                 tab = temp.split( "!" ) ;
181                 
182                 ret[0] = tab[ 0 ] ;   // node name
183                 ret[2] = tab[ 1 ] ;   // site name
184                 
185                 tab = ret[0].split( "-" ) ;
186                 
187                 ret[ 1 ] = tab[ 0 ] ; // cluster name
188                 
189                 return ret ;
190         }
191
192         
193         /**
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 
198          */
199         public static void writeGrid( Grid _gl, String _path, String _file )
200         {
201                 if( _file.equals( "" ) )
202                 {
203                         System.err.println( "No file's name !\n" ) ;
204                         return ;
205                 }
206                 
207                 if ( ! _file.endsWith( ".xml" ) )
208                 {
209                         _file = _file + ".xml"; // Adding xml extension to file
210                 }
211                 
212                 if( ! _file.equals( "" ) )
213                 {
214                         String path = "" ;
215                         
216                         if( _path.length() != 0 )
217                         {
218                                 path = _path+"/"+_file ;
219                         } else {
220                                 path = new String( "./" + _file ) ;
221                         }
222                         
223                         XStream xstream = new XStream( new DomDriver() ) ;
224                         
225                         String xml = xstream.toXML( _gl ) ;
226                         
227                         PrintWriter ecrivain = null ;
228
229                         try {
230                                 ecrivain = new PrintWriter( new OutputStreamWriter( new FileOutputStream( path ), "UTF8" ) ) ;
231
232                                 ecrivain.println( "<?xml version=\"1.0\" encoding=\"UTF8\"?>" ) ;
233                                 ecrivain.println( xml ) ;
234
235                                 ecrivain.close() ;
236                         } catch( Exception e ) {
237                                 System.err.println( "\nError during the write opération !\n" ) ; 
238                                 e.printStackTrace() ;
239                         }
240                 }
241         }
242         
243         /**
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
248          */
249         public static void writeGraph( Graph _gr, String _path, String _file )
250         {
251                 if( _file.equals( "" ) )
252                 {
253                         System.err.println( "No file's name !\n" ) ;
254                         return ;
255                 }
256                 
257                 if ( ! _file.endsWith( ".xml" ) )
258                 {
259                         _file = _file + ".xml"; // Adding xml extension to file
260                 }
261                 
262                 if( ! _file.equals( "" ) )
263                 {
264                         String path = "" ;
265                         
266                         if( _path.length() != 0 )
267                         {
268                                 path = _path+"/"+_file ;
269                         } else {
270                                 path = new String( "./" + _file ) ;
271                         }
272                         
273                         XStream xstream = new XStream( new DomDriver() ) ;
274                         
275                         String xml = xstream.toXML( _gr ) ;
276                         
277                         PrintWriter ecrivain = null ;
278
279                         try {
280                                 ecrivain = new PrintWriter( new OutputStreamWriter( new FileOutputStream( path ), "UTF8" ) ) ;
281
282                                 ecrivain.println( "<?xml version=\"1.0\" encoding=\"UTF8\"?>" ) ;
283                                 ecrivain.println( xml ) ;
284
285                                 ecrivain.close() ;
286                         } catch( Exception e ) {
287                                 System.err.println( "\nError during the write opération !\n" ) ;
288                                 e.printStackTrace() ;
289                         }
290                 }
291         }
292         
293         
294         /**
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
299          */
300         public static void writeMapping( Mapping _mp, String _path, String _file )
301         {
302                 if( _file.equals( "" ) )
303                 {
304                         System.err.println( "No file's name !\n" ) ;
305                         return ;
306                 }
307                 
308                 if ( ! _file.endsWith( ".xml" ) )
309                 {
310                         _file = _file + ".xml"; // Adding xml extension to file
311                 }
312                 
313                 if( ! _file.equals( "" ) )
314                 {
315                         String path = "" ;
316                         
317                         if( _path.length() != 0 )
318                         {
319                                 path = _path+"/"+_file ;
320                         } else {
321                                 path = new String( "./" + _file ) ;
322                         }
323                         
324                         XStream xstream = new XStream( new DomDriver() ) ;
325                         
326                         String xml = xstream.toXML( _mp ) ;
327                         
328                         PrintWriter ecrivain = null ;
329
330                         try {
331                                 ecrivain = new PrintWriter( new OutputStreamWriter( new FileOutputStream( path ), "UTF8" ) ) ;
332
333                                 ecrivain.println( "<?xml version=\"1.0\" encoding=\"UTF8\"?>" ) ;
334                                 ecrivain.println( xml ) ;
335
336                                 ecrivain.close() ;
337                         } catch( Exception e ) {
338                                 System.err.println( "\nError during the write opération !\n" ) ;
339                                 e.printStackTrace() ;
340                         }
341                 }
342         }
343         
344         
345         /**
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
350          */
351         public static Graph readGraph( String _path, String _file )
352         {
353                 if ( _file.equals( "" ) )
354                 {
355                         System.err.println( "No file's name !\n" ) ;
356                         return null ;
357                 }       
358                 
359                 if ( ! _file.endsWith( ".xml" ) )
360                 {
361                         _file = _file + ".xml"; // Adding xml extension to file
362                 }
363                 
364                 String path = "" ;
365                 
366                 if( _path.length() != 0 )
367                 {
368                         path = _path+"/"+_file ;
369                 } else {
370                         path = new String( "./" + _file ) ;
371                 }       
372                         
373                 Graph gr = null ;
374                 
375                 XStream xstream = new XStream( new DomDriver() ) ;
376                 
377                 try {
378                         gr = (Graph) xstream.fromXML( new FileInputStream( path ) ) ;
379                 } catch( FileNotFoundException e ) {
380                         System.err.println( "File not found !\n" ) ;
381                         e.printStackTrace();
382                         return null ;
383                 } catch( ClassCastException e ) {
384                         System.err.println( "The file does not contain a valid Graph" ) ;
385                         e.printStackTrace() ;
386                         return null ;
387                 }
388                 
389                 return gr ;
390         }
391
392         
393         /**
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
398          */
399         public static Grid readGrid( String _path, String _file )
400         {       
401                 if ( _file.equals( "" ) )
402                 {
403                         System.err.println( "No file's name !\n" ) ;
404                         return null ;
405                 }       
406                 
407                 if ( ! _file.endsWith( ".xml" ) )
408                 {
409                         _file = _file + ".xml"; // Adding xml extension to file
410                 }
411
412                 String path = "" ;
413                 
414                 if( _path.length() != 0 )
415                 {
416                         path = _path+"/"+_file ;
417                 } else {
418                         path = new String( "./" + _file ) ;
419                 }       
420                 
421                 Grid gr = null ;
422                 
423                 XStream xstream = new XStream( new DomDriver() ) ;
424                 
425                 try {
426                         gr = (Grid) xstream.fromXML( new FileInputStream( path ) ) ;
427                 } catch( FileNotFoundException e ) {
428                         System.err.println( "File not found !\n" ) ;
429                         e.printStackTrace();
430                         return null ;
431                 } catch( ClassCastException e ) {
432                         System.err.println( "The file does not contain a valid Grid" ) ;
433                         e.printStackTrace() ;
434                         return null ;
435                 }
436                 
437                 return gr ;
438         }
439         
440         
441         /**
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
446          */
447         public static Mapping readMapping( String _path, String _file )
448         {       
449                 if ( _file.equals( "" ) )
450                 {
451                         System.err.println( "No file's name !\n" ) ;
452                         return null ;
453                 }       
454                 
455                 if ( ! _file.endsWith( ".xml" ) )
456                 {
457                         _file = _file + ".xml"; // Adding xml extension to file
458                 }
459
460                 String path = "" ;
461                 
462                 if( _path.length() != 0 )
463                 {
464                         path = _path+"/"+_file ;
465                 } else {
466                         path = new String( "./" + _file ) ;
467                 }       
468                 
469                 Mapping mp = null ;
470                 
471                 XStream xstream = new XStream( new DomDriver() ) ;
472                 
473                 try {
474                         mp = (Mapping) xstream.fromXML( new FileInputStream( path ) ) ;
475                 } catch( FileNotFoundException e ) {
476                         System.err.println( "File not found !\n" ) ;
477                         e.printStackTrace();
478                         return null ;
479                 } catch( ClassCastException e ) {
480                         System.err.println( "The file does not contain a valid Mapping" ) ;
481                         e.printStackTrace() ;
482                         return null ;
483                 }
484                 
485                 return mp ;
486         }
487         
488 }
489
490
491 /** La programmation est un art, respectons ceux qui la pratiquent !! **/