Logo AND Algorithmique Numérique Distribuée

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