Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
57345cd7126a6e50d52fa03d3d73ca48eb8b4276
[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         private ArrayList<Cluster> clusters ;
18         private ArrayList<GNode> gnodesList;
19         private boolean gnodesList_done;
20         
21         
22         /**
23          * Default constructor
24          */
25         public Grid()
26         {
27                 clusters = new ArrayList<Cluster>() ;
28                 gnodesList = new ArrayList<GNode>() ;
29                 gnodesList_done = false ;
30         }
31         
32         
33         /**
34          * Add a cluster in the grid.
35          * @param c Cluster to be add
36          */
37         public void addCluster( Cluster c )
38         {
39                 if( c != null )
40                 {
41                         clusters.add( c ) ;
42                         
43                         for( int i = 0 ; i < c.getNbGNode() ; i++ )
44                         {
45                                 gnodesList.add( c.getGNodes().get( i ) ) ;
46                         }
47                 }
48         }
49         
50         
51         /**
52          * Add a clusters list in the grid.
53          * @param al List of clusters to be add
54          */
55         public void addClusters( ArrayList<Cluster> al )
56         {
57                 if( al != null )
58                 {
59                         int nbCLusterNodes = 0 ;
60                         
61                         for( int i = 0 ; i < al.size() ; i++ )
62                         {
63                                 clusters.add( al.get( i ) ) ;
64                                 nbCLusterNodes = al.get( i ).getNbGNode() ;
65                                 
66                                 for( int j = 0 ; j < nbCLusterNodes ; j++ )
67                                 {
68                                         gnodesList.add( al.get( i ).getGNodes().get( j ) ) ;
69                                 }
70                         }
71                 }
72         }
73
74         
75         /**
76          * Return the amount of clusters in the grid.
77          * @return The amount of clusters
78          */
79         public int getNbCluster()
80         {
81                 return clusters.size() ;
82         }
83         
84         
85         /**
86          * Return the amount of computing nodes in the grid.
87          * @return The amount of computing nodes
88          */
89         public int getNbGNode()
90         {
91                 return gnodesList.size() ;
92         }
93         
94         
95         /**
96          * Return the grid in a clusters list view.
97          * @return Clusters list
98          */
99         public ArrayList<Cluster> getClusters()
100         {
101                 return clusters ;
102         }
103         
104         
105         /**
106          * Compute and return the distance between two clusters.
107          * @param _g1 First cluster
108          * @param _g2 Second cluster
109          * @return The distance between the two clusters
110          */
111         public double getDistance( GNode _g1, GNode _g2 )
112         {
113                 double d = 0 ;
114                 
115                 if( _g1.equals( _g2 ) ) 
116                 {
117                         return d ;
118                 }
119                 
120                 String cluster1 = "c1", cluster2 = "c2", site1 = "s1", site2 = "s2" ;
121                 
122                 for( int i = 0 ; i < clusters.size() ; i++ )
123                 {
124                         if( clusters.get( i ).isIn( _g1 ) != -1 )
125                         {
126                                 cluster1 = clusters.get( i ).getName() ;
127                                 site1 = clusters.get( i ).getSite() ;
128                         }
129                         
130                         if( clusters.get( i ).isIn( _g2 ) != -1 )
131                         {
132                                 cluster2 = clusters.get( i ).getName() ;
133                                 site2 = clusters.get( i ).getSite() ;
134                         }
135                 }
136                 
137                 if( cluster1.compareTo( cluster2 ) == 0 )
138                 {
139                         d = 1 ;
140                 } else {
141                         if( site1.compareTo( site2 ) == 0 )
142                         {
143                                 d = 2 ;
144                         } else {
145                                 d = 3 ;
146                         }
147                 }
148                 
149                 return d ;
150         }
151         
152         
153         /**
154          * Return the list of computing nodes in the grid.
155          * @return The list of computing nodes
156          */
157         public ArrayList<GNode> getGNodes()
158         {
159                 if( ! gnodesList_done )
160                 {
161                         gnodesList = new ArrayList<GNode>() ;
162                         
163                         for( int i = 0 ; i < clusters.size() ; i++ )
164                         {
165                                 ArrayList<GNode> ar = clusters.get( i ).getGNodes() ;
166                                 
167                                 for( int j = 0 ; j < ar.size() ; j++ )
168                                 {
169                                         gnodesList.add( ar.get( j ) ) ;
170                                 }
171                         }
172                         
173                         gnodesList_done = true ;
174                 }
175                 
176                 return gnodesList ;
177         }
178         
179         
180         /**
181          * Upgrade the grid with new nodes.
182          * @param _gnodes The list of new nodes
183          */
184         public void updateGrid( ArrayList<GNode> _gnodes )
185         {
186                 if( _gnodes != null && _gnodes.size() != 0 )
187                 {
188                         for( int i = 0 ; i < _gnodes.size() ; i++ )
189                         {
190                                 /** Searching the cluster in which the node should be added **/
191                                 int j = 0 ;
192                                 for( j = 0; j < clusters.size(); j++ )
193                                 {
194                                         if( _gnodes.get( i ).getCluster().equalsIgnoreCase( clusters.get( j ).getName() ) )
195                                         {
196                                                 int pos = clusters.get( j ).isIn( _gnodes.get( i ) ) ;
197                                                 
198                                                 if( pos == -1 )
199                                                 {
200                                                         _gnodes.get( i ).setCluster( clusters.get( j ).getName() ) ;
201                                                         _gnodes.get( i ).setSite( clusters.get( j ).getSite() ) ;
202                                                         _gnodes.get( i ).setInCluster( true ) ;
203                                                         _gnodes.get( i ).setMapped( false ) ;
204                                                 
205                                                         clusters.get( j ).addGNode( _gnodes.get( i ) ) ;
206                                                         gnodesList.add( _gnodes.get( i ) ) ;
207                                                         
208                                                 } else {
209                                                         clusters.get( j ).setGNodeStatus( _gnodes.get( i ), _gnodes.get( i ).getMapped() ) ;
210                                                 }
211                                                 
212                                                 break ;
213                                         }
214                                 }
215                                 
216                                 /** The cluster was not found, so it is a new one **/
217                                 if( j == clusters.size() )
218                                 {
219                                         String site = "", cluster = "" ;
220                                         Cluster nClust = new Cluster() ;
221                                         
222                                         
223                                         String names[] = Utils.decodeG5Knames( _gnodes.get( i ).getName() ) ;
224                                         
225                                         cluster = names[ 1 ] ;
226                                         site = names[ 2 ] ;
227                                         
228                                         nClust.setName( cluster ) ;
229                                         nClust.setSite( site ) ;
230                                         
231                                         _gnodes.get( i ).setInCluster( true ) ;
232                                         _gnodes.get( i ).setMapped( false ) ;
233                                         _gnodes.get( i ).setSite( site ) ;
234                                         _gnodes.get( i ).setCluster( cluster ) ;
235                                         
236                                         nClust.addGNode( _gnodes.get( i ) ) ;
237                                         
238                                         clusters.add( nClust ) ;
239                                         gnodesList.add( _gnodes.get( i ) ) ;
240                                 }
241                         }
242                         
243                         gnodesList_done = false ;
244                 }
245         }
246         
247         
248         /**
249          * Upgrade the grid with a new node.
250          * @param _g The new node
251          */
252         public void addGNode( GNode _g )
253         {
254                 if( _g != null )
255                 {
256                         /** Searching the cluster in which the node should be added **/
257                         int j = 0 ;
258                         for( j = 0; j < clusters.size(); j++ )
259                         {
260                                 if( _g.getCluster().equalsIgnoreCase( clusters.get( j ).getName() ) )
261                                 {
262                                         int pos = clusters.get( j ).isIn( _g ) ;
263                                                 
264                                         if( pos == -1 )
265                                         {
266                                                 _g.setSite( clusters.get( j ).getSite() ) ;
267                                                 _g.setInCluster( true ) ;
268                                                 _g.setMapped( false ) ;
269                                                 
270                                                 clusters.get( j ).addGNode( _g ) ;
271                                                 gnodesList.add( _g ) ;
272                                                         
273                                         } else {
274                                                 clusters.get( j ).removeGNode( _g ) ;
275                                                 clusters.get( j ).addGNode( _g ) ;
276                                         }
277                                                 
278                                         break ;
279                                 }
280                         }
281                                 
282                         /** The cluster was not found, so it is a new one **/
283                         if( j == clusters.size() )
284                         {
285                                 String site = "", cluster = "" ;
286                                 Cluster nClust = new Cluster() ;
287                                                 
288                                 String names[] = Utils.decodeG5Knames( _g.getName() ) ;
289                                         
290                                 cluster = names[ 1 ] ;
291                                 site = names[ 2 ] ;
292                                 
293                                 System.out.println("** (Grid) Creation of cluster: "+cluster);
294                                 
295                                 nClust.setName( cluster ) ;
296                                 nClust.setSite( site ) ;
297                                         
298                                 _g.setInCluster( true ) ;
299                                 _g.setMapped( false ) ;
300                                 _g.setSite( site ) ;
301                                 _g.setCluster( cluster ) ;
302                                         
303                                 nClust.addGNode( _g ) ;
304                                         
305                                 clusters.add( nClust ) ;
306                                 gnodesList.add( _g ) ;
307                         }
308                 }
309                         
310                 gnodesList_done = false ;
311         }
312         
313         
314         /**
315          * Remove a computing node from the grid.
316          * @param _dead The node to be removed
317          */
318         public void removeGNode( GNode _dead ) 
319         {
320                 if( _dead != null )
321                 {
322                         /** Removing GNode from its cluster **/ 
323                         int id = getClusterOfNode( _dead ) ; 
324                         
325                         if( id != -1 )
326                         {
327                                 clusters.get( id ).removeGNode( _dead ) ;
328                         } else {
329                                 System.err.println( "(Grid) Cluster of dead node not found!" ) ;
330                         }
331
332                         /** Removing the dead node from the global list **/
333                         int i = 0 ;
334                         for( i = 0 ; i < gnodesList.size() ; i++ )
335                         {
336                                 if( _dead.getId() == gnodesList.get( i ).getId() )
337                                 {
338                                         gnodesList.remove( i ) ;
339                                         break ;
340                                 }
341                         }
342
343                 } else {
344                         System.err.println( "(Grid) The GNode to be deleted is null!" ) ;
345                 }
346
347                 gnodesList_done = false ;
348         }
349         
350         
351         /**
352          * Change the mapping status of a node in the grid and in its cluster.
353          * @param _g The node to change the mapping status
354          * @param _status The node's mapping status
355          */
356         public void setMappedStatus( GNode _g, boolean _status )
357         {
358                 if( _g != null )
359                 {
360                         /** Change in the cluster **/
361                         int id = getClusterOfNode( _g ) ;
362                         
363                         if( id != -1 )
364                         {
365                                 clusters.get(id).setGNodeStatus( _g, _status ) ;
366                         } else {
367                                 System.err.println( "(Grid) Cluster "+_g.getCluster()+" not found!" ) ;
368                         }
369                         
370                         /** Change in local list **/
371                         for( int i = 0 ; i < gnodesList.size() ; i++ )
372                         {
373                                 if( _g.getId() == gnodesList.get( i ).getId() )
374                                 {
375                                         gnodesList.get( i ).setMapped( _status ) ;
376                                         break ;
377                                 }
378                         }
379                 }
380         }
381         
382         
383         /**
384          * Search and return the cluster containing the specified node.
385          * @param _g A node
386          * @return The cluster containing the node
387          */
388         public int getClusterOfNode( GNode _g )
389         {
390                 int ret = -1 ;
391                 
392                 if( _g != null )
393                 {
394                         for( int i = 0 ; i < clusters.size() ; i++ )
395                         {
396                                 if( _g.getCluster().equalsIgnoreCase( clusters.get( i ).getName() ) ) 
397                                 {
398                                         if( _g.getSite().equalsIgnoreCase( clusters.get( i ).getSite() ) ) 
399                                         {
400                                                 ret = i ;
401                                                 break ;
402                                         }
403                                 }
404                         }
405                 }
406                 
407                 return ret ;
408         }
409         
410         /**
411          * Compute the heterogeneity degree of the grid.
412          * This is based on the relative standard deviation.
413          * @return The heterogeneity degree of the grid
414          */
415         public double getHeterogenityDegre()
416         {
417                 if( ! ( gnodesList.size() > 0 ) )
418                 {
419                         System.err.println( "Mapping - Heterogeneity degree computation! " +
420                                         "List's size not corresponding!" ) ;
421                         return -1 ;
422                 }
423                 
424                 double hd = -1 ;
425                 
426                 double average = 0 ;
427                 double std = 0 ;
428                 double temp = 0 ;
429                 
430                 /** Computation of the average power of computing nodes **/
431                 for( int i = 0 ; i < gnodesList.size() ; i++ )
432                 {
433                         temp += gnodesList.get(i).getPower() ;
434                 }
435                 
436                 average = temp / gnodesList.size() ;
437                 
438                 /** Computation of the variance **/
439                 temp = 0 ;
440                 for( int i = 0 ; i < gnodesList.size() ; i++ )
441                 {
442                         temp += Math.pow( ( gnodesList.get(i).getPower() - average ), 2 ) ;
443                 }
444                 
445                 /** Computation of the standard deviation **/
446                 temp = temp / gnodesList.size() ;
447                 std = Math.sqrt( temp ) ;
448                 
449                 /** Computation of the relative standard deviation **/
450                 hd = std / average ;
451                 
452                 /** Correction of the maximum value **/
453                 if( hd > 1 )
454                 {
455                         hd = 1 ;
456                 }
457                 
458                 
459                 return hd ;
460         }
461         
462         /**
463          * Initialization of computing nodes in the grid. Set all
464          * of these nodes to be not mapped on, and do the same thing in each
465          * of its clusters.
466          */     
467         public void initGNodes()
468         {
469                 /** Initialization of the local nodes **/
470                 for( int i = 0 ; i < gnodesList.size() ; i++ )
471                 {
472                         gnodesList.get( i ).setMapped( false ) ;
473                 }
474                 
475                 /** Initialization in clusters **/
476                 for( int i = 0 ; i < clusters.size() ; i++ )
477                 {
478                         clusters.get( i ).initGNodes() ;
479                 }
480         }
481         
482         
483         
484         /**
485          * Initialization of a set of computing nodes in the grid. Set all
486          * of these given nodes to be not mapped on, and propagates in each
487          * of its clusters.
488          * @param _ag The nodes' list to initialize
489          */     
490         public void initGNodes( ArrayList<GNode> _ag )
491         {
492                 if( _ag != null && _ag.size() > 0 )
493                 {
494                         /** Initialization of the local and clusters nodes **/
495                         for( int i = 0 ; i < _ag.size() ; i++ )
496                         {
497                                 setMappedStatus( _ag.get( i ), false ) ;
498                                 
499                                 int id = getClusterOfNode( _ag.get( i ) ) ;
500                                 
501                                 if( id != -1 )
502                                 {
503                                         clusters.get(id).setGNodeStatus( _ag.get( i ), false ) ;
504                                 } else {
505                                         System.err.println( "Cluster not found" ) ;
506                                 }
507                         }
508                 }
509         }
510         
511         
512         
513         /**
514          * Print a comprehensible text version of the grid.
515          */
516         public void print()
517         {
518                 System.out.println();
519                 System.out.println( "\t=> Grid composition:\n" ) ; 
520                 for( int i = 0 ; i < clusters.size() ; i++ )
521                 {
522                         System.out.println( "\t\tCluster \""+ clusters.get(i).getName() +"\" : " + clusters.get(i).getNbGNode() ) ;
523                 }
524                 
525                 System.out.println();
526         }
527
528
529         /**
530          * Return the average power of the grid, by computing the average for 
531          * eac cluster.
532          * @return The average power of the grid
533          */
534         public double getAvgFreePower() 
535         {
536                 double ret = 0 ;
537                 int nb = 0 ;
538                 
539                 if( clusters != null && clusters.size() > 0 )
540                 {
541                         for( int i = 0 ; i < clusters.size() ; i++ )
542                         {
543                                 nb++ ;
544                         
545                                 ret += clusters.get( i ).getAvailablePower() / clusters.get( i ).getNbFreeNodes() ;
546                         }
547                 
548                         ret = ret / nb ;
549                         
550                 }
551                 
552                 return ret ;
553         }
554         
555
556         /**
557          * Return the amount of freenodes available in the whole grid.
558          * @return The amount of available nodes
559          */
560         public int getNbFreenodes()
561         {
562                 int ret = 0 ;
563                 
564                 for( int i = 0 ; i < clusters.size() ; i++ )
565                         ret += clusters.get(i).getNbFreeNodes() ;
566                 return ret ;
567         }
568         
569 }
570
571
572 /** La programmation est un art, respectons ceux qui la pratiquent !! **/