Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
2451b95c0c135f7872feb9b4905915a18150eff5
[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 = 10 ;
140                 } else {
141                         if( site1.compareTo( site2 ) == 0 )
142                         {
143                                 d = 20 ;
144                         } else {
145                                 d = 30 ;
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         /**
412          * Compute the heterogeneity degree of the grid.
413          * This is based on the relative standard deviation.
414          * @return The heterogeneity degree of the grid
415          */
416         public double getHeterogenityDegre()
417         {
418                 if( ! ( gnodesList.size() > 0 ) )
419                 {
420                         System.err.println( "Mapping - Heterogeneity degree computation! " +
421                                         "List's size not corresponding!" ) ;
422                         return -1 ;
423                 }
424                 
425                 double hd = -1 ;
426                 
427                 double average = 0 ;
428                 double std = 0 ;
429                 double temp = 0 ;
430                 long nb_freenodes = 0 ;
431                 
432                 /** Computation of the average power of computing nodes **/
433                 for( int i = 0 ; i < gnodesList.size() ; i++ )
434                 {
435                         if( ! gnodesList.get(i).getMapped() )
436                         {
437                                 temp += gnodesList.get(i).getPower() ;
438                                 nb_freenodes++ ;
439                         }
440                 }
441                 
442                 average = temp / nb_freenodes ; //gnodesList.size() ;
443                 
444                 /** Computation of the variance **/
445                 temp = 0 ;
446                 for( int i = 0 ; i < gnodesList.size() ; i++ )
447                 {
448                         if( ! gnodesList.get(i).getMapped() )
449                         {
450                                 temp += Math.pow( ( gnodesList.get(i).getPower() - average ), 2 ) ;
451                         }
452                 }
453                 
454                 /** Computation of the standard deviation **/
455                 temp = temp / nb_freenodes ; //gnodesList.size() ;
456                 std = Math.sqrt( temp ) ;
457                 
458                 /** Computation of the relative standard deviation **/
459                 hd = std / average ;
460                 
461                 /** Correction of the maximum value **/
462                 if( hd > 1 )
463                 {
464                         hd = 1 ;
465                 }
466                 
467                 
468                 return hd ;
469         }
470         
471         
472         /**
473          * Return the amount of nodes available in the cluster containing the
474          * maximum of available nodes.
475          * @return The maximum available nodes of the architecture
476          */
477         public int getMaxClusterNode()
478         {
479                 int max = 0 ;
480                 
481                 for( int i = 0 ; i < clusters.size() ; i++ )
482                 {
483                         if( max < clusters.get( i ).getNbFreeNodes() )
484                         {
485                                 max = clusters.get( i ).getNbFreeNodes() ;
486                         }
487                 }
488                 
489                 return max ;
490         }
491         
492         
493         /**
494          * Initialization of computing nodes in the grid. Set all
495          * of these nodes to be not mapped on, and do the same thing in each
496          * of its clusters.
497          */     
498         public void initGNodes()
499         {
500                 /** Initialization of the local nodes **/
501                 for( int i = 0 ; i < gnodesList.size() ; i++ )
502                 {
503                         gnodesList.get( i ).setMapped( false ) ;
504                 }
505                 
506                 /** Initialization in clusters **/
507                 for( int i = 0 ; i < clusters.size() ; i++ )
508                 {
509                         clusters.get( i ).initGNodes() ;
510                 }
511         }
512         
513         
514         
515         /**
516          * Initialization of a set of computing nodes in the grid. Set all
517          * of these given nodes to be not mapped on, and propagates in each
518          * of its clusters.
519          * @param _ag The nodes' list to initialize
520          */     
521         public void initGNodes( ArrayList<GNode> _ag )
522         {
523                 if( _ag != null && _ag.size() > 0 )
524                 {
525                         /** Initialization of the local and clusters nodes **/
526                         for( int i = 0 ; i < _ag.size() ; i++ )
527                         {
528                                 setMappedStatus( _ag.get( i ), false ) ;
529                                 
530                                 int id = getClusterOfNode( _ag.get( i ) ) ;
531                                 
532                                 if( id != -1 )
533                                 {
534                                         clusters.get(id).setGNodeStatus( _ag.get( i ), false ) ;
535                                 } else {
536                                         System.err.println( "Cluster not found" ) ;
537                                 }
538                         }
539                 }
540         }
541         
542         
543         
544         /**
545          * Print a comprehensible text version of the grid.
546          */
547         public void print()
548         {
549                 System.out.println();
550                 System.out.println( "\t=> Grid composition:\n" ) ; 
551                 for( int i = 0 ; i < clusters.size() ; i++ )
552                 {
553                         System.out.println( "\t\tCluster \""+ clusters.get(i).getName() +"\" : " + clusters.get(i).getNbGNode() ) ;
554                 }
555                 
556                 System.out.println();
557         }
558
559
560         /**
561          * Return the average power of the grid, by computing the average for 
562          * eac cluster.
563          * @return The average power of the grid
564          */
565         public double getAvgFreePower() 
566         {
567                 double ret = 0 ;
568                 int nb = 0 ;
569                 
570                 if( clusters != null && clusters.size() > 0 )
571                 {
572                         for( int i = 0 ; i < clusters.size() ; i++ )
573                         {
574                                 nb++ ;
575                         
576                                 ret += clusters.get( i ).getAvailablePower() / clusters.get( i ).getNbFreeNodes() ;
577                         }
578                 
579                         ret = ret / nb ;
580                         
581                 }
582                 
583                 return ret ;
584         }
585         
586
587         /**
588          * Return the amount of freenodes available in the whole grid.
589          * @return The amount of available nodes
590          */
591         public int getNbFreenodes()
592         {
593                 int ret = 0 ;
594                 
595                 for( int i = 0 ; i < clusters.size() ; i++ )
596                         ret += clusters.get(i).getNbFreeNodes() ;
597                 return ret ;
598         }
599         
600 }
601
602
603 /** La programmation est un art, respectons ceux qui la pratiquent !! **/