Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
New version of MAHEVE plus corrections.
[mapping.git] / src / and / Mapping / Grid.java
index 16e46b1..902e40f 100644 (file)
@@ -14,9 +14,6 @@ public class Grid implements Serializable
 {
        private static final long serialVersionUID = 1L;
 
-       
-       private int nb_cluster ;
-       private int nb_node ;
        private ArrayList<Cluster> clusters ;
        private ArrayList<GNode> gnodesList;
        private boolean gnodesList_done;
@@ -27,8 +24,6 @@ public class Grid implements Serializable
         */
        public Grid()
        {
-               nb_cluster = 0 ;
-               nb_node = 0 ;
                clusters = new ArrayList<Cluster>() ;
                gnodesList = new ArrayList<GNode>() ;
                gnodesList_done = false ;
@@ -44,8 +39,11 @@ public class Grid implements Serializable
                if( c != null )
                {
                        clusters.add( c ) ;
-                       nb_cluster ++ ;
-                       nb_node += c.getNbGNode() ;
+                       
+                       for( int i = 0 ; i < c.getNbGNode() ; i++ )
+                       {
+                               gnodesList.add( c.getGNodes().get( i ) ) ;
+                       }
                }
        }
        
@@ -63,9 +61,7 @@ public class Grid implements Serializable
                        for( int i = 0 ; i < al.size() ; i++ )
                        {
                                clusters.add( al.get( i ) ) ;
-                               nb_cluster ++ ;
                                nbCLusterNodes = al.get( i ).getNbGNode() ;
-                               nb_node += nbCLusterNodes ;
                                
                                for( int j = 0 ; j < nbCLusterNodes ; j++ )
                                {
@@ -82,7 +78,7 @@ public class Grid implements Serializable
         */
        public int getNbCluster()
        {
-               return nb_cluster ;
+               return clusters.size() ;
        }
        
        
@@ -92,7 +88,7 @@ public class Grid implements Serializable
         */
        public int getNbGNode()
        {
-               return nb_node ;
+               return gnodesList.size() ;
        }
        
        
@@ -107,14 +103,23 @@ public class Grid implements Serializable
        
        
        /**
-        * Initialization of clusters.
+        * Search a cluster of the given name, and return it if it exists.
+        * @param _name The name of the cluster
+        * @return The cluster
         */
-       public void initClusters()
+       public Cluster getCluster( String _name )
        {
-               for( int i = 0 ; i < nb_cluster ; i++ )
+               for( int i = 0 ; i < clusters.size() ; i++ )
                {
-                       clusters.get( i ).initIndice() ;
+                       if( clusters.get( i ).getName().equalsIgnoreCase( _name ) )
+                       {
+                               return clusters.get( i ) ;
+                       }
                }
+               
+               System.err.println( "The cluster \"" + _name + "\" does not exist!" ) ;
+               
+               return null ;
        }
        
        
@@ -126,41 +131,29 @@ public class Grid implements Serializable
         */
        public double getDistance( GNode _g1, GNode _g2 )
        {
-               double d = 0 ;
-               
-               if( _g1.equals( _g2 ) ) 
+               if( _g1 == null || _g2 == null )
                {
-                       return d ;
+                       return -1 ;
                }
                
-               String cluster1 = "c1", cluster2 = "c2", site1 = "s1", site2 = "s2" ;
+               double d = 0 ;
                
-               for( int i = 0 ; i < clusters.size() ; i++ )
-               {
-                       if( clusters.get( i ).isIn( _g1) )
-                       {
-                               cluster1 = clusters.get( i ).getName() ;
-                               site1 = clusters.get( i ).getSite() ;
-                       }
-                       
-                       if( clusters.get( i ).isIn( _g2) )
-                       {
-                               cluster2 = clusters.get( i ).getName() ;
-                               site2 = clusters.get( i ).getSite() ;
-                       }
-               }
+               String cluster1 = "c1", cluster2 = "c2", site1 = "s1", site2 = "s2" ;
                
-               //
+               cluster1 = _g1.getClusterName() ;
+               site1 = _g1.getSiteName() ;
+               cluster2 = _g2.getClusterName() ;
+               site2 = _g2.getSiteName() ;
                
-               if( cluster1.compareTo( cluster2 ) == 0 )
+               if( cluster1.equalsIgnoreCase( cluster2 ) )
                {
-                       d = 1 ;
+                       d = 10 ;
                } else {
-                       if( site1.compareTo( site2 ) == 0 )
+                       if( site1.equalsIgnoreCase( site2 ) )
                        {
-                               d = 2 ;
+                               d = 15 ;
                        } else {
-                               d = 3 ;
+                               d = 30 ;
                        }
                }
                
@@ -196,8 +189,30 @@ public class Grid implements Serializable
        
        
        /**
-        * Plop !!
-        * @param _gnodes
+        * Return the list of free computing nodes in the grid.
+        * @return The list of free computing nodes
+        */
+       public ArrayList<GNode> getFreeGNodes()
+       {
+               ArrayList<GNode> ret = new ArrayList<GNode>() ;
+                       
+               for( int i = 0 ; i < clusters.size() ; i++ )
+               {
+                       ArrayList<GNode> ar = clusters.get( i ).getFreeGNodes() ;
+                               
+                       for( int j = 0 ; j < ar.size() ; j++ )
+                       {
+                               ret.add( ar.get( j ) ) ;
+                       }
+               }
+               
+               return ret ;
+       }
+       
+       
+       /**
+        * Upgrade the grid with new nodes.
+        * @param _gnodes The list of new nodes
         */
        public void updateGrid( ArrayList<GNode> _gnodes )
        {
@@ -205,7 +220,58 @@ public class Grid implements Serializable
                {
                        for( int i = 0 ; i < _gnodes.size() ; i++ )
                        {
+                               /** Searching the cluster in which the node should be added **/
+                               int j = 0 ;
+                               boolean ok = false ;
                                
+                               for( j = 0 ; j < clusters.size() ; j++ )
+                               {
+                                       if( _gnodes.get( i ).getClusterName().equalsIgnoreCase( clusters.get( j ).getName() )
+                                               && _gnodes.get( i ).getSiteName().equalsIgnoreCase( clusters.get( j ).getSite() ) )
+                                       {
+                                               int pos = clusters.get( j ).isIn( _gnodes.get( i ) ) ;
+                                               
+                                               if( pos == -1 )
+                                               {
+                                                       _gnodes.get( i ).setCluster( clusters.get( j ) ) ;
+                                                       _gnodes.get( i ).setInCluster( true ) ;
+                                                       _gnodes.get( i ).setMapped( false ) ;
+                                               
+                                                       clusters.get( j ).addGNode( _gnodes.get( i ) ) ;
+                                                       gnodesList.add( _gnodes.get( i ) ) ;
+                                                       
+                                               } else {
+                                                       clusters.get( j ).setGNodeStatus( _gnodes.get( i ), _gnodes.get( i ).getMapped() ) ;
+                                               }
+                                               
+                                               ok = true ;
+                                               break ;
+                                       }
+                               }
+                               
+                               /** The cluster was not found, so it is a new one **/
+                               if( ! ok )
+                               {
+                                       String site = "", cluster = "" ;
+                                       Cluster nClust = new Cluster() ;
+
+                                       cluster = _gnodes.get( i ).getClusterName() ; // names[ 1 ] ;
+                                       site = _gnodes.get( i ).getSiteName() ; // names[ 2 ] ;
+                                       
+                                       System.out.println( "** (Grid) Creation of cluster " + cluster + " on site " + site ) ;
+                                       
+                                       nClust.setName( cluster ) ;
+                                       nClust.setSite( site ) ;
+                                       
+                                       _gnodes.get( i ).setInCluster( true ) ;
+                                       _gnodes.get( i ).setMapped( false ) ;
+                                       _gnodes.get( i ).setCluster( nClust ) ;
+                                       
+                                       nClust.addGNode( _gnodes.get( i ) ) ;
+                                       
+                                       clusters.add( nClust ) ;
+                                       gnodesList.add( _gnodes.get( i ) ) ;
+                               }
                        }
                        
                        gnodesList_done = false ;
@@ -214,34 +280,93 @@ public class Grid implements Serializable
        
        
        /**
-        * Remove a computing node from the grid.
-        * @param _dead The node to be removed
+        * Upgrade the grid with a new node.
+        * @param _g The new node
         */
-       public void removeGNode( GNode _dead ) 
+       public void addGNode( GNode _g )
        {
-               if( _dead != null )
+               if( _g != null )
                {
-                       if( _dead.getMapped() )
-                       {
-                               String site = "", cluster = "" ;
-                       
-                               site = _dead.getSite() ;
-                               cluster = _dead.getCluster() ;
+                       /** Searching the cluster in which the node should be added **/
+                       int j = 0 ;
+                       boolean ok = false ;
                        
-                               /* Removing GNode from its cluster */                   
-                               for( int j = 0 ; j < clusters.size() ; j++ )
+                       for( j = 0 ; j < clusters.size() ; j++ )
+                       {
+                               if( _g.getClusterName().equalsIgnoreCase( clusters.get( j ).getName() ) 
+                                       && _g.getSiteName().equalsIgnoreCase( clusters.get( j ).getSite() ) )
                                {
-                                       if( clusters.get( j ).getName().equals( cluster ) && clusters.get( j ).getSite().equals( site ))
+                                       int pos = clusters.get( j ).isIn( _g ) ;
+                                               
+                                       if( pos == -1 )
                                        {
-                                               clusters.get( j ).removeGNode( _dead ) ;
-                                       
-                                               break ;
+                                               _g.setInCluster( true ) ;
+                                               _g.setMapped( false ) ;
+                                               _g.setCluster( clusters.get( j ) ) ;
+                                               
+                                               clusters.get( j ).addGNode( _g ) ;
+                                               gnodesList.add( _g ) ;
+                                               
+                                       } else {
+                                               _g.setCluster( clusters.get( j ) ) ;
+                                               clusters.get( j ).replaceGNode( _g ) ;
                                        }
+                                               
+                                       ok = true ;
+                                       break ;
                                }
                        }
+                               
+                       /** The cluster was not found, so it is a new one **/
+                       if( ! ok )
+                       {
+                               String site = "", cluster = "" ;
+                               Cluster nClust = new Cluster() ;
+                                               
+                               cluster = _g.getClusterName() ; // names[ 1 ] ;
+                               site = _g.getSiteName() ; //names[ 2 ] ;
+                               
+                               System.out.println( "** (Grid) Creation of cluster " + cluster + " on site " + site ) ;
+                               
+                               nClust.setName( cluster ) ;
+                               nClust.setSite( site ) ;
+                                       
+                               _g.setInCluster( true ) ;
+                               _g.setMapped( false ) ;
+                               _g.setCluster( nClust ) ;
+                                       
+                               nClust.addGNode( _g ) ;
+                                       
+                               clusters.add( nClust ) ;
+                               gnodesList.add( _g ) ;
+                       }
+               }
                        
-                       /* Removing the dead node from the global list */
-                       for( int i = 0 ; i < nb_node ; i++ )
+               gnodesList_done = false ;
+       }
+       
+       
+       /**
+        * Remove a computing node from the grid.
+        * @param _dead The node to be removed
+        */
+       public void removeGNode( GNode _dead ) 
+       {
+               if( _dead != null )
+               {
+                       /** Removing GNode from its cluster **/ 
+                       int id = getClusterOfNode( _dead ) ; 
+                       
+                       if( id != -1 )
+                       {
+                               clusters.get( id ).removeGNode( _dead ) ;
+                       } else {
+                               System.err.println( "(Grid) Cluster of dead node not found!" ) ;
+                       }
+
+                       /** Removing the dead node from the global list **/
+                       int i = 0 ;
+                       for( i = 0 ; i < gnodesList.size() ; i++ )
                        {
                                if( _dead.getId() == gnodesList.get( i ).getId() )
                                {
@@ -249,13 +374,73 @@ public class Grid implements Serializable
                                        break ;
                                }
                        }
+
+               } else {
+                       System.err.println( "(Grid) The GNode to be deleted is null!" ) ;
                }
-               
-               nb_node-- ;
+
                gnodesList_done = false ;
        }
        
        
+       /**
+        * Change the mapping status of a node in the grid and in its cluster.
+        * @param _g The node to change the mapping status
+        * @param _status The node's mapping status
+        */
+       public void setMappedStatus( GNode _g, boolean _status )
+       {
+               if( _g != null )
+               {
+                       /** Change in the cluster **/
+                       int id = getClusterOfNode( _g ) ;
+                       
+                       if( id != -1 )
+                       {
+                               clusters.get( id ).setGNodeStatus( _g, _status ) ;
+                       } else {
+                               System.err.println( "(Grid) Cluster " + _g.getClusterName() + " not found!" ) ;
+                       }
+                       
+                       /** Change in local list **/
+                       for( int i = 0 ; i < gnodesList.size() ; i++ )
+                       {
+                               if( _g.getId() == gnodesList.get( i ).getId() )
+                               {
+                                       gnodesList.get( i ).setMapped( _status ) ;
+                                       break ;
+                               }
+                       }
+               }
+       }
+       
+       
+       /**
+        * Search and return the cluster containing the specified node.
+        * @param _g A node
+        * @return The cluster containing the node
+        */
+       public int getClusterOfNode( GNode _g )
+       {
+               int ret = -1 ;
+               
+               if( _g != null )
+               {
+                       for( int i = 0 ; i < clusters.size() ; i++ )
+                       {
+                               if( _g.getClusterName().equalsIgnoreCase( clusters.get( i ).getName() ) 
+                                 && _g.getSiteName().equalsIgnoreCase( clusters.get( i ).getSite() ) ) 
+                               {       
+                                       ret = i ;
+                                       break ;
+                               }
+                       }
+               }
+               
+               return ret ;
+       }
+       
+       
        /**
         * Compute the heterogeneity degree of the grid.
         * This is based on the relative standard deviation.
@@ -263,7 +448,7 @@ public class Grid implements Serializable
         */
        public double getHeterogenityDegre()
        {
-               if( nb_node != gnodesList.size() )
+               if( ! ( gnodesList.size() > 0 ) )
                {
                        System.err.println( "Mapping - Heterogeneity degree computation! " +
                                        "List's size not corresponding!" ) ;
@@ -275,50 +460,203 @@ public class Grid implements Serializable
                double average = 0 ;
                double std = 0 ;
                double temp = 0 ;
+               long nb_freenodes = 0 ;
                
                /** Computation of the average power of computing nodes **/
-               for( int i = 0 ; i < nb_node ; i++ )
+               for( int i = 0 ; i < gnodesList.size() ; i++ )
                {
-                       temp += gnodesList.get(i).getPower() ;
+                       if( ! gnodesList.get( i ).getMapped() )
+                       {
+                               temp += gnodesList.get(i).getPower() ;
+                               nb_freenodes++ ;
+                       }
                }
                
-               average = temp / nb_node ;
+               average = temp / nb_freenodes ; //gnodesList.size() ;
                
                /** Computation of the variance **/
                temp = 0 ;
-               for( int i = 0 ; i < nb_node ; i++ )
+               for( int i = 0 ; i < gnodesList.size() ; i++ )
                {
-                       temp += Math.pow( ( gnodesList.get(i).getPower() - average ), 2 ) ;
+                       if( ! gnodesList.get( i ).getMapped() )
+                       {
+                               temp += Math.pow( ( gnodesList.get(i).getPower() - average ), 2 ) ;
+                       }
                }
                
                /** Computation of the standard deviation **/
-               temp = temp / nb_node ;
+               temp = temp / nb_freenodes ; //gnodesList.size() ;
                std = Math.sqrt( temp ) ;
                
-               /** Computation of the relative standard deviation
-                * plus modifications 
-                */
-               hd = 100 * std / average / 10 ;
+               /** Computation of the relative standard deviation **/
+               hd = std / average ;
                
+               /** Correction of the maximum value **/
+               if( hd > 1 )
+               {
+                       hd = 1 ;
+               }
                
                return hd ;
        }
        
        
+       /**
+        * Return the amount of nodes available in the cluster containing the
+        * maximum of available nodes.
+        * @return The maximum available nodes of the architecture
+        */
+       public int getMaxClusterNode()
+       {
+               int max = 0 ;
+               
+               for( int i = 0 ; i < clusters.size() ; i++ )
+               {
+                       if( max < clusters.get( i ).getNbFreeNodes() )
+                       {
+                               max = clusters.get( i ).getNbFreeNodes() ;
+                       }
+               }
+               
+               return max ;
+       }
+       
+       
+       /**
+        * Return the average amount of nodes available in all clusters.
+        * @return The average available nodes of the architecture
+        */
+       public double getAvgClusterNode()
+       {
+               int nb = 0 ;
+               
+               for( int i = 0 ; i < clusters.size() ; i++ )
+               {
+                       nb += clusters.get( i ).getNbFreeNodes() ;
+               }
+               
+               return ( nb / getNbFreenodes() ) ;
+       }
+       
+       
+       /**
+        * Initialization of computing nodes in the grid. Set all
+        * of these nodes to be not mapped on, and do the same thing in each
+        * of its clusters.
+        */     
+       public void initGNodes()
+       {
+               /** Initialization of the local nodes **/
+               for( int i = 0 ; i < gnodesList.size() ; i++ )
+               {
+                       gnodesList.get( i ).setMapped( false ) ;
+               }
+               
+               /** Initialization in clusters **/
+               for( int i = 0 ; i < clusters.size() ; i++ )
+               {
+                       clusters.get( i ).initGNodes() ;
+                       clusters.get( i ).initMoreGNode() ;
+               }
+       }
+       
+       
+       
+       /**
+        * Initialization of a set of computing nodes in the grid. Set all
+        * of these given nodes to be not mapped on, and propagates in each
+        * of its clusters.
+        * @param _ag The nodes' list to initialize
+        */     
+       public void initGNodes( ArrayList<GNode> _ag )
+       {
+               if( _ag != null && _ag.size() > 0 )
+               {
+                       /** Initialization of the local and clusters nodes **/
+                       for( int i = 0 ; i < _ag.size() ; i++ )
+                       {
+                               setMappedStatus( _ag.get( i ), false ) ;
+                               
+                               int id = getClusterOfNode( _ag.get( i ) ) ;
+                               
+                               if( id != -1 )
+                               {
+                                       clusters.get(id).setGNodeStatus( _ag.get( i ), false ) ;
+                               } else {
+                                       System.err.println( "Cluster not found" ) ;
+                               }
+                       }
+               }
+       }
+       
+       
+       
        /**
         * Print a comprehensible text version of the grid.
         */
        public void print()
        {
                System.out.println();
-               System.out.println( "\t=> Grid composition :\n" ) ; 
-               for( int i = 0 ; i < nb_cluster ; i++ )
+               System.out.println( "\t=> Grid composition:\n" ) ; 
+               for( int i = 0 ; i < clusters.size() ; i++ )
                {
                        System.out.println( "\t\tCluster \""+ clusters.get(i).getName() +"\" : " + clusters.get(i).getNbGNode() ) ;
                }
                
                System.out.println();
        }
+
+
+       /**
+        * Return the average power of the grid, by computing the average for 
+        * eac cluster.
+        * @return The average power of the grid
+        */
+       public double getAvgFreePower() 
+       {
+               double ret = 0 ;
+               int nb = 0 ;
+               
+               if( clusters != null && clusters.size() > 0 )
+               {
+                       for( int i = 0 ; i < clusters.size() ; i++ )
+                       {
+                               nb++ ;
+                       
+                               ret += clusters.get( i ).getAvailablePower() / clusters.get( i ).getNbFreeNodes() ;
+                       }
+               
+                       ret = ret / nb ;
+                       
+               }
+               
+               return ret ;
+       }
+       
+
+       /**
+        * Return the amount of freenodes available in the whole grid.
+        * @return The amount of available nodes
+        */
+       public int getNbFreenodes()
+       {
+               int ret = 0 ;
+               
+               for( int i = 0 ; i < clusters.size() ; i++ )
+                       ret += clusters.get(i).getNbFreeNodes() ;
+               return ret ;
+       }
+       
+       
+       /**
+        * Return the max distance it could exist between two computing nodes.
+        * @return The max distance
+        */
+       public double getMaxDistance()
+       {
+               // TODO
+               return 30 ;
+       }
        
 }