Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
16e46b1bff55b0ab3736a504311a6caa316af830
[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          * Plop !!
200          * @param _gnodes
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                                 
209                         }
210                         
211                         gnodesList_done = false ;
212                 }
213         }
214         
215         
216         /**
217          * Remove a computing node from the grid.
218          * @param _dead The node to be removed
219          */
220         public void removeGNode( GNode _dead ) 
221         {
222                 if( _dead != null )
223                 {
224                         if( _dead.getMapped() )
225                         {
226                                 String site = "", cluster = "" ;
227                         
228                                 site = _dead.getSite() ;
229                                 cluster = _dead.getCluster() ;
230                         
231                                 /* Removing GNode from its cluster */                   
232                                 for( int j = 0 ; j < clusters.size() ; j++ )
233                                 {
234                                         if( clusters.get( j ).getName().equals( cluster ) && clusters.get( j ).getSite().equals( site ))
235                                         {
236                                                 clusters.get( j ).removeGNode( _dead ) ;
237                                         
238                                                 break ;
239                                         }
240                                 }
241                         }
242                         
243                         /* Removing the dead node from the global list */
244                         for( int i = 0 ; i < nb_node ; i++ )
245                         {
246                                 if( _dead.getId() == gnodesList.get( i ).getId() )
247                                 {
248                                         gnodesList.remove( i ) ;
249                                         break ;
250                                 }
251                         }
252                 }
253                 
254                 nb_node-- ;
255                 gnodesList_done = false ;
256         }
257         
258         
259         /**
260          * Compute the heterogeneity degree of the grid.
261          * This is based on the relative standard deviation.
262          * @return The heterogeneity degree of the grid
263          */
264         public double getHeterogenityDegre()
265         {
266                 if( nb_node != gnodesList.size() )
267                 {
268                         System.err.println( "Mapping - Heterogeneity degree computation! " +
269                                         "List's size not corresponding!" ) ;
270                         return -1 ;
271                 }
272                 
273                 double hd = -1 ;
274                 
275                 double average = 0 ;
276                 double std = 0 ;
277                 double temp = 0 ;
278                 
279                 /** Computation of the average power of computing nodes **/
280                 for( int i = 0 ; i < nb_node ; i++ )
281                 {
282                         temp += gnodesList.get(i).getPower() ;
283                 }
284                 
285                 average = temp / nb_node ;
286                 
287                 /** Computation of the variance **/
288                 temp = 0 ;
289                 for( int i = 0 ; i < nb_node ; i++ )
290                 {
291                         temp += Math.pow( ( gnodesList.get(i).getPower() - average ), 2 ) ;
292                 }
293                 
294                 /** Computation of the standard deviation **/
295                 temp = temp / nb_node ;
296                 std = Math.sqrt( temp ) ;
297                 
298                 /** Computation of the relative standard deviation
299                  * plus modifications 
300                  */
301                 hd = 100 * std / average / 10 ;
302                 
303                 
304                 return hd ;
305         }
306         
307         
308         /**
309          * Print a comprehensible text version of the grid.
310          */
311         public void print()
312         {
313                 System.out.println();
314                 System.out.println( "\t=> Grid composition :\n" ) ; 
315                 for( int i = 0 ; i < nb_cluster ; i++ )
316                 {
317                         System.out.println( "\t\tCluster \""+ clusters.get(i).getName() +"\" : " + clusters.get(i).getNbGNode() ) ;
318                 }
319                 
320                 System.out.println();
321         }
322         
323 }
324
325
326 /** La programmation est un art, respectons ceux qui la pratiquent !! **/