Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Correction of some bugs.
[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          * Read an application Graph from a file.
284          * @param _file File's name
285          * @param _path File's path
286          * @return The application Graph read
287          */
288         public static Graph readGraph( String _path, String _file )
289         {
290                 if ( _file.equals( "" ) )
291                 {
292                         System.err.println( "No file's name !\n" ) ;
293                         return null ;
294                 }       
295                 
296                 if ( ! _file.endsWith( ".xml" ) )
297                 {
298                         _file = _file + ".xml"; // On ajoute l'extension xml au nom du fichier
299                 }
300                 
301                 String path = "" ;
302                 
303                 if( _path.length() != 0 )
304                 {
305                         path = _path+"/"+_file ;
306                 } else {
307                         path = new String( "./" + _file ) ;
308                 }       
309                         
310                 Graph gr = null ;
311                 
312                 XStream xstream = new XStream( new DomDriver() ) ;
313                 
314                 try {
315                         gr = (Graph) xstream.fromXML( new FileInputStream( path ) ) ;
316                 } catch( FileNotFoundException e ) {
317                         System.err.println( "File not found !\n" ) ;
318                         e.printStackTrace();
319                         return null ;
320                 } catch( ClassCastException e ) {
321                         System.err.println( "The file does not contain a valid Graph" ) ;
322                         e.printStackTrace() ;
323                         return null ;
324                 }
325                 
326                 return gr ;
327         }
328
329         
330         /**
331          * Read a Grid graph from a file.
332          * @param _file File's name
333          * @param _path File's path
334          * @return The Grid graph read
335          */
336         public static Grid readGrid( String _path, String _file )
337         {       
338                 if ( _file.equals( "" ) )
339                 {
340                         System.err.println( "No file's name !\n" ) ;
341                         return null ;
342                 }       
343                 
344                 if ( ! _file.endsWith( ".xml" ) )
345                 {
346                         _file = _file + ".xml"; // On ajoute l'extension xml au nom du fichier
347                 }
348
349                 String path = "" ;
350                 
351                 if( _path.length() != 0 )
352                 {
353                         path = _path+"/"+_file ;
354                 } else {
355                         path = new String( "./" + _file ) ;
356                 }       
357                 
358                 Grid gr = null ;
359                 
360                 XStream xstream = new XStream( new DomDriver() ) ;
361                 
362                 try {
363                         gr = (Grid) xstream.fromXML( new FileInputStream( path ) ) ;
364                 } catch( FileNotFoundException e ) {
365                         System.err.println( "File not found !\n" ) ;
366                         e.printStackTrace();
367                         return null ;
368                 } catch( ClassCastException e ) {
369                         System.err.println( "The file does not contain a valid Grid" ) ;
370                         e.printStackTrace() ;
371                         return null ;
372                 }
373                 
374                 return gr ;
375         }
376         
377 }
378
379
380 /** La programmation est un art, respectons ceux qui la pratiquent !! **/