Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
New version of MAHEVE plus corrections.
[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          * Search a cluster of the given name, and return it if it exists.
107          * @param _name The name of the cluster
108          * @return The cluster
109          */
110         public Cluster getCluster( String _name )
111         {
112                 for( int i = 0 ; i < clusters.size() ; i++ )
113                 {
114                         if( clusters.get( i ).getName().equalsIgnoreCase( _name ) )
115                         {
116                                 return clusters.get( i ) ;
117                         }
118                 }
119                 
120                 System.err.println( "The cluster \"" + _name + "\" does not exist!" ) ;
121                 
122                 return null ;
123         }
124         
125         
126         /**
127          * Compute and return the distance between two clusters.
128          * @param _g1 First cluster
129          * @param _g2 Second cluster
130          * @return The distance between the two clusters
131          */
132         public double getDistance( GNode _g1, GNode _g2 )
133         {
134                 if( _g1 == null || _g2 == null )
135                 {
136                         return -1 ;
137                 }
138                 
139                 double d = 0 ;
140                 
141                 String cluster1 = "c1", cluster2 = "c2", site1 = "s1", site2 = "s2" ;
142                 
143                 cluster1 = _g1.getClusterName() ;
144                 site1 = _g1.getSiteName() ;
145                 cluster2 = _g2.getClusterName() ;
146                 site2 = _g2.getSiteName() ;
147                 
148                 if( cluster1.equalsIgnoreCase( cluster2 ) )
149                 {
150                         d = 10 ;
151                 } else {
152                         if( site1.equalsIgnoreCase( site2 ) )
153                         {
154                                 d = 15 ;
155                         } else {
156                                 d = 30 ;
157                         }
158                 }
159                 
160                 return d ;
161         }
162         
163         
164         /**
165          * Return the list of computing nodes in the grid.
166          * @return The list of computing nodes
167          */
168         public ArrayList<GNode> getGNodes()
169         {
170                 if( ! gnodesList_done )
171                 {
172                         gnodesList = new ArrayList<GNode>() ;
173                         
174                         for( int i = 0 ; i < clusters.size() ; i++ )
175                         {
176                                 ArrayList<GNode> ar = clusters.get( i ).getGNodes() ;
177                                 
178                                 for( int j = 0 ; j < ar.size() ; j++ )
179                                 {
180                                         gnodesList.add( ar.get( j ) ) ;
181                                 }
182                         }
183                         
184                         gnodesList_done = true ;
185                 }
186                 
187                 return gnodesList ;
188         }
189         
190         
191         /**
192          * Return the list of free computing nodes in the grid.
193          * @return The list of free computing nodes
194          */
195         public ArrayList<GNode> getFreeGNodes()
196         {
197                 ArrayList<GNode> ret = new ArrayList<GNode>() ;
198                         
199                 for( int i = 0 ; i < clusters.size() ; i++ )
200                 {
201                         ArrayList<GNode> ar = clusters.get( i ).getFreeGNodes() ;
202                                 
203                         for( int j = 0 ; j < ar.size() ; j++ )
204                         {
205                                 ret.add( ar.get( j ) ) ;
206                         }
207                 }
208                 
209                 return ret ;
210         }
211         
212         
213         /**
214          * Upgrade the grid with new nodes.
215          * @param _gnodes The list of new nodes
216          */
217         public void updateGrid( ArrayList<GNode> _gnodes )
218         {
219                 if( _gnodes != null && _gnodes.size() != 0 )
220                 {
221                         for( int i = 0 ; i < _gnodes.size() ; i++ )
222                         {
223                                 /** Searching the cluster in which the node should be added **/
224                                 int j = 0 ;
225                                 boolean ok = false ;
226                                 
227                                 for( j = 0 ; j < clusters.size() ; j++ )
228                                 {
229                                         if( _gnodes.get( i ).getClusterName().equalsIgnoreCase( clusters.get( j ).getName() )
230                                                 && _gnodes.get( i ).getSiteName().equalsIgnoreCase( clusters.get( j ).getSite() ) )
231                                         {
232                                                 int pos = clusters.get( j ).isIn( _gnodes.get( i ) ) ;
233                                                 
234                                                 if( pos == -1 )
235                                                 {
236                                                         _gnodes.get( i ).setCluster( clusters.get( j ) ) ;
237                                                         _gnodes.get( i ).setInCluster( true ) ;
238                                                         _gnodes.get( i ).setMapped( false ) ;
239                                                 
240                                                         clusters.get( j ).addGNode( _gnodes.get( i ) ) ;
241                                                         gnodesList.add( _gnodes.get( i ) ) ;
242                                                         
243                                                 } else {
244                                                         clusters.get( j ).setGNodeStatus( _gnodes.get( i ), _gnodes.get( i ).getMapped() ) ;
245                                                 }
246                                                 
247                                                 ok = true ;
248                                                 break ;
249                                         }
250                                 }
251                                 
252                                 /** The cluster was not found, so it is a new one **/
253                                 if( ! ok )
254                                 {
255                                         String site = "", cluster = "" ;
256                                         Cluster nClust = new Cluster() ;
257
258                                         cluster = _gnodes.get( i ).getClusterName() ; // names[ 1 ] ;
259                                         site = _gnodes.get( i ).getSiteName() ; // names[ 2 ] ;
260                                         
261                                         System.out.println( "** (Grid) Creation of cluster " + cluster + " on site " + site ) ;
262                                         
263                                         nClust.setName( cluster ) ;
264                                         nClust.setSite( site ) ;
265                                         
266                                         _gnodes.get( i ).setInCluster( true ) ;
267                                         _gnodes.get( i ).setMapped( false ) ;
268                                         _gnodes.get( i ).setCluster( nClust ) ;
269                                         
270                                         nClust.addGNode( _gnodes.get( i ) ) ;
271                                         
272                                         clusters.add( nClust ) ;
273                                         gnodesList.add( _gnodes.get( i ) ) ;
274                                 }
275                         }
276                         
277                         gnodesList_done = false ;
278                 }
279         }
280         
281         
282         /**
283          * Upgrade the grid with a new node.
284          * @param _g The new node
285          */
286         public void addGNode( GNode _g )
287         {
288                 if( _g != null )
289                 {
290                         /** Searching the cluster in which the node should be added **/
291                         int j = 0 ;
292                         boolean ok = false ;
293                         
294                         for( j = 0 ; j < clusters.size() ; j++ )
295                         {
296                                 if( _g.getClusterName().equalsIgnoreCase( clusters.get( j ).getName() ) 
297                                         && _g.getSiteName().equalsIgnoreCase( clusters.get( j ).getSite() ) )
298                                 {
299                                         int pos = clusters.get( j ).isIn( _g ) ;
300                                                 
301                                         if( pos == -1 )
302                                         {
303                                                 _g.setInCluster( true ) ;
304                                                 _g.setMapped( false ) ;
305                                                 _g.setCluster( clusters.get( j ) ) ;
306                                                 
307                                                 clusters.get( j ).addGNode( _g ) ;
308                                                 gnodesList.add( _g ) ;
309                                                 
310                                         } else {
311                                                 _g.setCluster( clusters.get( j ) ) ;
312                                                 clusters.get( j ).replaceGNode( _g ) ;
313                                         }
314                                                 
315                                         ok = true ;
316                                         break ;
317                                 }
318                         }
319                                 
320                         /** The cluster was not found, so it is a new one **/
321                         if( ! ok )
322                         {
323                                 String site = "", cluster = "" ;
324                                 Cluster nClust = new Cluster() ;
325                                                 
326                                 cluster = _g.getClusterName() ; // names[ 1 ] ;
327                                 site = _g.getSiteName() ; //names[ 2 ] ;
328                                 
329                                 System.out.println( "** (Grid) Creation of cluster " + cluster + " on site " + site ) ;
330                                 
331                                 nClust.setName( cluster ) ;
332                                 nClust.setSite( site ) ;
333                                         
334                                 _g.setInCluster( true ) ;
335                                 _g.setMapped( false ) ;
336                                 _g.setCluster( nClust ) ;
337                                         
338                                 nClust.addGNode( _g ) ;
339                                         
340                                 clusters.add( nClust ) ;
341                                 gnodesList.add( _g ) ;
342                         }
343                 }
344                         
345                 gnodesList_done = false ;
346         }
347         
348         
349         /**
350          * Remove a computing node from the grid.
351          * @param _dead The node to be removed
352          */
353         public void removeGNode( GNode _dead ) 
354         {
355                 if( _dead != null )
356                 {
357                         /** Removing GNode from its cluster **/ 
358                         int id = getClusterOfNode( _dead ) ; 
359                         
360                         if( id != -1 )
361                         {
362                                 clusters.get( id ).removeGNode( _dead ) ;
363                         } else {
364                                 System.err.println( "(Grid) Cluster of dead node not found!" ) ;
365                         }
366
367                         /** Removing the dead node from the global list **/
368                         int i = 0 ;
369                         for( i = 0 ; i < gnodesList.size() ; i++ )
370                         {
371                                 if( _dead.getId() == gnodesList.get( i ).getId() )
372                                 {
373                                         gnodesList.remove( i ) ;
374                                         break ;
375                                 }
376                         }
377
378                 } else {
379                         System.err.println( "(Grid) The GNode to be deleted is null!" ) ;
380                 }
381
382                 gnodesList_done = false ;
383         }
384         
385         
386         /**
387          * Change the mapping status of a node in the grid and in its cluster.
388          * @param _g The node to change the mapping status
389          * @param _status The node's mapping status
390          */
391         public void setMappedStatus( GNode _g, boolean _status )
392         {
393                 if( _g != null )
394                 {
395                         /** Change in the cluster **/
396                         int id = getClusterOfNode( _g ) ;
397                         
398                         if( id != -1 )
399                         {
400                                 clusters.get( id ).setGNodeStatus( _g, _status ) ;
401                         } else {
402                                 System.err.println( "(Grid) Cluster " + _g.getClusterName() + " not found!" ) ;
403                         }
404                         
405                         /** Change in local list **/
406                         for( int i = 0 ; i < gnodesList.size() ; i++ )
407                         {
408                                 if( _g.getId() == gnodesList.get( i ).getId() )
409                                 {
410                                         gnodesList.get( i ).setMapped( _status ) ;
411                                         break ;
412                                 }
413                         }
414                 }
415         }
416         
417         
418         /**
419          * Search and return the cluster containing the specified node.
420          * @param _g A node
421          * @return The cluster containing the node
422          */
423         public int getClusterOfNode( GNode _g )
424         {
425                 int ret = -1 ;
426                 
427                 if( _g != null )
428                 {
429                         for( int i = 0 ; i < clusters.size() ; i++ )
430                         {
431                                 if( _g.getClusterName().equalsIgnoreCase( clusters.get( i ).getName() ) 
432                                   && _g.getSiteName().equalsIgnoreCase( clusters.get( i ).getSite() ) ) 
433                                 {       
434                                         ret = i ;
435                                         break ;
436                                 }
437                         }
438                 }
439                 
440                 return ret ;
441         }
442         
443         
444         /**
445          * Compute the heterogeneity degree of the grid.
446          * This is based on the relative standard deviation.
447          * @return The heterogeneity degree of the grid
448          */
449         public double getHeterogenityDegre()
450         {
451                 if( ! ( gnodesList.size() > 0 ) )
452                 {
453                         System.err.println( "Mapping - Heterogeneity degree computation! " +
454                                         "List's size not corresponding!" ) ;
455                         return -1 ;
456                 }
457                 
458                 double hd = -1 ;
459                 
460                 double average = 0 ;
461                 double std = 0 ;
462                 double temp = 0 ;
463                 long nb_freenodes = 0 ;
464                 
465                 /** Computation of the average power of computing nodes **/
466                 for( int i = 0 ; i < gnodesList.size() ; i++ )
467                 {
468                         if( ! gnodesList.get( i ).getMapped() )
469                         {
470                                 temp += gnodesList.get(i).getPower() ;
471                                 nb_freenodes++ ;
472                         }
473                 }
474                 
475                 average = temp / nb_freenodes ; //gnodesList.size() ;
476                 
477                 /** Computation of the variance **/
478                 temp = 0 ;
479                 for( int i = 0 ; i < gnodesList.size() ; i++ )
480                 {
481                         if( ! gnodesList.get( i ).getMapped() )
482                         {
483                                 temp += Math.pow( ( gnodesList.get(i).getPower() - average ), 2 ) ;
484                         }
485                 }
486                 
487                 /** Computation of the standard deviation **/
488                 temp = temp / nb_freenodes ; //gnodesList.size() ;
489                 std = Math.sqrt( temp ) ;
490                 
491                 /** Computation of the relative standard deviation **/
492                 hd = std / average ;
493                 
494                 /** Correction of the maximum value **/
495                 if( hd > 1 )
496                 {
497                         hd = 1 ;
498                 }
499                 
500                 return hd ;
501         }
502         
503         
504         /**
505          * Return the amount of nodes available in the cluster containing the
506          * maximum of available nodes.
507          * @return The maximum available nodes of the architecture
508          */
509         public int getMaxClusterNode()
510         {
511                 int max = 0 ;
512                 
513                 for( int i = 0 ; i < clusters.size() ; i++ )
514                 {
515                         if( max < clusters.get( i ).getNbFreeNodes() )
516                         {
517                                 max = clusters.get( i ).getNbFreeNodes() ;
518                         }
519                 }
520                 
521                 return max ;
522         }
523         
524         
525         /**
526          * Return the average amount of nodes available in all clusters.
527          * @return The average available nodes of the architecture
528          */
529         public double getAvgClusterNode()
530         {
531                 int nb = 0 ;
532                 
533                 for( int i = 0 ; i < clusters.size() ; i++ )
534                 {
535                         nb += clusters.get( i ).getNbFreeNodes() ;
536                 }
537                 
538                 return ( nb / getNbFreenodes() ) ;
539         }
540         
541         
542         /**
543          * Initialization of computing nodes in the grid. Set all
544          * of these nodes to be not mapped on, and do the same thing in each
545          * of its clusters.
546          */     
547         public void initGNodes()
548         {
549                 /** Initialization of the local nodes **/
550                 for( int i = 0 ; i < gnodesList.size() ; i++ )
551                 {
552                         gnodesList.get( i ).setMapped( false ) ;
553                 }
554                 
555                 /** Initialization in clusters **/
556                 for( int i = 0 ; i < clusters.size() ; i++ )
557                 {
558                         clusters.get( i ).initGNodes() ;
559                         clusters.get( i ).initMoreGNode() ;
560                 }
561         }
562         
563         
564         
565         /**
566          * Initialization of a set of computing nodes in the grid. Set all
567          * of these given nodes to be not mapped on, and propagates in each
568          * of its clusters.
569          * @param _ag The nodes' list to initialize
570          */     
571         public void initGNodes( ArrayList<GNode> _ag )
572         {
573                 if( _ag != null && _ag.size() > 0 )
574                 {
575                         /** Initialization of the local and clusters nodes **/
576                         for( int i = 0 ; i < _ag.size() ; i++ )
577                         {
578                                 setMappedStatus( _ag.get( i ), false ) ;
579                                 
580                                 int id = getClusterOfNode( _ag.get( i ) ) ;
581                                 
582                                 if( id != -1 )
583                                 {
584                                         clusters.get(id).setGNodeStatus( _ag.get( i ), false ) ;
585                                 } else {
586                                         System.err.println( "Cluster not found" ) ;
587                                 }
588                         }
589                 }
590         }
591         
592         
593         
594         /**
595          * Print a comprehensible text version of the grid.
596          */
597         public void print()
598         {
599                 System.out.println();
600                 System.out.println( "\t=> Grid composition:\n" ) ; 
601                 for( int i = 0 ; i < clusters.size() ; i++ )
602                 {
603                         System.out.println( "\t\tCluster \""+ clusters.get(i).getName() +"\" : " + clusters.get(i).getNbGNode() ) ;
604                 }
605                 
606                 System.out.println();
607         }
608
609
610         /**
611          * Return the average power of the grid, by computing the average for 
612          * eac cluster.
613          * @return The average power of the grid
614          */
615         public double getAvgFreePower() 
616         {
617                 double ret = 0 ;
618                 int nb = 0 ;
619                 
620                 if( clusters != null && clusters.size() > 0 )
621                 {
622                         for( int i = 0 ; i < clusters.size() ; i++ )
623                         {
624                                 nb++ ;
625                         
626                                 ret += clusters.get( i ).getAvailablePower() / clusters.get( i ).getNbFreeNodes() ;
627                         }
628                 
629                         ret = ret / nb ;
630                         
631                 }
632                 
633                 return ret ;
634         }
635         
636
637         /**
638          * Return the amount of freenodes available in the whole grid.
639          * @return The amount of available nodes
640          */
641         public int getNbFreenodes()
642         {
643                 int ret = 0 ;
644                 
645                 for( int i = 0 ; i < clusters.size() ; i++ )
646                         ret += clusters.get(i).getNbFreeNodes() ;
647                 return ret ;
648         }
649         
650         
651         /**
652          * Return the max distance it could exist between two computing nodes.
653          * @return The max distance
654          */
655         public double getMaxDistance()
656         {
657                 // TODO
658                 return 30 ;
659         }
660         
661 }
662
663
664 /** La programmation est un art, respectons ceux qui la pratiquent !! **/