Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Adding some new functions.
[mapping.git] / src / and / Mapping / Grid.java
index b9a6efa..877ac33 100644 (file)
@@ -15,8 +15,8 @@ public class Grid implements Serializable
        private static final long serialVersionUID = 1L;
 
        
-       private int nb_cluster ;
-       private int nb_node ;
+//     private int nb_cluster ;
+//     private int nb_node ;
        private ArrayList<Cluster> clusters ;
        private ArrayList<GNode> gnodesList;
        private boolean gnodesList_done;
@@ -27,10 +27,10 @@ public class Grid implements Serializable
         */
        public Grid()
        {
-               nb_cluster = 0 ;
-               nb_node = 0 ;
+//             nb_cluster = 0 ;
+//             nb_node = 0 ;
                clusters = new ArrayList<Cluster>() ;
-               gnodesList = null ;
+               gnodesList = new ArrayList<GNode>() ;
                gnodesList_done = false ;
        }
        
@@ -44,8 +44,13 @@ 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 ) ) ;
+                       }
+//                     nb_cluster++ ;
+//                     nb_node += c.getNbGNode() ;
                }
        }
        
@@ -58,11 +63,19 @@ public class Grid implements Serializable
        {
                if( al != null )
                {
+                       int nbCLusterNodes = 0 ;
+                       
                        for( int i = 0 ; i < al.size() ; i++ )
                        {
                                clusters.add( al.get( i ) ) ;
-                               nb_cluster ++ ;
-                               nb_node += al.get( i ).getNbGNode() ;
+//                             nb_cluster++ ;
+                               nbCLusterNodes = al.get( i ).getNbGNode() ;
+//                             nb_node += nbCLusterNodes ;
+                               
+                               for( int j = 0 ; j < nbCLusterNodes ; j++ )
+                               {
+                                       gnodesList.add( al.get( i ).getGNodes().get( j ) ) ;
+                               }
                        }
                }
        }
@@ -74,7 +87,8 @@ public class Grid implements Serializable
         */
        public int getNbCluster()
        {
-               return nb_cluster ;
+               return clusters.size() ;
+//             return nb_cluster ;
        }
        
        
@@ -84,7 +98,8 @@ public class Grid implements Serializable
         */
        public int getNbGNode()
        {
-               return nb_node ;
+               return gnodesList.size() ;
+//             return nb_node ;
        }
        
        
@@ -98,16 +113,16 @@ public class Grid implements Serializable
        }
        
        
-       /**
-        * Initialization of clusters.
-        */
-       public void initClusters()
-       {
-               for( int i = 0 ; i < nb_cluster ; i++ )
-               {
-                       clusters.get( i ).initIndice() ;
-               }
-       }
+//     /**
+//      * Initialization of clusters.
+//      */
+//     public void initClusters()
+//     {
+//             for( int i = 0 ; i < nb_cluster ; i++ )
+//             {
+//                     clusters.get( i ).initIndice() ;
+//             }
+//     }
        
        
        /**
@@ -129,13 +144,13 @@ public class Grid implements Serializable
                
                for( int i = 0 ; i < clusters.size() ; i++ )
                {
-                       if( clusters.get( i ).isIn( _g1) )
+                       if( clusters.get( i ).isIn( _g1 ) != -1 )
                        {
                                cluster1 = clusters.get( i ).getName() ;
                                site1 = clusters.get( i ).getSite() ;
                        }
                        
-                       if( clusters.get( i ).isIn( _g2) )
+                       if( clusters.get( i ).isIn( _g2 ) != -1 )
                        {
                                cluster2 = clusters.get( i ).getName() ;
                                site2 = clusters.get( i ).getSite() ;
@@ -188,8 +203,8 @@ public class Grid implements Serializable
        
        
        /**
-        * Plop !!
-        * @param _gnodes
+        * Upgrade the grid with new nodes.
+        * @param _gnodes The list of new nodes
         */
        public void updateGrid( ArrayList<GNode> _gnodes )
        {
@@ -197,7 +212,60 @@ 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 ;
+                               for( j = 0; j < clusters.size(); j++ )
+                               {
+                                       if( _gnodes.get( i ).getCluster().equalsIgnoreCase( clusters.get( j ).getName() ) )
+                                       {
+                                               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 ).setInCluster( true ) ;
+                                                       _gnodes.get( i ).setMapped( false ) ;
+                                               
+                                                       clusters.get( i ).addGNode( _gnodes.get( j ) ) ;
+//                                                     nb_node++ ;
+                                                       gnodesList.add( _gnodes.get( i ) ) ;
+                                                       
+                                               } else {
+                                                       clusters.get( j ).setGNodeStatus( _gnodes.get( i ), _gnodes.get( i ).getMapped() ) ;
+                                               }
+                                               
+                                               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( _gnodes.get( i ).getName() ) ;
+                                       
+                                       cluster = names[ 1 ] ;
+                                       site = names[ 2 ] ;
+                                       
+                                       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 ) ;
+                                       
+                                       nClust.addGNode( _gnodes.get( i ) ) ;
+//                                     nb_cluster++ ;
+                                       
+                                       clusters.add( nClust ) ;
+//                                     nb_node++ ;
+                                       gnodesList.add( _gnodes.get( i ) ) ;
+                               }
                        }
                        
                        gnodesList_done = false ;
@@ -220,7 +288,7 @@ public class Grid implements Serializable
                                site = _dead.getSite() ;
                                cluster = _dead.getCluster() ;
                        
-                               /* Removing GNode from its cluster */                   
+                               /** Removing GNode from its cluster **/                 
                                for( int j = 0 ; j < clusters.size() ; j++ )
                                {
                                        if( clusters.get( j ).getName().equals( cluster ) && clusters.get( j ).getSite().equals( site ))
@@ -232,8 +300,8 @@ public class Grid implements Serializable
                                }
                        }
                        
-                       /* Removing the dead node from the global list */
-                       for( int i = 0 ; i < nb_node ; i++ )
+                       /** Removing the dead node from the global list **/
+                       for( int i = 0 ; i < gnodesList.size() ; i++ )
                        {
                                if( _dead.getId() == gnodesList.get( i ).getId() )
                                {
@@ -243,25 +311,146 @@ public class Grid implements Serializable
                        }
                }
                
-               nb_node-- ;
+//             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 )
+               {
+                       getClusterOfNode( _g ).setGNodeStatus( _g, _status ) ;
+                       
+                       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 Cluster getClusterOfNode( GNode _g )
+       {
+               if( _g != null )
+               {
+                       for( int i = 0 ; i < clusters.size() ; i++ )
+                       {
+                               if( _g.getCluster().equalsIgnoreCase( clusters.get( i ).getName() ) ) 
+                               {
+                                       return clusters.get( i ) ;
+                               }
+                       }
+               }
+               
+               return null ;
+       }
+       
        /**
         * Compute the heterogeneity degree of the grid.
-        * This is based on a ratio between the average and the 
-        * standard deviation of computing nodes' power.
+        * This is based on the relative standard deviation.
         * @return The heterogeneity degree of the grid
         */
        public double getHeterogenityDegre()
        {
+               if( ! ( gnodesList.size() > 0 ) )
+               {
+                       System.err.println( "Mapping - Heterogeneity degree computation! " +
+                                       "List's size not corresponding!" ) ;
+                       return -1 ;
+               }
+               
                double hd = -1 ;
                
+               double average = 0 ;
+               double std = 0 ;
+               double temp = 0 ;
+               
+               /** Computation of the average power of computing nodes **/
+               for( int i = 0 ; i < gnodesList.size() ; i++ )
+               {
+                       temp += gnodesList.get(i).getPower() ;
+               }
+               
+               average = temp / 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 ) ;
+               }
+               
+               /** Computation of the standard deviation **/
+               temp = temp / gnodesList.size() ;
+               std = Math.sqrt( temp ) ;
+               
+               /** Computation of the relative standard deviation
+                * plus modifications 
+                */
+               hd = 100 * std / average / 10 ;
+               
                
                return hd ;
        }
        
+       /**
+        * 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() ;
+               }
+       }
+       
+       
+       
+       /**
+        * 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 ) ;
+                               
+                               getClusterOfNode( _ag.get( i ) ).setGNodeStatus( _ag.get( i ), false ) ;
+                       }
+               }
+       }
+       
+       
        
        /**
         * Print a comprehensible text version of the grid.
@@ -269,8 +458,8 @@ public class Grid implements Serializable
        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() ) ;
                }