Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
New version of MAHEVE plus corrections.
[mapping.git] / src / and / Mapping / Grid.java
index 2451b95..902e40f 100644 (file)
@@ -102,6 +102,27 @@ public class Grid implements Serializable
        }
        
        
+       /**
+        * 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 ;
+       }
+       
+       
        /**
         * Compute and return the distance between two clusters.
         * @param _g1 First cluster
@@ -110,37 +131,27 @@ 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 ;
                }
                
+               double d = 0 ;
+               
                String cluster1 = "c1", cluster2 = "c2", site1 = "s1", site2 = "s2" ;
                
-               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() ;
-                       }
-               }
+               cluster1 = _g1.getClusterName() ;
+               site1 = _g1.getSiteName() ;
+               cluster2 = _g2.getClusterName() ;
+               site2 = _g2.getSiteName() ;
                
-               if( cluster1.compareTo( cluster2 ) == 0 )
+               if( cluster1.equalsIgnoreCase( cluster2 ) )
                {
                        d = 10 ;
                } else {
-                       if( site1.compareTo( site2 ) == 0 )
+                       if( site1.equalsIgnoreCase( site2 ) )
                        {
-                               d = 20 ;
+                               d = 15 ;
                        } else {
                                d = 30 ;
                        }
@@ -177,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
@@ -189,16 +222,18 @@ 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 ) ;
                                                
@@ -209,29 +244,28 @@ public class Grid implements Serializable
                                                        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 ) ) ;
                                        
@@ -255,50 +289,51 @@ 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( _g.getCluster().equalsIgnoreCase( clusters.get( j ).getName() ) )
+                               if( _g.getClusterName().equalsIgnoreCase( clusters.get( j ).getName() ) 
+                                       && _g.getSiteName().equalsIgnoreCase( clusters.get( j ).getSite() ) )
                                {
                                        int pos = clusters.get( j ).isIn( _g ) ;
                                                
                                        if( pos == -1 )
                                        {
-                                               _g.setSite( clusters.get( j ).getSite() ) ;
                                                _g.setInCluster( true ) ;
                                                _g.setMapped( false ) ;
+                                               _g.setCluster( clusters.get( j ) ) ;
                                                
                                                clusters.get( j ).addGNode( _g ) ;
                                                gnodesList.add( _g ) ;
-                                                       
+                                               
                                        } else {
-                                               clusters.get( j ).removeGNode( _g ) ;
-                                               clusters.get( j ).addGNode( _g ) ;
+                                               _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( j == clusters.size() )
+                       if( ! ok )
                        {
                                String site = "", cluster = "" ;
                                Cluster nClust = new Cluster() ;
                                                
-                               String names[] = Utils.decodeG5Knames( _g.getName() ) ;
-                                       
-                               cluster = names[ 1 ] ;
-                               site = names[ 2 ] ;
+                               cluster = _g.getClusterName() ; // names[ 1 ] ;
+                               site = _g.getSiteName() ; //names[ 2 ] ;
                                
-                               System.out.println("** (Grid) Creation of cluster: "+cluster);
+                               System.out.println( "** (Grid) Creation of cluster " + cluster + " on site " + site ) ;
                                
                                nClust.setName( cluster ) ;
                                nClust.setSite( site ) ;
                                        
                                _g.setInCluster( true ) ;
                                _g.setMapped( false ) ;
-                               _g.setSite( site ) ;
-                               _g.setCluster( cluster ) ;
+                               _g.setCluster( nClust ) ;
                                        
                                nClust.addGNode( _g ) ;
                                        
@@ -362,9 +397,9 @@ public class Grid implements Serializable
                        
                        if( id != -1 )
                        {
-                               clusters.get(id).setGNodeStatus( _g, _status ) ;
+                               clusters.get( id ).setGNodeStatus( _g, _status ) ;
                        } else {
-                               System.err.println( "(Grid) Cluster "+_g.getCluster()+" not found!" ) ;
+                               System.err.println( "(Grid) Cluster " + _g.getClusterName() + " not found!" ) ;
                        }
                        
                        /** Change in local list **/
@@ -393,13 +428,11 @@ public class Grid implements Serializable
                {
                        for( int i = 0 ; i < clusters.size() ; i++ )
                        {
-                               if( _g.getCluster().equalsIgnoreCase( clusters.get( i ).getName() ) ) 
-                               {
-                                       if( _g.getSite().equalsIgnoreCase( clusters.get( i ).getSite() ) ) 
-                                       {
-                                               ret = i ;
-                                               break ;
-                                       }
+                               if( _g.getClusterName().equalsIgnoreCase( clusters.get( i ).getName() ) 
+                                 && _g.getSiteName().equalsIgnoreCase( clusters.get( i ).getSite() ) ) 
+                               {       
+                                       ret = i ;
+                                       break ;
                                }
                        }
                }
@@ -432,7 +465,7 @@ public class Grid implements Serializable
                /** Computation of the average power of computing nodes **/
                for( int i = 0 ; i < gnodesList.size() ; i++ )
                {
-                       if( ! gnodesList.get(i).getMapped() )
+                       if( ! gnodesList.get( i ).getMapped() )
                        {
                                temp += gnodesList.get(i).getPower() ;
                                nb_freenodes++ ;
@@ -445,7 +478,7 @@ public class Grid implements Serializable
                temp = 0 ;
                for( int i = 0 ; i < gnodesList.size() ; i++ )
                {
-                       if( ! gnodesList.get(i).getMapped() )
+                       if( ! gnodesList.get( i ).getMapped() )
                        {
                                temp += Math.pow( ( gnodesList.get(i).getPower() - average ), 2 ) ;
                        }
@@ -464,7 +497,6 @@ public class Grid implements Serializable
                        hd = 1 ;
                }
                
-               
                return hd ;
        }
        
@@ -490,6 +522,23 @@ public class Grid implements Serializable
        }
        
        
+       /**
+        * 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
@@ -507,6 +556,7 @@ public class Grid implements Serializable
                for( int i = 0 ; i < clusters.size() ; i++ )
                {
                        clusters.get( i ).initGNodes() ;
+                       clusters.get( i ).initMoreGNode() ;
                }
        }
        
@@ -597,6 +647,17 @@ public class Grid implements Serializable
                return ret ;
        }
        
+       
+       /**
+        * Return the max distance it could exist between two computing nodes.
+        * @return The max distance
+        */
+       public double getMaxDistance()
+       {
+               // TODO
+               return 30 ;
+       }
+       
 }