Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
New version of MAHEVE plus corrections.
[mapping.git] / src / and / Mapping / Mapping.java
index 1136d55..1803d63 100644 (file)
@@ -16,7 +16,9 @@ public class Mapping implements Serializable
        /* Two kinds of Mapping, according to algorithms' goal */
        private ArrayList<Association> mapping ;
        private ArrayList<Association> mapping2 ;
+       private ArrayList<GNode> other ;
        private int type ; // 0 : mapping task/node ; 1 : mapping tasks/cluster
+       private Grid gd ;
        
        
        /**
@@ -26,7 +28,9 @@ public class Mapping implements Serializable
        {
                mapping = new ArrayList<Association>() ;
                mapping2 = new ArrayList<Association>() ;
+               other = new ArrayList<GNode>() ;
                type = -1 ;
+               gd = null ;
        }
        
        
@@ -37,7 +41,19 @@ public class Mapping implements Serializable
        {
                mapping = new ArrayList<Association>() ;
                mapping2 = new ArrayList<Association>() ;
+               other = new ArrayList<GNode>() ;
                type = -1 ;
+               gd = null ;
+       }
+       
+       
+       /**
+        * Set the grid in which the mapping is done.
+        * @param _gd The grid
+        */
+       public void setGrid( Grid _gd )
+       {
+               gd = _gd ;
        }
        
        
@@ -59,18 +75,29 @@ public class Mapping implements Serializable
                }
                
                /** For the usage of algorithms which map groups of tasks on cluster **/
+               GNode tmp = null ;
+               c.initMoreGNode() ;
                for( int i = 0 ; i < at.size() ; i++ )
                {
-                       insertMapping( new Association( c.nextGNode(), at.get( i ) ) ) ;
+                       tmp = c.moreGNode() ;
+                       if( tmp != null )
+                       {
+                               insertMapping( new Association( tmp, at.get( i ) ), false ) ;
+                       } else {
+                               System.err.println( "Error during reception of the next GNode !" ) ;
+                               break ;
+                       }
                }
        }
        
        
        /**
-        * Add a mapping association in the general mapping.
-        * @param _a Association between a task and a node
+        * Add a mapping association between a task and a node
+        * in the general mapping.
+        * @param _gn The node on which the task is mapped
+        * @param _gt The task mapped on the node
         */
-       public void addMapping( Association _a )
+       public void addMapping( GNode _gn, GTask _gt )
        {
                if( type == 0 || type == -1 )
                {
@@ -80,7 +107,7 @@ public class Mapping implements Serializable
                        System.exit( 1 ) ;
                }
                
-               insertMapping( _a ) ;
+               insertMapping( new Association( _gn, _gt ), true ) ;
        }
        
        
@@ -88,14 +115,108 @@ public class Mapping implements Serializable
         * Insert the association at the right place.
         * @param _a The association to be inserted
         */
-       public void insertMapping( Association _a )
+       public void insertMapping( Association _a, boolean _other )
        {
                if( _a != null && _a.getGNode() != null && _a.getGTask() != null )
                {
-//                     int ind = _a.getGTask().getNum() ;
-//                     
-//                     mapping.add( ind - 1, _a ) ;
                        mapping.add( _a ) ;
+                       
+                       if( _other )
+                       {
+                               updateMapping2( _a ) ;
+                       }
+               }
+       }
+       
+       
+       private void updateMapping2( Association _a ) 
+       {
+               boolean ok = false ;
+               
+               for( int i = 0 ; i < mapping2.size() ; i++ )
+               {
+                       if( mapping2.get( i ).getCluster().getName().equalsIgnoreCase( _a.getGNode().getClusterName() ) )
+                       {
+                               mapping2.get( i ).getGtasks().add( _a.getGTask() ) ;
+                               
+                               ok = true ;
+                               break ;
+                       }
+               }
+               
+               if( !ok )
+               {
+                       Cluster cl = _a.getGNode().getCluster() ;
+                       ArrayList<GTask> gr = new ArrayList<GTask>() ;
+                       gr.add( _a.getGTask() ) ;
+                       mapping2.add( new Association( cl, gr ) ) ;
+               }
+       }
+
+
+       /**
+        * Determine if a node is used as an other node in the mapping.
+        * @param _g The node to search
+        * @return The position of the node
+        */
+       private int searchOther( GNode _g )
+       {
+               int pos = -1 ;
+               
+               if( _g != null )
+               {
+                       for( int i = 0 ; i < other.size() ; i++ )
+                       {
+                               if( _g.getId() == other.get( i ).getId() ) 
+                               {
+                                       pos = i ;
+                                       break ;
+                               }
+                       }
+               }
+               
+               return pos ;
+       }
+       
+       
+       /**
+        * Add a new node in the other nodes list.
+        * @param _g The other node
+        */
+       public void addOtherNode( GNode _g )
+       {
+               if( _g != null )
+               {
+                       int pos = searchOther( _g ) ;
+                       
+                       if( pos != -1 )
+                       {
+                               other.add( _g ) ;
+                       } else {
+                               System.out.println( "This node already exist in OtherNodes! " +
+                                               "I replace it" ) ;
+                               other.set( pos, _g ) ;
+                       }
+               }
+       }
+       
+       
+       /**
+        * Remove a node in the other nodes list.
+        * @param _g The node to be removed
+        */
+       public void removeOtherNode( GNode _g )
+       {
+               if( _g != null )
+               {
+                       int pos = searchOther( _g ) ;
+                       
+                       if( pos != -1 )
+                       {
+                               other.remove( pos ) ;
+                       } else {
+                               System.err.println( "This node does not exist in OtherNodes!" ) ;
+                       }
                }
        }
        
@@ -131,59 +252,54 @@ public class Mapping implements Serializable
        public ArrayList<GNode> getMappedGNodes()
        {
                ArrayList<GNode> ar = new ArrayList<GNode>() ;
-               
+
                if( mapping.size() != 0 )
                {
-//                     if( type == 0 )
-//                     {
-//                             ArrayList<Association> tmp = (ArrayList<Association>) mapping.clone() ;
+                       ArrayList<Association> mp = organizeMapping() ;
                        
-                               for( int i = 0 ; i < mapping.size() ; i++ )
-                               {
-//                                     for( int j = 0 ; j < tmp.size() ; j++ )
-//                                     {
-//                                             if( tmp.get( j ).getGTask().getNum() == i )
-//                                             {
-                                                       ar.add( mapping.get( i ).getGNode() ) ;
-//                                                     tmp.remove( j ) ;
-//                                                     j = tmp.size() + 2 ;
-//                                             }
-//                                     }
-                               }
-//                     }
-//                     
-//                     if( type == 1 )
-//                     {
-//                             ArrayList<Association> tmp = (ArrayList<Association>) mapping2.clone() ;
-//                             
-//                             for( int i = 0 ; i < mapping2.size() ; i++ )
-//                             {
-//                                     for( int j = 0 ; j < tmp.size() ; j++ )
-//                                     {
-//                                             if( tmp.get( j ).getGTask().getNum() == i )
-//                                             {
-//                                                     ar.add( tmp.get( j ).getGNode() ) ;
-//                                                     tmp.remove( j ) ;
-//                                                     j = tmp.size() + 2 ;
-//                                             }
-//                                     }
-//                             }
-//                     }
+                       for( int i = 0 ; i < mp.size() ; i++ )
+                       {
+                               ar.add( mp.get( i ).getGNode() ) ;
+                       }
                }
-               
+
                return ar ;
        }
+
        
-       
+       private ArrayList<Association> organizeMapping() 
+       {
+               ArrayList<Association> ret = null ;
+               
+               if( mapping.size() > 0 )
+               {
+                       ret = new ArrayList<Association>( mapping.size() ) ;
+                       for( int i = 0 ; i < mapping.size() ; i++ )
+                               ret.add( null ) ;
+                       
+                       for( int i = 0 ; i < mapping.size() ; i++ )
+                       {
+                               ret.set( mapping.get( i ).getGTask().getNum(), mapping.get( i ) ) ;
+                       }
+               } else {
+                       System.out.println( "No mapping..." ) ;
+               }
+               
+               return ret ;
+       }
+
+
        /**
         * Print the status of the mapping done, according to its type.
         */
-       public void print()
+       public void print( int _type )
        {
+               int type_print = _type ;
+               
                System.out.println();
-               System.out.println( "\t=> Mapping done :\n" ) ;
+               System.out.println( "\t=> Mapping done:\n" ) ;
                
-               if( type == 0 )
+               if( type_print == 0 )
                {
                        ArrayList<GNode> ar = getMappedGNodes() ;
                        
@@ -195,16 +311,16 @@ public class Mapping implements Serializable
                        System.out.println() ;
                }
                
-               if( type == 1 )
+               if( type_print == 1 )
                {
                        for( int i = 0 ; i < mapping2.size() ; i++ )
                        {
                                System.out.print( "\t\tCluster \"" + mapping2.get( i ).getCluster().getName() + "\" => { ") ;
-                               for( int j = 0 ; j < mapping2.get( i ).getGtask().size() ; j++ )
+                               for( int j = 0 ; j < mapping2.get( i ).getGtasks().size() ; j++ )
                                {
-                                       System.out.print( mapping2.get( i ).getGtask().get( j ).getNum() ) ;
+                                       System.out.print( mapping2.get( i ).getGtasks().get( j ).getNum() ) ;
                                
-                                       if( j != mapping2.get( i ).getGtask().size() - 1 )
+                                       if( j != mapping2.get( i ).getGtasks().size() - 1 )
                                        {
                                                System.out.print( ", " ) ;
                                        }
@@ -249,6 +365,59 @@ public class Mapping implements Serializable
        }
        
        
+       /**
+        * Return the association of the given position.
+        * @param _id The position of the Association
+        * @return The Association requested
+        */
+       public Association getAssociation( int _id ) 
+       {
+               if( _id >= 0 && _id < mapping.size() )
+               {
+                       return mapping.get( _id ) ;
+               } else { 
+                       return null ;
+               }
+       }
+       
+       
+       /**
+        * Remove the association of the given position.
+        * @param _id The position of the Association
+        * @return The Association removed
+        */
+       public Association removeAssociation( int _id )
+       {
+               if( _id >= 0 && _id < mapping.size() )
+               {
+                       return mapping.remove( _id ) ;
+               } else {
+                       return null ;
+               }
+       }
+       
+       /**
+        * Return the position of the association containing 
+        * the GTask of a specified rank.
+        * @param _taskRank The rank of the task
+        * @return The position of the association
+        */
+       public int getIdOfAssociation( int _taskRank )
+       {
+               int ret = -1 ;
+               
+               for( int i = 0 ; i < mapping.size() ; i++ )
+               {
+                       if( mapping.get( i ).getGTask().getNum() == _taskRank )
+                       {
+                               ret = i ;
+                               break ;
+                       }
+               }
+               
+               return ret ;
+       }
+       
        /**
         * Return the amount of external tasks dependencies, in cluster point of view.
         * @return The amount of external dependencies
@@ -256,23 +425,19 @@ public class Mapping implements Serializable
        public int calcDepExt()
        {
                int depExt = 0 ;
-               ArrayList<GTask> ar ;
-               ArrayList<GTask> deps ;
+
+               ArrayList<Association> mp = organizeMapping() ;
                
-               for( int i = 0 ; i < mapping.size() ; i++ )
+               for( int i = 0 ; i < mp.size() ; i++ )
                {
-                       ar = mapping.get(i).getGtask() ;
-                       
-                       for( int j = 0 ; j < ar.size() ; j++ )
+                       ArrayList<Integer> dids = mp.get( i ).getGTask().getDependenciesIds() ;
+                                       
+                       for( int j = 0 ; j < dids.size() ; j++ )
                        {
-                               deps = ar.get(j).getDependencies() ;
-                               
-                               for( int k = 0 ; k < deps.size() ; k++ )
+                               if( ! mp.get( i ).getGNode().getSiteName().equalsIgnoreCase( 
+                                               mp.get( dids.get(j) ).getGNode().getSiteName() ) )
                                {
-                                       if( ! ar.contains( deps.get(k) ) )
-                                       {
-                                               depExt++ ;
-                                       }
+                                       depExt++ ;
                                }
                        }
                }
@@ -280,6 +445,79 @@ public class Mapping implements Serializable
                return ( depExt / 2 ) ;
        }
        
+       
+       /**
+        * ** TO BE MODIFIED !!!
+        * @return
+        */
+       public double calcAsyncMark()
+       {
+               double mark = 0 ;
+               
+               ArrayList<Double> comput = new ArrayList<Double>() ;
+               ArrayList<Double> comput2 = new ArrayList<Double>() ;
+               double max_time = 0 ; 
+               
+               ArrayList<Association> mp = organizeMapping() ;
+               
+               /** Initialization **/
+               for( int i = 0 ; i < mp.size() ; i++ )
+               {
+                       Double calc = new Double( mp.get( i ).getGTask().getWeight() / 
+                                       mp.get( i ).getGNode().getPower() ) ;
+                       
+                       comput.add( calc ) ;
+                       comput2.add( new Double ( -1 ) ) ;
+                       
+                       if( calc > max_time )
+                       {
+                               max_time = calc ;
+                       }
+               }
+               
+               /** Normalization **/
+               for( int i = 0 ; i < mp.size() ; i++ )
+               {
+                       comput.set( i, comput.get( i ) / max_time ) ;
+               }
+               
+               
+               for( int k = 0 ; k < 2 ; k++ )
+               for( int i = 0 ; i < mp.size() ; i++ )
+               {
+                       ArrayList<Integer> tmp = mp.get(i).getGTask().getDependenciesIds() ;
+
+                       double calc = 0 ;
+                       double valv ;
+                                               
+                       for( int j = 0 ; j < tmp.size() ; j++ )
+                       {
+                               if( comput2.get( j ) != -1  )
+                               {
+                                       valv = comput2.get( j ) ;
+                               } else {
+                                       valv = comput.get( j ) ;
+                               }
+
+                               calc += ( valv + ( gd.getDistance( mp.get( i ).getGNode(), mp.get( j ).getGNode() ) 
+                                                               / gd.getMaxDistance() ) ) ;
+                       }
+                       
+                       comput2.set( i, comput.get( i ) * calc ) ;
+               }
+               
+               mark = -1 ;
+               for( int i = 0 ; i < comput2.size() ; i++ )
+               {
+                       if( mark < comput2.get( i ) )
+                       {
+                               mark = comput2.get( i ) ;
+                       }
+               }
+                               
+               return mark ;
+       }
+       
 }
 
 /** La programmation est un art, respectons ceux qui la pratiquent !! **/