Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
0499f1ad213c4a158ec5004af1302ecd1d10a844
[mapping.git] / src / and / Mapping / Mapping.java
1 package and.Mapping ;
2
3 import java.io.Serializable;
4 import java.util.ArrayList;
5
6
7 /**
8  * Class representing the tasks mapping on clusters and/or nodes
9  * @author Sébastien Miquée
10  *
11  */
12 public class Mapping implements Serializable
13 {       
14         private static final long serialVersionUID = 1L;
15         
16         /* Two kinds of Mapping, according to algorithms' goal */
17         private ArrayList<Association> mapping ;
18         private ArrayList<Association> mapping2 ;
19         private int type ; // 0 : mapping task/node ; 1 : mapping tasks/cluster
20         
21         
22         /**
23          * Default constructor
24          */
25         public Mapping()
26         {
27                 mapping = new ArrayList<Association>() ;
28                 mapping2 = new ArrayList<Association>() ;
29                 type = -1 ;
30         }
31         
32         
33         /**
34          * Initialization of the Mapping variables
35          */
36         public void initMapping()
37         {
38                 mapping = new ArrayList<Association>() ;
39                 mapping2 = new ArrayList<Association>() ;
40                 type = -1 ;
41         }
42         
43         
44         /**
45          * Add in the mapping an association between a cluster and tasks set.
46          * @param c Cluster of the association 
47          * @param at Tasks set to be associated
48          */
49         public void addMapping( Cluster c, ArrayList<GTask> at )
50         {
51                 mapping2.add( new Association( c, at ) ) ;
52                 
53                 if( type == 1 || type == -1 )
54                 {
55                         type = 1 ;
56                 } else {
57                         System.err.println( "Mapping type mismatch !" ) ;
58                         System.exit( 1 ) ;
59                 }
60                 
61                 /** For the usage of algorithms which map groups of tasks on cluster **/
62                 for( int i = 0 ; i < at.size() ; i++ )
63                 {
64                         insertMapping( new Association( c.nextGNode(), at.get( i ) ) ) ;
65                 }
66         }
67         
68         
69         /**
70          * Add a mapping association in the general mapping.
71          * @param _a Association between a task and a node
72          */
73         public void addMapping( Association _a )
74         {
75                 if( type == 0 || type == -1 )
76                 {
77                         type = 0 ;
78                 } else {
79                         System.err.println( "Mapping type mismatch !" ) ;
80                         System.exit( 1 ) ;
81                 }
82                 
83                 insertMapping( _a ) ;
84         }
85         
86         
87         /**
88          * Insert the association at the right place.
89          * @param _a The association to be inserted
90          */
91         public void insertMapping( Association _a )
92         {
93                 if( _a != null && _a.getGNode() != null && _a.getGTask() != null )
94                 {
95                         int ind = _a.getGTask().getNum() ;
96                         
97                         mapping.add( ind - 1, _a ) ;
98                 }
99         }
100         
101         
102         /**
103          * Remove a failed node from the mapping.
104          * @param _deadNode The failed node
105          * @return The task associated with the failed node
106          */
107         public GTask removeGNode( GNode _deadNode )
108         {
109                 GTask gt = null ;
110                 
111                 for( int i = 0 ; i < mapping.size() ; i++ )
112                 {
113                         if( mapping.get( i ).getGNode().getId() == _deadNode.getId() ) 
114                         {
115                                 gt = mapping.get( i ).getGTask() ;
116                                 mapping.remove( i ) ;
117                                 break ;
118                         }
119                 }
120                 
121                 return gt ;
122         }
123         
124         
125         /**
126          * Return the list of GNodes on which tasks are mapped, in order
127          * of the task number.
128          * @return The ordered list, according to the GTasks id, of GNodes involved in the mapping
129          */
130         public ArrayList<GNode> getMappedGNodes()
131         {
132                 ArrayList<GNode> ar = new ArrayList<GNode>() ;
133                 
134                 if( mapping.size() != 0 )
135                 {
136 //                      if( type == 0 )
137 //                      {
138 //                              ArrayList<Association> tmp = (ArrayList<Association>) mapping.clone() ;
139                         
140                                 for( int i = 0 ; i < mapping.size() ; i++ )
141                                 {
142 //                                      for( int j = 0 ; j < tmp.size() ; j++ )
143 //                                      {
144 //                                              if( tmp.get( j ).getGTask().getNum() == i )
145 //                                              {
146                                                         ar.add( mapping.get( i ).getGNode() ) ;
147 //                                                      tmp.remove( j ) ;
148 //                                                      j = tmp.size() + 2 ;
149 //                                              }
150 //                                      }
151                                 }
152 //                      }
153 //                      
154 //                      if( type == 1 )
155 //                      {
156 //                              ArrayList<Association> tmp = (ArrayList<Association>) mapping2.clone() ;
157 //                              
158 //                              for( int i = 0 ; i < mapping2.size() ; i++ )
159 //                              {
160 //                                      for( int j = 0 ; j < tmp.size() ; j++ )
161 //                                      {
162 //                                              if( tmp.get( j ).getGTask().getNum() == i )
163 //                                              {
164 //                                                      ar.add( tmp.get( j ).getGNode() ) ;
165 //                                                      tmp.remove( j ) ;
166 //                                                      j = tmp.size() + 2 ;
167 //                                              }
168 //                                      }
169 //                              }
170 //                      }
171                 }
172                 
173                 return ar ;
174         }
175         
176         
177         /**
178          * Print the status of the mapping done, according to its type.
179          */
180         public void print()
181         {
182                 System.out.println();
183                 System.out.println( "\t=> Mapping done :\n" ) ;
184                 
185                 if( type == 0 )
186                 {
187                         ArrayList<GNode> ar = getMappedGNodes() ;
188                         
189                         for( int i = 0 ; i < ar.size() ; i++ )
190                         {
191                                 System.out.println( "Task " + i + " on  " + ar.get( i ).getName() ) ; 
192                         }
193                         
194                         System.out.println() ;
195                 }
196                 
197                 if( type == 1 )
198                 {
199                         for( int i = 0 ; i < mapping2.size() ; i++ )
200                         {
201                                 System.out.print( "\t\tCluster \"" + mapping2.get( i ).getCluster().getName() + "\" => { ") ;
202                                 for( int j = 0 ; j < mapping2.get( i ).getGtask().size() ; j++ )
203                                 {
204                                         System.out.print( mapping2.get( i ).getGtask().get( j ).getNum() ) ;
205                                 
206                                         if( j != mapping2.get( i ).getGtask().size() - 1 )
207                                         {
208                                                 System.out.print( ", " ) ;
209                                         }
210                                 }
211                                 System.out.println( " } " ) ;
212                                 System.out.println() ;
213                         }
214                 }
215         }
216
217         
218         /**
219          * Return the mapping done.
220          * @return The mapping
221          */
222         public ArrayList<Association> getMapping() 
223         {
224                 return mapping ;
225         }
226         
227         
228         /**
229          * Return the position of the association containing 
230          * the GNode _g.
231          * @param _g The GNode to be search
232          * @return The position of the association
233          */
234         public int getIdOfAssociation( GNode _g )
235         {
236                 int ret = -1 ;
237                 
238                 for( int i = 0 ; i < mapping.size() ; i++ )
239                 {
240                         if( mapping.get( i ).getGNode().getId() == _g.getId() )
241                         {
242                                 i = ret ;
243                                 break ;
244                         }
245                 }
246                 
247                 return ret ;
248         }
249         
250         
251         /**
252          * Return the amount of external tasks dependencies, in cluster point of view.
253          * @return The amount of external dependencies
254          */
255         public int calcDepExt()
256         {
257                 int depExt = 0 ;
258                 ArrayList<GTask> ar ;
259                 ArrayList<GTask> deps ;
260                 
261                 for( int i = 0 ; i < mapping.size() ; i++ )
262                 {
263                         ar = mapping.get(i).getGtask() ;
264                         
265                         for( int j = 0 ; j < ar.size() ; j++ )
266                         {
267                                 deps = ar.get(j).getDependencies() ;
268                                 
269                                 for( int k = 0 ; k < deps.size() ; k++ )
270                                 {
271                                         if( ! ar.contains( deps.get(k) ) )
272                                         {
273                                                 depExt++ ;
274                                         }
275                                 }
276                         }
277                 }
278                 
279                 return ( depExt / 2 ) ;
280         }
281         
282 }
283
284 /** La programmation est un art, respectons ceux qui la pratiquent !! **/