Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
New version of MAHEVE plus corrections.
[mapping.git] / src / and / Mapping / Mapping.java
index e8320c4..1803d63 100644 (file)
@@ -18,6 +18,7 @@ public class Mapping implements Serializable
        private ArrayList<Association> mapping2 ;
        private ArrayList<GNode> other ;
        private int type ; // 0 : mapping task/node ; 1 : mapping tasks/cluster
+       private Grid gd ;
        
        
        /**
@@ -29,6 +30,7 @@ public class Mapping implements Serializable
                mapping2 = new ArrayList<Association>() ;
                other = new ArrayList<GNode>() ;
                type = -1 ;
+               gd = null ;
        }
        
        
@@ -41,6 +43,17 @@ public class Mapping implements Serializable
                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 ;
        }
        
        
@@ -63,12 +76,13 @@ 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++ )
                {
                        tmp = c.moreGNode() ;
                        if( tmp != null )
                        {
-                               insertMapping( new Association( tmp, at.get( i ) ) ) ;
+                               insertMapping( new Association( tmp, at.get( i ) ), false ) ;
                        } else {
                                System.err.println( "Error during reception of the next GNode !" ) ;
                                break ;
@@ -78,10 +92,12 @@ public class Mapping implements Serializable
        
        
        /**
-        * 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 )
                {
@@ -91,7 +107,7 @@ public class Mapping implements Serializable
                        System.exit( 1 ) ;
                }
                
-               insertMapping( _a ) ;
+               insertMapping( new Association( _gn, _gt ), true ) ;
        }
        
        
@@ -99,15 +115,45 @@ 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 )
                {
                        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
@@ -209,9 +255,11 @@ public class Mapping implements Serializable
 
                if( mapping.size() != 0 )
                {
-                       for( int i = 0 ; i < mapping.size() ; i++ )
+                       ArrayList<Association> mp = organizeMapping() ;
+                       
+                       for( int i = 0 ; i < mp.size() ; i++ )
                        {
-                               ar.add( mapping.get( i ).getGNode() ) ;
+                               ar.add( mp.get( i ).getGNode() ) ;
                        }
                }
 
@@ -219,6 +267,28 @@ public class Mapping implements Serializable
        }
 
        
+       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.
         */
@@ -246,11 +316,11 @@ public class Mapping implements Serializable
                        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( ", " ) ;
                                        }
@@ -355,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++ ;
                                }
                        }
                }
@@ -379,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 !! **/