Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
3092a995863ef2a36db3350e91cb1115fdda10cc
[mapping.git] / src / and / Mapping / Simple.java
1 package and.Mapping ;
2
3
4 import java.util.ArrayList;
5
6
7 /**
8  * Implementation of Simple Mapping algorithm
9  * @author Sébastien Miquée
10  * @version 1.0
11  */
12 public class Simple extends Algo
13 {
14         private static final long serialVersionUID = 1L;
15         
16         
17         private ArrayList<GTask> atraiter ;
18         private ArrayList<GNode> archi ;        
19         
20         /**
21          * Default constructor.
22          */
23         public Simple()
24         {
25                 super() ;
26         }
27         
28
29         /**
30          * Constructor.
31          * @param _gr Application graph to be mapped on
32          * @param _gd Grid graph
33          */
34         public Simple( Graph _gr, Grid _gd )
35         {
36                 super( _gr, _gd ) ;
37         }
38         
39         /**
40          * 
41          * @return
42          */
43         private ArrayList<GNode> sortInitGNode() 
44         {
45                 ArrayList<GNode> grn = null ;
46                 
47                 if( gl != null )
48                 {
49                         grn = new ArrayList<GNode>() ;
50                         
51                         // Tri des clusters suivant leur taille
52                     ArrayList<Cluster> cl = gl.getClusters() ;
53                     ArrayList<Cluster> cl2 = new ArrayList<Cluster>() ;
54                     
55                     int max = 0 ;
56                     Cluster c = null ;
57                     Cluster c2 = null ;
58         
59                     while( cl2.size() != gl.getNbCluster() )
60                     {
61                     
62                         max = 0 ;
63                     
64                         for( int i = 0 ; i < cl.size() ; i++ )
65                         {
66                             c = cl.get( i ) ;
67                             if( c.getNbGNode() > max )
68                             {
69                                 c2 = c ;
70                                 max = c.getNbGNode() ;
71                             }
72                         }
73                     
74                         cl2.add( c2 ) ;
75                         cl.remove( c2 ) ;
76                     }
77         
78                         for( int i = 0 ; i < cl2.size() ; i++ )
79                         {
80                             Cluster tmp = cl2.get( i ) ;
81                             
82                             for( int j = 0 ; j < tmp.getNbGNode() ; j++ )
83                             {
84                                 grn.add( tmp.getGNodes().get( j ) ) ;
85                             }
86                         }
87                 }
88                 
89                 return grn ;
90         }
91
92
93         @Override
94         public void map() 
95         {
96                 /* If the mapping is possible ... */
97                 if( gr.getNbGTask() <= gl.getNbGNode() )
98                 {
99                         archi = sortInitGNode() ;
100                         atraiter = gr.getGraph() ;
101                         
102                         System.out.println( "******************************************" ) ;
103                         System.out.println( "* Launching the Simple Mapping algorithm *" ) ;
104                         System.out.println( "******************************************\n\n" ) ;
105                         
106                         /** Save the Mapping **/
107                         for( int i = 0 ; i < atraiter.size() ; i++ )
108                         {
109                                 mp.addMapping( new Association( archi.get( i ), atraiter.get( i ) ) ) ;
110                         }
111                 
112                 } else {
113                         System.err.println( "\n\n!!! Unable to map application !\n\n" ) ;
114                 }
115                 
116                 /** Update in cluster the status of nodes **/
117                 updateGrid() ;
118         }
119
120
121         @Override
122         public GNode replaceNode(GNode _dead, ArrayList<GNode> _ag ) 
123         {
124                 GNode ret = null ;
125                 
126                 if( _dead != null )
127                 {
128                         int pos = 0 ;
129                         pos = mp.getIdOfAssociation( _dead ) ;
130                         
131                         if( pos == -1 )
132                         {
133                                 System.err.println( "GNode "+_dead+" does not exist in the mapping!" ) ;
134                                 return null ;
135                         }
136                         
137                         if( _ag.size() > 0 )
138                         {
139                                 ret = _ag.get( 0 ) ;
140                                 
141                                 
142                         } else {
143                                 System.err.println( "Not enought available nodes in gnodes to replace one !" ) ;
144                                 return null ;
145                         }               
146                 }
147                 
148                 /** Update in cluster the status of nodes **/
149                 updateGrid() ;
150                 
151                 return ret ;
152         }
153
154
155         @Override
156         public GNode getOtherGNode( ArrayList<GNode> _ag ) 
157         {
158                 /** Returning the first node in the list **/
159                 if( _ag.size() > 0 )
160                 {
161                         return _ag.get( 1 ) ;
162                 } 
163                 
164                 return null ;
165         }
166 }
167
168
169
170 /** La programmation est un art, respectons ceux qui la pratiquent !! **/