Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
eb3fb4f0a6940426187a3614d11edad5474a902d
[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                         nb_cluster ++ ;
48                         nb_node += c.getNbGNode() ;
49                 }
50         }
51         
52         
53         /**
54          * Add a clusters list in the grid.
55          * @param al List of clusters to be add
56          */
57         public void addClusters( ArrayList<Cluster> al )
58         {
59                 if( al != null )
60                 {
61                         int nbCLusterNodes = 0 ;
62                         
63                         for( int i = 0 ; i < al.size() ; i++ )
64                         {
65                                 clusters.add( al.get( i ) ) ;
66                                 nb_cluster ++ ;
67                                 nbCLusterNodes = al.get( i ).getNbGNode() ;
68                                 nb_node += nbCLusterNodes ;
69                                 
70                                 for( int j = 0 ; j < nbCLusterNodes ; j++ )
71                                 {
72                                         gnodesList.add( al.get( i ).getGNodes().get( j ) ) ;
73                                 }
74                         }
75                 }
76         }
77
78         
79         /**
80          * Return the amount of clusters in the grid.
81          * @return The amount of clusters
82          */
83         public int getNbCluster()
84         {
85                 return nb_cluster ;
86         }
87         
88         
89         /**
90          * Return the amount of computing nodes in the grid.
91          * @return The amount of computing nodes
92          */
93         public int getNbGNode()
94         {
95                 return nb_node ;
96         }
97         
98         
99         /**
100          * Return the grid in a clusters list view.
101          * @return Clusters list
102          */
103         public ArrayList<Cluster> getClusters()
104         {
105                 return clusters ;
106         }
107         
108         
109         /**
110          * Initialization of clusters.
111          */
112         public void initClusters()
113         {
114                 for( int i = 0 ; i < nb_cluster ; i++ )
115                 {
116                         clusters.get( i ).initIndice() ;
117                 }
118         }
119         
120         
121         /**
122          * Compute and return the distance between two clusters.
123          * @param _g1 First cluster
124          * @param _g2 Second cluster
125          * @return The distance between the two clusters
126          */
127         public double getDistance( GNode _g1, GNode _g2 )
128         {
129                 double d = 0 ;
130                 
131                 if( _g1.equals( _g2 ) ) 
132                 {
133                         return d ;
134                 }
135                 
136                 String cluster1 = "c1", cluster2 = "c2", site1 = "s1", site2 = "s2" ;
137                 
138                 for( int i = 0 ; i < clusters.size() ; i++ )
139                 {
140                         if( clusters.get( i ).isIn( _g1) )
141                         {
142                                 cluster1 = clusters.get( i ).getName() ;
143                                 site1 = clusters.get( i ).getSite() ;
144                         }
145                         
146                         if( clusters.get( i ).isIn( _g2) )
147                         {
148                                 cluster2 = clusters.get( i ).getName() ;
149                                 site2 = clusters.get( i ).getSite() ;
150                         }
151                 }
152                 
153                 //
154                 
155                 if( cluster1.compareTo( cluster2 ) == 0 )
156                 {
157                         d = 1 ;
158                 } else {
159                         if( site1.compareTo( site2 ) == 0 )
160                         {
161                                 d = 2 ;
162                         } else {
163                                 d = 3 ;
164                         }
165                 }
166                 
167                 return d ;
168         }
169         
170         
171         /**
172          * Return the list of computing nodes in the grid.
173          * @return The list of computing nodes
174          */
175         public ArrayList<GNode> getGNodes()
176         {
177                 if( ! gnodesList_done )
178                 {
179                         gnodesList = new ArrayList<GNode>() ;
180                         
181                         for( int i = 0 ; i < clusters.size() ; i++ )
182                         {
183                                 ArrayList<GNode> ar = clusters.get( i ).getGNodes() ;
184                                 
185                                 for( int j = 0 ; j < ar.size() ; j++ )
186                                 {
187                                         gnodesList.add( ar.get( j ) ) ;
188                                 }
189                         }
190                         
191                         gnodesList_done = true ;
192                 }
193                 
194                 return gnodesList ;
195         }
196         
197         
198         /**
199          * Upgrade the grid with new nodes.
200          * @param _gnodes The list of new nodes
201          */
202         public void updateGrid( ArrayList<GNode> _gnodes )
203         {
204                 if( _gnodes != null && _gnodes.size() != 0 )
205                 {
206                         for( int i = 0 ; i < _gnodes.size() ; i++ )
207                         {
208                                 /** Searching the cluster in which the node should be added **/
209                                 int j = 0 ;
210                                 for( j = 0; j < clusters.size(); j++ )
211                                 {
212                                         if( _gnodes.get( i ).getCluster().equalsIgnoreCase( clusters.get( j ).getName() ) )
213                                         {
214                                                 if( ! clusters.get( j ).isIn( _gnodes.get( i ) ) )
215                                                 {
216                                                         _gnodes.get( i ).setCluster( clusters.get( j ).getName() ) ;
217                                                         _gnodes.get( i ).setSite( clusters.get( j ).getSite() ) ;
218                                                         _gnodes.get( i ).setInCluster( true ) ;
219                                                         _gnodes.get( i ).setMapped( false ) ;
220                                                 
221                                                         clusters.get( i ).addGNode( _gnodes.get( j ) ) ;
222                                                         nb_node++ ;
223                                                         gnodesList.add( _gnodes.get( i ) ) ;
224                                                 }
225                                                 
226                                                 break ;
227                                         }
228                                 }
229                                 
230                                 /** The cluster was not found, so it is a new one **/
231                                 if( j == clusters.size() )
232                                 {
233                                         String site = "", cluster = "" ;
234                                         Cluster nClust = new Cluster() ;
235                                         
236                                         
237                                         String names[] = Utils.decodeG5Knames( _gnodes.get( i ).getName() ) ;
238                                         
239                                         cluster = names[ 1 ] ;
240                                         site = names[ 2 ] ;
241                                         
242                                         nClust.setName( cluster ) ;
243                                         nClust.setSite( site ) ;
244                                         
245                                         _gnodes.get( i ).setInCluster( true ) ;
246                                         _gnodes.get( i ).setMapped( false ) ;
247                                         _gnodes.get( i ).setSite( site ) ;
248                                         _gnodes.get( i ).setCluster( cluster ) ;
249                                         
250                                         nClust.addGNode( _gnodes.get( i ) ) ;
251                                         nb_cluster++ ;
252                                         
253                                         clusters.add( nClust ) ;
254                                         nb_node++ ;
255                                         gnodesList.add( _gnodes.get( i ) ) ;
256                                 }
257                         }
258                         
259                         gnodesList_done = false ;
260                 }
261         }
262         
263         
264         /**
265          * Remove a computing node from the grid.
266          * @param _dead The node to be removed
267          */
268         public void removeGNode( GNode _dead ) 
269         {
270                 if( _dead != null )
271                 {
272                         if( _dead.getMapped() )
273                         {
274                                 String site = "", cluster = "" ;
275                         
276                                 site = _dead.getSite() ;
277                                 cluster = _dead.getCluster() ;
278                         
279                                 /** Removing GNode from its cluster **/                 
280                                 for( int j = 0 ; j < clusters.size() ; j++ )
281                                 {
282                                         if( clusters.get( j ).getName().equals( cluster ) && clusters.get( j ).getSite().equals( site ))
283                                         {
284                                                 clusters.get( j ).removeGNode( _dead ) ;
285                                         
286                                                 break ;
287                                         }
288                                 }
289                         }
290                         
291                         /** Removing the dead node from the global list **/
292                         for( int i = 0 ; i < nb_node ; i++ )
293                         {
294                                 if( _dead.getId() == gnodesList.get( i ).getId() )
295                                 {
296                                         gnodesList.remove( i ) ;
297                                         break ;
298                                 }
299                         }
300                 }
301                 
302                 nb_node-- ;
303                 gnodesList_done = false ;
304         }
305         
306         
307         /**
308          * Compute the heterogeneity degree of the grid.
309          * This is based on the relative standard deviation.
310          * @return The heterogeneity degree of the grid
311          */
312         public double getHeterogenityDegre()
313         {
314                 if( nb_node != gnodesList.size() )
315                 {
316                         System.err.println( "Mapping - Heterogeneity degree computation! " +
317                                         "List's size not corresponding!" ) ;
318                         return -1 ;
319                 }
320                 
321                 double hd = -1 ;
322                 
323                 double average = 0 ;
324                 double std = 0 ;
325                 double temp = 0 ;
326                 
327                 /** Computation of the average power of computing nodes **/
328                 for( int i = 0 ; i < nb_node ; i++ )
329                 {
330                         temp += gnodesList.get(i).getPower() ;
331                 }
332                 
333                 average = temp / nb_node ;
334                 
335                 /** Computation of the variance **/
336                 temp = 0 ;
337                 for( int i = 0 ; i < nb_node ; i++ )
338                 {
339                         temp += Math.pow( ( gnodesList.get(i).getPower() - average ), 2 ) ;
340                 }
341                 
342                 /** Computation of the standard deviation **/
343                 temp = temp / nb_node ;
344                 std = Math.sqrt( temp ) ;
345                 
346                 /** Computation of the relative standard deviation
347                  * plus modifications 
348                  */
349                 hd = 100 * std / average / 10 ;
350                 
351                 
352                 return hd ;
353         }
354         
355         
356         /**
357          * Print a comprehensible text version of the grid.
358          */
359         public void print()
360         {
361                 System.out.println();
362                 System.out.println( "\t=> Grid composition :\n" ) ; 
363                 for( int i = 0 ; i < nb_cluster ; i++ )
364                 {
365                         System.out.println( "\t\tCluster \""+ clusters.get(i).getName() +"\" : " + clusters.get(i).getNbGNode() ) ;
366                 }
367                 
368                 System.out.println();
369         }
370         
371 }
372
373
374 /** La programmation est un art, respectons ceux qui la pratiquent !! **/