Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
New version of MAHEVE plus corrections.
[mapping.git] / src / and / Mapping / Grid.java
index 0370940..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 ;
@@ -49,8 +44,6 @@ public class Grid implements Serializable
                        {
                                gnodesList.add( c.getGNodes().get( i ) ) ;
                        }
-//                     nb_cluster++ ;
-//                     nb_node += c.getNbGNode() ;
                }
        }
        
@@ -68,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++ )
                                {
@@ -88,7 +79,6 @@ public class Grid implements Serializable
        public int getNbCluster()
        {
                return clusters.size() ;
-//             return nb_cluster ;
        }
        
        
@@ -99,7 +89,6 @@ public class Grid implements Serializable
        public int getNbGNode()
        {
                return gnodesList.size() ;
-//             return nb_node ;
        }
        
        
@@ -113,16 +102,25 @@ public class Grid implements Serializable
        }
        
        
-//     /**
-//      * Initialization of clusters.
-//      */
-//     public void initClusters()
-//     {
-//             for( int i = 0 ; i < nb_cluster ; i++ )
-//             {
-//                     clusters.get( i ).initIndice() ;
-//             }
-//     }
+       /**
+        * Search a cluster of the given name, and return it if it exists.
+        * @param _name The name of the cluster
+        * @return The cluster
+        */
+       public Cluster getCluster( String _name )
+       {
+               for( int i = 0 ; i < clusters.size() ; i++ )
+               {
+                       if( clusters.get( i ).getName().equalsIgnoreCase( _name ) )
+                       {
+                               return clusters.get( i ) ;
+                       }
+               }
+               
+               System.err.println( "The cluster \"" + _name + "\" does not exist!" ) ;
+               
+               return null ;
+       }
        
        
        /**
@@ -133,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 ) != -1 )
-                       {
-                               cluster1 = clusters.get( i ).getName() ;
-                               site1 = clusters.get( i ).getSite() ;
-                       }
-                       
-                       if( clusters.get( i ).isIn( _g2 ) != -1 )
-                       {
-                               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 ;
                        }
                }
                
@@ -202,6 +188,28 @@ public class Grid implements Serializable
        }
        
        
+       /**
+        * 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
@@ -214,56 +222,54 @@ public class Grid implements Serializable
                        {
                                /** Searching the cluster in which the node should be added **/
                                int j = 0 ;
-                               for( j = 0; j < clusters.size(); j++ )
+                               boolean ok = false ;
+                               
+                               for( j = 0 ; j < clusters.size() ; j++ )
                                {
-                                       if( _gnodes.get( i ).getCluster().equalsIgnoreCase( clusters.get( j ).getName() ) )
+                                       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 ).getName() ) ;
-                                                       _gnodes.get( i ).setSite( clusters.get( j ).getSite() ) ;
+                                                       _gnodes.get( i ).setCluster( clusters.get( j ) ) ;
                                                        _gnodes.get( i ).setInCluster( true ) ;
                                                        _gnodes.get( i ).setMapped( false ) ;
                                                
-                                                       clusters.get( i ).addGNode( _gnodes.get( j ) ) ;
-//                                                     nb_node++ ;
+                                                       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( j == clusters.size() )
+                               if( ! ok )
                                {
                                        String site = "", cluster = "" ;
                                        Cluster nClust = new Cluster() ;
+
+                                       cluster = _gnodes.get( i ).getClusterName() ; // names[ 1 ] ;
+                                       site = _gnodes.get( i ).getSiteName() ; // names[ 2 ] ;
                                        
-                                       
-                                       String names[] = Utils.decodeG5Knames( _gnodes.get( i ).getName() ) ;
-                                       
-                                       cluster = names[ 1 ] ;
-                                       site = 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 ).setSite( site ) ;
-                                       _gnodes.get( i ).setCluster( cluster ) ;
+                                       _gnodes.get( i ).setCluster( nClust ) ;
                                        
                                        nClust.addGNode( _gnodes.get( i ) ) ;
-//                                     nb_cluster++ ;
                                        
                                        clusters.add( nClust ) ;
-//                                     nb_node++ ;
                                        gnodesList.add( _gnodes.get( i ) ) ;
                                }
                        }
@@ -274,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 ) ;
+                       }
+               }
                        
+               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 **/
-                       for( int i = 0 ; i < gnodesList.size() ; i++ )
+                       int i = 0 ;
+                       for( i = 0 ; i < gnodesList.size() ; i++ )
                        {
                                if( _dead.getId() == gnodesList.get( i ).getId() )
                                {
@@ -309,9 +374,11 @@ public class Grid implements Serializable
                                        break ;
                                }
                        }
+
+               } else {
+                       System.err.println( "(Grid) The GNode to be deleted is null!" ) ;
                }
-               
-//             nb_node-- ;
+
                gnodesList_done = false ;
        }
        
@@ -325,8 +392,17 @@ public class Grid implements Serializable
        {
                if( _g != null )
                {
-                       getClusterOfNode( _g ).setGNodeStatus( _g, _status ) ;
+                       /** 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() )
@@ -344,22 +420,27 @@ public class Grid implements Serializable
         * @param _g A node
         * @return The cluster containing the node
         */
-       public Cluster getClusterOfNode( GNode _g )
+       public int getClusterOfNode( GNode _g )
        {
+               int ret = -1 ;
+               
                if( _g != null )
                {
                        for( int i = 0 ; i < clusters.size() ; i++ )
                        {
-                               if( _g.getCluster().equalsIgnoreCase( clusters.get( i ).getName() ) ) 
-                               {
-                                       return clusters.get( i ) ;
+                               if( _g.getClusterName().equalsIgnoreCase( clusters.get( i ).getName() ) 
+                                 && _g.getSiteName().equalsIgnoreCase( clusters.get( i ).getSite() ) ) 
+                               {       
+                                       ret = i ;
+                                       break ;
                                }
                        }
                }
                
-               return null ;
+               return ret ;
        }
        
+       
        /**
         * Compute the heterogeneity degree of the grid.
         * This is based on the relative standard deviation.
@@ -379,36 +460,137 @@ 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 < gnodesList.size() ; i++ )
                {
-                       temp += gnodesList.get(i).getPower() ;
+                       if( ! gnodesList.get( i ).getMapped() )
+                       {
+                               temp += gnodesList.get(i).getPower() ;
+                               nb_freenodes++ ;
+                       }
                }
                
-               average = temp / gnodesList.size() ;
+               average = temp / nb_freenodes ; //gnodesList.size() ;
                
                /** Computation of the variance **/
                temp = 0 ;
                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 / gnodesList.size() ;
+               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.
         */
@@ -423,6 +605,58 @@ public class Grid implements Serializable
                
                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 ;
+       }
        
 }