Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
25cd4a748f8c993e9681bc5503db466a676cfd38
[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 temp ;
98                         String nom, cluster, site ;
99                 
100                         
101                         ArrayList<Cluster> clusters = new ArrayList<Cluster>() ;
102                         
103                         for( int i = 0 ; i < _an.size() ; i++ )
104                         {
105                                 /* Variables edition */
106                                 String tab[] = new String[ 5 ] ;                                
107                                 
108                                 temp = _an.get( i ).getName().trim().replace('.', '!' ) ;
109                                 
110                                 tab = temp.split( "!" ) ;
111                                 
112                                 nom = tab[ 0 ] ;
113                                 site = tab[ 1 ] ;
114                                 
115                                 tab = nom.split( "-" ) ;
116                                 
117                                 // IUT
118                                 //cluster = "iut" ;
119                                 
120                                 // G5K
121                                 cluster = tab[ 0 ] ;
122                                 
123                                 //System.out.println( nom + " dans " + cluster + " du site " + site ) ;
124                                 
125                                 
126                                 /* Insertion du noeud dans son 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 ).setSite( clusters.get( j ).getSite() ) ;
135                                                 _an.get( i ).setCluster( clusters.get( j ).getName() ) ;
136                                                 clusters.get( j ).addGNode( _an.get( i ) ) ;
137                                                 trouve  = true ;
138                                                 
139                                                 break ;
140                                         }
141                                 }
142                                 
143                                 if( ! trouve )
144                                 {
145                                         Cluster cl = new Cluster() ;
146                                         
147                                         cl.setName( cluster ) ;
148                                         cl.setSite( site ) ;
149                                         
150                                         _an.get( i ).setInCluster( true ) ;
151                                         _an.get( i ).setSite( site ) ;
152                                         _an.get( i ).setCluster( cluster ) ;
153                                         
154                                         cl.addGNode( _an.get( i ) ) ;
155                                         
156                                         clusters.add( cl ) ;
157                                 }       
158                         }
159                         
160                         gr.addClusters( clusters ) ;
161                         
162                 }
163                 
164                 return gr ;
165         }
166
167         
168         /**
169          * Write the Grid object in an XML file.
170          * @param _gl Grid graph to be write
171          * @param _file File's name 
172          * @param _path File's path
173          */
174         public static void writeGrid( Grid _gl, String _file, String _path )
175         {
176                 if( _file.equals( "" ) )
177                 {
178                         System.err.println( "No file's name !\n" ) ;
179                         return ;
180                 }
181                 
182                 if ( ! _file.endsWith( ".xml" ) )
183                 {
184                         _file = _file + ".xml"; // On ajoute l'extension xml au nom du fichier
185                 }
186                 
187                 if( ! _file.equals( "" ) )
188                 {
189                         String path = "" ;
190                         
191                         if( _path.length() != 0 )
192                         {
193                                 path = _path+"/"+_file ;
194                         } else {
195                                 path = new String( "./" + _file ) ;
196                         }
197                         
198                         XStream xstream = new XStream( new DomDriver() ) ;
199                         
200                         String xml = xstream.toXML( _gl ) ;
201                         
202                         PrintWriter ecrivain = null ;
203
204                         try {
205                                 ecrivain = new PrintWriter( new OutputStreamWriter( new FileOutputStream( path ), "UTF8" ) ) ;
206
207                                 ecrivain.println( "<?xml version=\"1.0\" encoding=\"UTF8\"?>" ) ;
208                                 ecrivain.println( xml ) ;
209
210                                 ecrivain.close() ;
211                         } catch( Exception e ) {
212                                 System.err.println( "\nError during the write opération !\n" ) ; 
213                                 e.printStackTrace() ;
214                         }
215                 }
216         }
217         
218         /**
219          * Write an application Graph in a file.
220          * @param _gr Application Graph to be write
221          * @param _file File's name
222          * @param _path File's path
223          */
224         public static void writeGraph( Graph _gr, String _file, String _path )
225         {
226                 if( _file.equals( "" ) )
227                 {
228                         System.err.println( "No file's name !\n" ) ;
229                         return ;
230                 }
231                 
232                 if ( ! _file.endsWith( ".xml" ) )
233                 {
234                         _file = _file + ".xml"; // On ajoute l'extension xml au nom du fichier
235                 }
236                 
237                 if( ! _file.equals( "" ) )
238                 {
239                         String path = "" ;
240                         
241                         if( _path.length() != 0 )
242                         {
243                                 path = _path+"/"+_file ;
244                         } else {
245                                 path = new String( "./" + _file ) ;
246                         }
247                         
248                         XStream xstream = new XStream( new DomDriver() ) ;
249                         
250                         String xml = xstream.toXML( _gr ) ;
251                         
252                         PrintWriter ecrivain = null ;
253
254                         try {
255                                 ecrivain = new PrintWriter( new OutputStreamWriter( new FileOutputStream( path ), "UTF8" ) ) ;
256
257                                 ecrivain.println( "<?xml version=\"1.0\" encoding=\"UTF8\"?>" ) ;
258                                 ecrivain.println( xml ) ;
259
260                                 ecrivain.close() ;
261                         } catch( Exception e ) {
262                                 System.err.println( "\nError during the write opération !\n" ) ;
263                                 e.printStackTrace() ;
264                         }
265                 }
266         }
267         
268         
269         /**
270          * Read an application Graph from a file.
271          * @param _file File's name
272          * @param _path File's path
273          * @return The application Graph read
274          */
275         public static Graph readGraph( String _path, String _file )
276         {
277                 if ( _file.equals( "" ) )
278                 {
279                         System.err.println( "No file's name !\n" ) ;
280                         return null ;
281                 }       
282                 
283                 if ( ! _file.endsWith( ".xml" ) )
284                 {
285                         _file = _file + ".xml"; // On ajoute l'extension xml au nom du fichier
286                 }
287                 
288                 String path = "" ;
289                 
290                 if( _path.length() != 0 )
291                 {
292                         path = _path+"/"+_file ;
293                 } else {
294                         path = new String( "./" + _file ) ;
295                 }       
296                         
297                 Graph gr = null ;
298                 
299                 XStream xstream = new XStream( new DomDriver() ) ;
300                 
301                 try {
302                         gr = (Graph) xstream.fromXML( new FileInputStream( path ) ) ;
303                 } catch( FileNotFoundException e ) {
304                         System.err.println( "File not found !\n" ) ;
305                         e.printStackTrace();
306                         return null ;
307                 } catch( ClassCastException e ) {
308                         System.err.println( "The file does not contain a valid Graph" ) ;
309                         e.printStackTrace() ;
310                         return null ;
311                 }
312                 
313                 return gr ;
314         }
315
316         
317         /**
318          * Read a Grid graph from a file.
319          * @param _file File's name
320          * @param _path File's path
321          * @return The Grid graph read
322          */
323         public static Grid readGrid( String _path, String _file )
324         {       
325                 if ( _file.equals( "" ) )
326                 {
327                         System.err.println( "No file's name !\n" ) ;
328                         return null ;
329                 }       
330                 
331                 if ( ! _file.endsWith( ".xml" ) )
332                 {
333                         _file = _file + ".xml"; // On ajoute l'extension xml au nom du fichier
334                 }
335
336                 String path = "" ;
337                 
338                 if( _path.length() != 0 )
339                 {
340                         path = _path+"/"+_file ;
341                 } else {
342                         path = new String( "./" + _file ) ;
343                 }       
344                 
345                 Grid gr = null ;
346                 
347                 XStream xstream = new XStream( new DomDriver() ) ;
348                 
349                 try {
350                         gr = (Grid) xstream.fromXML( new FileInputStream( path ) ) ;
351                 } catch( FileNotFoundException e ) {
352                         System.err.println( "File not found !\n" ) ;
353                         e.printStackTrace();
354                         return null ;
355                 } catch( ClassCastException e ) {
356                         System.err.println( "The file does not contain a valid Grid" ) ;
357                         e.printStackTrace() ;
358                         return null ;
359                 }
360                 
361                 return gr ;
362         }
363         
364 }
365
366
367 /** La programmation est un art, respectons ceux qui la pratiquent !! **/