Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Correction of bugs and new functionalities.
[mapping.git] / src / and / Mapping / Grid.java
1 package and.Mapping ;
2
3
4 import java.io.Serializable;
5 import java.util.ArrayList;
6
7
8
9 /**
10  * Class representing a computing grid, composed of multiple clusters
11  * @author Sébastien Miquée
12  */
13 public class Grid implements Serializable
14 {
15         private static final long serialVersionUID = 1L;
16
17         
18 //      private int nb_cluster ;
19 //      private int nb_node ;
20         private ArrayList<Cluster> clusters ;
21         private ArrayList<GNode> gnodesList;
22         private boolean gnodesList_done;
23         
24         
25         /**
26          * Default constructor
27          */
28         public Grid()
29         {
30 //              nb_cluster = 0 ;
31 //              nb_node = 0 ;
32                 clusters = new ArrayList<Cluster>() ;
33                 gnodesList = new ArrayList<GNode>() ;
34                 gnodesList_done = false ;
35         }
36         
37         
38         /**
39          * Add a cluster in the grid.
40          * @param c Cluster to be add
41          */
42         public void addCluster( Cluster c )
43         {
44                 if( c != null )
45                 {
46                         clusters.add( c ) ;
47                         
48                         for( int i = 0 ; i < c.getNbGNode() ; i++ )
49                         {
50                                 gnodesList.add( c.getGNodes().get( i ) ) ;
51                         }
52 //                      nb_cluster++ ;
53 //                      nb_node += c.getNbGNode() ;
54                 }
55         }
56         
57         
58         /**
59          * Add a clusters list in the grid.
60          * @param al List of clusters to be add
61          */
62         public void addClusters( ArrayList<Cluster> al )
63         {
64                 if( al != null )
65                 {
66                         int nbCLusterNodes = 0 ;
67                         
68                         for( int i = 0 ; i < al.size() ; i++ )
69                         {
70                                 clusters.add( al.get( i ) ) ;
71 //                              nb_cluster++ ;
72                                 nbCLusterNodes = al.get( i ).getNbGNode() ;
73 //                              nb_node += nbCLusterNodes ;
74                                 
75                                 for( int j = 0 ; j < nbCLusterNodes ; j++ )
76                                 {
77                                         gnodesList.add( al.get( i ).getGNodes().get( j ) ) ;
78                                 }
79                         }
80                 }
81         }
82
83         
84         /**
85          * Return the amount of clusters in the grid.
86          * @return The amount of clusters
87          */
88         public int getNbCluster()
89         {
90                 return clusters.size() ;
91 //              return nb_cluster ;
92         }
93         
94         
95         /**
96          * Return the amount of computing nodes in the grid.
97          * @return The amount of computing nodes
98          */
99         public int getNbGNode()
100         {
101                 return gnodesList.size() ;
102 //              return nb_node ;
103         }
104         
105         
106         /**
107          * Return the grid in a clusters list view.
108          * @return Clusters list
109          */
110         public ArrayList<Cluster> getClusters()
111         {
112                 return clusters ;
113         }
114         
115         
116 //      /**
117 //       * Initialization of clusters.
118 //       */
119 //      public void initClusters()
120 //      {
121 //              for( int i = 0 ; i < nb_cluster ; i++ )
122 //              {
123 //                      clusters.get( i ).initIndice() ;
124 //              }
125 //      }
126         
127         
128         /**
129          * Compute and return the distance between two clusters.
130          * @param _g1 First cluster
131          * @param _g2 Second cluster
132          * @return The distance between the two clusters
133          */
134         public double getDistance( GNode _g1, GNode _g2 )
135         {
136                 double d = 0 ;
137                 
138                 if( _g1.equals( _g2 ) ) 
139                 {
140                         return d ;
141                 }
142                 
143                 String cluster1 = "c1", cluster2 = "c2", site1 = "s1", site2 = "s2" ;
144                 
145                 for( int i = 0 ; i < clusters.size() ; i++ )
146                 {
147                         if( clusters.get( i ).isIn( _g1 ) != -1 )
148                         {
149                                 cluster1 = clusters.get( i ).getName() ;
150                                 site1 = clusters.get( i ).getSite() ;
151                         }
152                         
153                         if( clusters.get( i ).isIn( _g2 ) != -1 )
154                         {
155                                 cluster2 = clusters.get( i ).getName() ;
156                                 site2 = clusters.get( i ).getSite() ;
157                         }
158                 }
159                 
160                 //
161                 
162                 if( cluster1.compareTo( cluster2 ) == 0 )
163                 {
164                         d = 1 ;
165                 } else {
166                         if( site1.compareTo( site2 ) == 0 )
167                         {
168                                 d = 2 ;
169                         } else {
170                                 d = 3 ;
171                         }
172                 }
173                 
174                 return d ;
175         }
176         
177         
178         /**
179          * Return the list of computing nodes in the grid.
180          * @return The list of computing nodes
181          */
182         public ArrayList<GNode> getGNodes()
183         {
184                 if( ! gnodesList_done )
185                 {
186                         gnodesList = new ArrayList<GNode>() ;
187                         
188                         for( int i = 0 ; i < clusters.size() ; i++ )
189                         {
190                                 ArrayList<GNode> ar = clusters.get( i ).getGNodes() ;
191                                 
192                                 for( int j = 0 ; j < ar.size() ; j++ )
193                                 {
194                                         gnodesList.add( ar.get( j ) ) ;
195                                 }
196                         }
197                         
198                         gnodesList_done = true ;
199                 }
200                 
201                 return gnodesList ;
202         }
203         
204         
205         /**
206          * Upgrade the grid with new nodes.
207          * @param _gnodes The list of new nodes
208          */
209         public void updateGrid( ArrayList<GNode> _gnodes )
210         {
211                 if( _gnodes != null && _gnodes.size() != 0 )
212                 {
213                         for( int i = 0 ; i < _gnodes.size() ; i++ )
214                         {
215                                 /** Searching the cluster in which the node should be added **/
216                                 int j = 0 ;
217                                 for( j = 0; j < clusters.size(); j++ )
218                                 {
219                                         if( _gnodes.get( i ).getCluster().equalsIgnoreCase( clusters.get( j ).getName() ) )
220                                         {
221                                                 int pos = clusters.get( j ).isIn( _gnodes.get( i ) ) ;
222                                                 
223                                                 if( pos == -1 )
224                                                 {
225                                                         _gnodes.get( i ).setCluster( clusters.get( j ).getName() ) ;
226                                                         _gnodes.get( i ).setSite( clusters.get( j ).getSite() ) ;
227                                                         _gnodes.get( i ).setInCluster( true ) ;
228                                                         _gnodes.get( i ).setMapped( false ) ;
229                                                 
230                                                         clusters.get( i ).addGNode( _gnodes.get( j ) ) ;
231 //                                                      nb_node++ ;
232                                                         gnodesList.add( _gnodes.get( i ) ) ;
233                                                         
234                                                 } else {
235                                                         clusters.get( j ).setGNodeStatus( _gnodes.get( i ), _gnodes.get( i ).getMapped() ) ;
236                                                 }
237                                                 
238                                                 break ;
239                                         }
240                                 }
241                                 
242                                 /** The cluster was not found, so it is a new one **/
243                                 if( j == clusters.size() )
244                                 {
245                                         String site = "", cluster = "" ;
246                                         Cluster nClust = new Cluster() ;
247                                         
248                                         
249                                         String names[] = Utils.decodeG5Knames( _gnodes.get( i ).getName() ) ;
250                                         
251                                         cluster = names[ 1 ] ;
252                                         site = names[ 2 ] ;
253                                         
254                                         nClust.setName( cluster ) ;
255                                         nClust.setSite( site ) ;
256                                         
257                                         _gnodes.get( i ).setInCluster( true ) ;
258                                         _gnodes.get( i ).setMapped( false ) ;
259                                         _gnodes.get( i ).setSite( site ) ;
260                                         _gnodes.get( i ).setCluster( cluster ) ;
261                                         
262                                         nClust.addGNode( _gnodes.get( i ) ) ;
263 //                                      nb_cluster++ ;
264                                         
265                                         clusters.add( nClust ) ;
266 //                                      nb_node++ ;
267                                         gnodesList.add( _gnodes.get( i ) ) ;
268                                 }
269                         }
270                         
271                         gnodesList_done = false ;
272                 }
273         }
274         
275         
276         /**
277          * Remove a computing node from the grid.
278          * @param _dead The node to be removed
279          */
280         public void removeGNode( GNode _dead ) 
281         {
282                 if( _dead != null )
283                 {
284                         if( _dead.getMapped() )
285                         {
286                                 String site = "", cluster = "" ;
287                         
288                                 site = _dead.getSite() ;
289                                 cluster = _dead.getCluster() ;
290                         
291                                 /** Removing GNode from its cluster **/                 
292                                 for( int j = 0 ; j < clusters.size() ; j++ )
293                                 {
294                                         if( clusters.get( j ).getName().equals( cluster ) && clusters.get( j ).getSite().equals( site ))
295                                         {
296                                                 clusters.get( j ).removeGNode( _dead ) ;
297                                         
298                                                 break ;
299                                         }
300                                 }
301                         }
302                         
303                         /** Removing the dead node from the global list **/
304                         for( int i = 0 ; i < gnodesList.size() ; i++ )
305                         {
306                                 if( _dead.getId() == gnodesList.get( i ).getId() )
307                                 {
308                                         gnodesList.remove( i ) ;
309                                         break ;
310                                 }
311                         }
312                 }
313                 
314 //              nb_node-- ;
315                 gnodesList_done = false ;
316         }
317         
318         
319         /**
320          * Change the mapping status of a node in the grid and in its cluster.
321          * @param _g The node to change the mapping status
322          * @param _status The node's mapping status
323          */
324         public void setMappedStatus( GNode _g, boolean _status )
325         {
326                 if( _g != null )
327                 {
328                         getClusterOfNode( _g ).setGNodeStatus( _g, _status ) ;
329                         
330                         for( int i = 0 ; i < gnodesList.size() ; i++ )
331                         {
332                                 if( _g.getId() == gnodesList.get( i ).getId() )
333                                 {
334                                         gnodesList.get( i ).setMapped( _status ) ;
335                                         break ;
336                                 }
337                         }
338                 }
339         }
340         
341         
342         /**
343          * Search and return the cluster containing the specified node.
344          * @param _g A node
345          * @return The cluster containing the node
346          */
347         public Cluster getClusterOfNode( GNode _g )
348         {
349                 if( _g != null )
350                 {
351                         for( int i = 0 ; i < clusters.size() ; i++ )
352                         {
353                                 if( _g.getCluster().equalsIgnoreCase( clusters.get( i ).getName() ) ) 
354                                 {
355                                         return clusters.get( i ) ;
356                                 }
357                         }
358                 }
359                 
360                 return null ;
361         }
362         
363         /**
364          * Compute the heterogeneity degree of the grid.
365          * This is based on the relative standard deviation.
366          * @return The heterogeneity degree of the grid
367          */
368         public double getHeterogenityDegre()
369         {
370                 if( ! ( gnodesList.size() > 0 ) )
371                 {
372                         System.err.println( "Mapping - Heterogeneity degree computation! " +
373                                         "List's size not corresponding!" ) ;
374                         return -1 ;
375                 }
376                 
377                 double hd = -1 ;
378                 
379                 double average = 0 ;
380                 double std = 0 ;
381                 double temp = 0 ;
382                 
383                 /** Computation of the average power of computing nodes **/
384                 for( int i = 0 ; i < gnodesList.size() ; i++ )
385                 {
386                         temp += gnodesList.get(i).getPower() ;
387                 }
388                 
389                 average = temp / gnodesList.size() ;
390                 
391                 /** Computation of the variance **/
392                 temp = 0 ;
393                 for( int i = 0 ; i < gnodesList.size() ; i++ )
394                 {
395                         temp += Math.pow( ( gnodesList.get(i).getPower() - average ), 2 ) ;
396                 }
397                 
398                 /** Computation of the standard deviation **/
399                 temp = temp / gnodesList.size() ;
400                 std = Math.sqrt( temp ) ;
401                 
402                 /** Computation of the relative standard deviation
403                  * plus modifications 
404                  */
405                 hd = 100 * std / average / 10 ;
406                 
407                 
408                 return hd ;
409         }
410         
411         
412         /**
413          * Print a comprehensible text version of the grid.
414          */
415         public void print()
416         {
417                 System.out.println();
418                 System.out.println( "\t=> Grid composition:\n" ) ; 
419                 for( int i = 0 ; i < clusters.size() ; i++ )
420                 {
421                         System.out.println( "\t\tCluster \""+ clusters.get(i).getName() +"\" : " + clusters.get(i).getNbGNode() ) ;
422                 }
423                 
424                 System.out.println();
425         }
426         
427 }
428
429
430 /** La programmation est un art, respectons ceux qui la pratiquent !! **/