Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
068789da183a5d65d4583d42978de11e42a10464
[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.setCluster( names[1] ) ;
93                 n.setSite( 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                                 String names[] = decodeG5Knames( _an.get( i ).getName() ) ;
122                                 
123                                 cluster = names[ 1 ] ;
124                                 site = names[ 2 ] ;
125                                 
126                                 /* Insertion of the node in its cluster */
127                                 boolean trouve = false ;
128                                 
129                                 for( int j = 0 ; j < clusters.size() ; j++ )
130                                 {
131                                         if( clusters.get( j ).getName().equals( cluster ) && clusters.get( j ).getSite().equals( site ))
132                                         {
133                                                 _an.get( i ).setInCluster( true ) ;
134                                                 _an.get( i ).setMapped( false ) ;
135                                                 _an.get( i ).setSite( clusters.get( j ).getSite() ) ;
136                                                 _an.get( i ).setCluster( clusters.get( j ).getName() ) ;
137                                                 clusters.get( j ).addGNode( _an.get( i ) ) ;
138                                                 trouve  = true ;
139                                                 
140                                                 break ;
141                                         }
142                                 }
143                                 
144                                 if( ! trouve )
145                                 {
146                                         Cluster cl = new Cluster() ;
147                                         
148                                         cl.setName( cluster ) ;
149                                         cl.setSite( site ) ;
150                                         
151                                         _an.get( i ).setInCluster( true ) ;
152                                         _an.get( i ).setMapped( false ) ;
153                                         _an.get( i ).setSite( site ) ;
154                                         _an.get( i ).setCluster( cluster ) ;
155                                         
156                                         cl.addGNode( _an.get( i ) ) ;
157                                         
158                                         clusters.add( cl ) ;
159                                 }       
160                         }
161                         
162                         gr.addClusters( clusters ) ;
163                         
164                 }
165                 
166                 return gr ;
167         }
168
169         
170         /**
171          * Return the three parts of the name of a node on Grid'5000.
172          * @param _name The full name of the G5K node
173          * @return The three parts of the name
174          */
175         protected static String[] decodeG5Knames( String _name ) 
176         {
177                 String temp = "" ;
178                 String tab[] = new String[ 5 ] ;        
179                 String ret[] = new String[ 3 ] ;
180                 
181                 temp = _name.replace('.', '!' ) ;
182                 
183                 tab = temp.split( "!" ) ;
184                 
185                 ret[0] = tab[ 0 ] ;
186                 ret[2] = tab[ 1 ] ;
187                 
188                 tab = ret[0].split( "-" ) ;
189                 
190                 ret[ 1 ] = tab[ 0 ] ;
191                 
192                 return ret ;
193         }
194
195         
196         /**
197          * Write the Grid object in an XML file.
198          * @param _gl Grid graph to be write
199          * @param _path File's path
200          * @param _file File's name 
201          */
202         public static void writeGrid( Grid _gl, String _path, String _file )
203         {
204                 if( _file.equals( "" ) )
205                 {
206                         System.err.println( "No file's name !\n" ) ;
207                         return ;
208                 }
209                 
210                 if ( ! _file.endsWith( ".xml" ) )
211                 {
212                         _file = _file + ".xml"; // Adding xml extension to file
213                 }
214                 
215                 if( ! _file.equals( "" ) )
216                 {
217                         String path = "" ;
218                         
219                         if( _path.length() != 0 )
220                         {
221                                 path = _path+"/"+_file ;
222                         } else {
223                                 path = new String( "./" + _file ) ;
224                         }
225                         
226                         XStream xstream = new XStream( new DomDriver() ) ;
227                         
228                         String xml = xstream.toXML( _gl ) ;
229                         
230                         PrintWriter ecrivain = null ;
231
232                         try {
233                                 ecrivain = new PrintWriter( new OutputStreamWriter( new FileOutputStream( path ), "UTF8" ) ) ;
234
235                                 ecrivain.println( "<?xml version=\"1.0\" encoding=\"UTF8\"?>" ) ;
236                                 ecrivain.println( xml ) ;
237
238                                 ecrivain.close() ;
239                         } catch( Exception e ) {
240                                 System.err.println( "\nError during the write opération !\n" ) ; 
241                                 e.printStackTrace() ;
242                         }
243                 }
244         }
245         
246         /**
247          * Write an application Graph in a file.
248          * @param _gr Application Graph to be write
249          * @param _path File's path
250          * @param _file File's name
251          */
252         public static void writeGraph( Graph _gr, String _path, String _file )
253         {
254                 if( _file.equals( "" ) )
255                 {
256                         System.err.println( "No file's name !\n" ) ;
257                         return ;
258                 }
259                 
260                 if ( ! _file.endsWith( ".xml" ) )
261                 {
262                         _file = _file + ".xml"; // Adding xml extension to file
263                 }
264                 
265                 if( ! _file.equals( "" ) )
266                 {
267                         String path = "" ;
268                         
269                         if( _path.length() != 0 )
270                         {
271                                 path = _path+"/"+_file ;
272                         } else {
273                                 path = new String( "./" + _file ) ;
274                         }
275                         
276                         XStream xstream = new XStream( new DomDriver() ) ;
277                         
278                         String xml = xstream.toXML( _gr ) ;
279                         
280                         PrintWriter ecrivain = null ;
281
282                         try {
283                                 ecrivain = new PrintWriter( new OutputStreamWriter( new FileOutputStream( path ), "UTF8" ) ) ;
284
285                                 ecrivain.println( "<?xml version=\"1.0\" encoding=\"UTF8\"?>" ) ;
286                                 ecrivain.println( xml ) ;
287
288                                 ecrivain.close() ;
289                         } catch( Exception e ) {
290                                 System.err.println( "\nError during the write opération !\n" ) ;
291                                 e.printStackTrace() ;
292                         }
293                 }
294         }
295         
296         
297         /**
298          * Write a mapping done in a file.
299          * @param _mp The mapping done to be write
300          * @param _path File's path
301          * @param _file File's name
302          */
303         public static void writeMapping( Mapping _mp, String _path, String _file )
304         {
305                 if( _file.equals( "" ) )
306                 {
307                         System.err.println( "No file's name !\n" ) ;
308                         return ;
309                 }
310                 
311                 if ( ! _file.endsWith( ".xml" ) )
312                 {
313                         _file = _file + ".xml"; // Adding xml extension to file
314                 }
315                 
316                 if( ! _file.equals( "" ) )
317                 {
318                         String path = "" ;
319                         
320                         if( _path.length() != 0 )
321                         {
322                                 path = _path+"/"+_file ;
323                         } else {
324                                 path = new String( "./" + _file ) ;
325                         }
326                         
327                         XStream xstream = new XStream( new DomDriver() ) ;
328                         
329                         String xml = xstream.toXML( _mp ) ;
330                         
331                         PrintWriter ecrivain = null ;
332
333                         try {
334                                 ecrivain = new PrintWriter( new OutputStreamWriter( new FileOutputStream( path ), "UTF8" ) ) ;
335
336                                 ecrivain.println( "<?xml version=\"1.0\" encoding=\"UTF8\"?>" ) ;
337                                 ecrivain.println( xml ) ;
338
339                                 ecrivain.close() ;
340                         } catch( Exception e ) {
341                                 System.err.println( "\nError during the write opération !\n" ) ;
342                                 e.printStackTrace() ;
343                         }
344                 }
345         }
346         
347         
348         /**
349          * Read an application Graph from a file.
350          * @param _file File's name
351          * @param _path File's path
352          * @return The application Graph read
353          */
354         public static Graph readGraph( String _path, String _file )
355         {
356                 if ( _file.equals( "" ) )
357                 {
358                         System.err.println( "No file's name !\n" ) ;
359                         return null ;
360                 }       
361                 
362                 if ( ! _file.endsWith( ".xml" ) )
363                 {
364                         _file = _file + ".xml"; // Adding xml extension to file
365                 }
366                 
367                 String path = "" ;
368                 
369                 if( _path.length() != 0 )
370                 {
371                         path = _path+"/"+_file ;
372                 } else {
373                         path = new String( "./" + _file ) ;
374                 }       
375                         
376                 Graph gr = null ;
377                 
378                 XStream xstream = new XStream( new DomDriver() ) ;
379                 
380                 try {
381                         gr = (Graph) xstream.fromXML( new FileInputStream( path ) ) ;
382                 } catch( FileNotFoundException e ) {
383                         System.err.println( "File not found !\n" ) ;
384                         e.printStackTrace();
385                         return null ;
386                 } catch( ClassCastException e ) {
387                         System.err.println( "The file does not contain a valid Graph" ) ;
388                         e.printStackTrace() ;
389                         return null ;
390                 }
391                 
392                 return gr ;
393         }
394
395         
396         /**
397          * Read a Grid graph from a file.
398          * @param _file File's name
399          * @param _path File's path
400          * @return The Grid graph read
401          */
402         public static Grid readGrid( String _path, String _file )
403         {       
404                 if ( _file.equals( "" ) )
405                 {
406                         System.err.println( "No file's name !\n" ) ;
407                         return null ;
408                 }       
409                 
410                 if ( ! _file.endsWith( ".xml" ) )
411                 {
412                         _file = _file + ".xml"; // Adding xml extension to file
413                 }
414
415                 String path = "" ;
416                 
417                 if( _path.length() != 0 )
418                 {
419                         path = _path+"/"+_file ;
420                 } else {
421                         path = new String( "./" + _file ) ;
422                 }       
423                 
424                 Grid gr = null ;
425                 
426                 XStream xstream = new XStream( new DomDriver() ) ;
427                 
428                 try {
429                         gr = (Grid) xstream.fromXML( new FileInputStream( path ) ) ;
430                 } catch( FileNotFoundException e ) {
431                         System.err.println( "File not found !\n" ) ;
432                         e.printStackTrace();
433                         return null ;
434                 } catch( ClassCastException e ) {
435                         System.err.println( "The file does not contain a valid Grid" ) ;
436                         e.printStackTrace() ;
437                         return null ;
438                 }
439                 
440                 return gr ;
441         }
442         
443         
444         /**
445          * Read a Mapping done from a file.
446          * @param _file File's name
447          * @param _path File's path
448          * @return The Mapping read
449          */
450         public static Mapping readMapping( String _path, String _file )
451         {       
452                 if ( _file.equals( "" ) )
453                 {
454                         System.err.println( "No file's name !\n" ) ;
455                         return null ;
456                 }       
457                 
458                 if ( ! _file.endsWith( ".xml" ) )
459                 {
460                         _file = _file + ".xml"; // Adding xml extension to file
461                 }
462
463                 String path = "" ;
464                 
465                 if( _path.length() != 0 )
466                 {
467                         path = _path+"/"+_file ;
468                 } else {
469                         path = new String( "./" + _file ) ;
470                 }       
471                 
472                 Mapping mp = null ;
473                 
474                 XStream xstream = new XStream( new DomDriver() ) ;
475                 
476                 try {
477                         mp = (Mapping) xstream.fromXML( new FileInputStream( path ) ) ;
478                 } catch( FileNotFoundException e ) {
479                         System.err.println( "File not found !\n" ) ;
480                         e.printStackTrace();
481                         return null ;
482                 } catch( ClassCastException e ) {
483                         System.err.println( "The file does not contain a valid Grid" ) ;
484                         e.printStackTrace() ;
485                         return null ;
486                 }
487                 
488                 return mp ;
489         }
490         
491 }
492
493
494 /** La programmation est un art, respectons ceux qui la pratiquent !! **/