Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Adding new functionalities.
[mapping.git] / src / and / Mapping / Grid.java
index 3501a2d..2451b95 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,18 +102,6 @@ public class Grid implements Serializable
        }
        
        
-//     /**
-//      * Initialization of clusters.
-//      */
-//     public void initClusters()
-//     {
-//             for( int i = 0 ; i < nb_cluster ; i++ )
-//             {
-//                     clusters.get( i ).initIndice() ;
-//             }
-//     }
-       
-       
        /**
         * Compute and return the distance between two clusters.
         * @param _g1 First cluster
@@ -157,17 +134,15 @@ public class Grid implements Serializable
                        }
                }
                
-               //
-               
                if( cluster1.compareTo( cluster2 ) == 0 )
                {
-                       d = 1 ;
+                       d = 10 ;
                } else {
                        if( site1.compareTo( site2 ) == 0 )
                        {
-                               d = 2 ;
+                               d = 20 ;
                        } else {
-                               d = 3 ;
+                               d = 30 ;
                        }
                }
                
@@ -227,8 +202,7 @@ public class Grid implements Serializable
                                                        _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 {
@@ -260,10 +234,8 @@ public class Grid implements Serializable
                                        _gnodes.get( i ).setCluster( cluster ) ;
                                        
                                        nClust.addGNode( _gnodes.get( i ) ) ;
-//                                     nb_cluster++ ;
                                        
                                        clusters.add( nClust ) ;
-//                                     nb_node++ ;
                                        gnodesList.add( _gnodes.get( i ) ) ;
                                }
                        }
@@ -274,34 +246,92 @@ 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() )
+                       /** Searching the cluster in which the node should be added **/
+                       int j = 0 ;
+                       for( j = 0; j < clusters.size(); j++ )
                        {
-                               String site = "", cluster = "" ;
-                       
-                               site = _dead.getSite() ;
-                               cluster = _dead.getCluster() ;
-                       
-                               /** Removing GNode from its cluster **/                 
-                               for( int j = 0 ; j < clusters.size() ; j++ )
+                               if( _g.getCluster().equalsIgnoreCase( clusters.get( j ).getName() ) )
                                {
-                                       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.setSite( clusters.get( j ).getSite() ) ;
+                                               _g.setInCluster( true ) ;
+                                               _g.setMapped( false ) ;
+                                               
+                                               clusters.get( j ).addGNode( _g ) ;
+                                               gnodesList.add( _g ) ;
+                                                       
+                                       } else {
+                                               clusters.get( j ).removeGNode( _g ) ;
+                                               clusters.get( j ).addGNode( _g ) ;
                                        }
+                                               
+                                       break ;
                                }
                        }
+                               
+                       /** The cluster was not found, so it is a new one **/
+                       if( j == clusters.size() )
+                       {
+                               String site = "", cluster = "" ;
+                               Cluster nClust = new Cluster() ;
+                                               
+                               String names[] = Utils.decodeG5Knames( _g.getName() ) ;
+                                       
+                               cluster = names[ 1 ] ;
+                               site = names[ 2 ] ;
+                               
+                               System.out.println("** (Grid) Creation of cluster: "+cluster);
+                               
+                               nClust.setName( cluster ) ;
+                               nClust.setSite( site ) ;
+                                       
+                               _g.setInCluster( true ) ;
+                               _g.setMapped( false ) ;
+                               _g.setSite( site ) ;
+                               _g.setCluster( cluster ) ;
+                                       
+                               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 +339,11 @@ public class Grid implements Serializable
                                        break ;
                                }
                        }
+
+               } else {
+                       System.err.println( "(Grid) The GNode to be deleted is null!" ) ;
                }
-               
-//             nb_node-- ;
+
                gnodesList_done = false ;
        }
        
@@ -326,7 +358,14 @@ public class Grid implements Serializable
                if( _g != null )
                {
                        /** Change in the cluster **/
-                       getClusterOfNode( _g ).setGNodeStatus( _g, _status ) ;
+                       int id = getClusterOfNode( _g ) ;
+                       
+                       if( id != -1 )
+                       {
+                               clusters.get(id).setGNodeStatus( _g, _status ) ;
+                       } else {
+                               System.err.println( "(Grid) Cluster "+_g.getCluster()+" not found!" ) ;
+                       }
                        
                        /** Change in local list **/
                        for( int i = 0 ; i < gnodesList.size() ; i++ )
@@ -346,22 +385,29 @@ 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.getSite().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.
@@ -381,33 +427,69 @@ 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 **/
-               hd = 100 * std / average ;
+               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 ;
+       }
+       
+       
        /**
         * Initialization of computing nodes in the grid. Set all
         * of these nodes to be not mapped on, and do the same thing in each
@@ -445,7 +527,14 @@ public class Grid implements Serializable
                        {
                                setMappedStatus( _ag.get( i ), false ) ;
                                
-                               getClusterOfNode( _ag.get( i ) ).setGNodeStatus( _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" ) ;
+                               }
                        }
                }
        }
@@ -466,6 +555,47 @@ 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 ;
+       }
        
 }