Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
e8320c4332a0a33136d1f02b37a7c909a5ce540e
[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 ArrayList<GNode> other ;
20         private int type ; // 0 : mapping task/node ; 1 : mapping tasks/cluster
21         
22         
23         /**
24          * Default constructor
25          */
26         public Mapping()
27         {
28                 mapping = new ArrayList<Association>() ;
29                 mapping2 = new ArrayList<Association>() ;
30                 other = new ArrayList<GNode>() ;
31                 type = -1 ;
32         }
33         
34         
35         /**
36          * Initialization of the Mapping variables
37          */
38         public void initMapping()
39         {
40                 mapping = new ArrayList<Association>() ;
41                 mapping2 = new ArrayList<Association>() ;
42                 other = new ArrayList<GNode>() ;
43                 type = -1 ;
44         }
45         
46         
47         /**
48          * Add in the mapping an association between a cluster and tasks set.
49          * @param c Cluster of the association 
50          * @param at Tasks set to be associated
51          */
52         public void addMapping( Cluster c, ArrayList<GTask> at )
53         {
54                 mapping2.add( new Association( c, at ) ) ;
55                 
56                 if( type == 1 || type == -1 )
57                 {
58                         type = 1 ;
59                 } else {
60                         System.err.println( "Mapping type mismatch !" ) ;
61                         System.exit( 1 ) ;
62                 }
63                 
64                 /** For the usage of algorithms which map groups of tasks on cluster **/
65                 GNode tmp = null ;
66                 for( int i = 0 ; i < at.size() ; i++ )
67                 {
68                         tmp = c.moreGNode() ;
69                         if( tmp != null )
70                         {
71                                 insertMapping( new Association( tmp, at.get( i ) ) ) ;
72                         } else {
73                                 System.err.println( "Error during reception of the next GNode !" ) ;
74                                 break ;
75                         }
76                 }
77         }
78         
79         
80         /**
81          * Add a mapping association in the general mapping.
82          * @param _a Association between a task and a node
83          */
84         public void addMapping( Association _a )
85         {
86                 if( type == 0 || type == -1 )
87                 {
88                         type = 0 ;
89                 } else {
90                         System.err.println( "Mapping type mismatch !" ) ;
91                         System.exit( 1 ) ;
92                 }
93                 
94                 insertMapping( _a ) ;
95         }
96         
97         
98         /**
99          * Insert the association at the right place.
100          * @param _a The association to be inserted
101          */
102         public void insertMapping( Association _a )
103         {
104                 if( _a != null && _a.getGNode() != null && _a.getGTask() != null )
105                 {
106                         mapping.add( _a ) ;
107                 }
108         }
109         
110         
111         /**
112          * Determine if a node is used as an other node in the mapping.
113          * @param _g The node to search
114          * @return The position of the node
115          */
116         private int searchOther( GNode _g )
117         {
118                 int pos = -1 ;
119                 
120                 if( _g != null )
121                 {
122                         for( int i = 0 ; i < other.size() ; i++ )
123                         {
124                                 if( _g.getId() == other.get( i ).getId() ) 
125                                 {
126                                         pos = i ;
127                                         break ;
128                                 }
129                         }
130                 }
131                 
132                 return pos ;
133         }
134         
135         
136         /**
137          * Add a new node in the other nodes list.
138          * @param _g The other node
139          */
140         public void addOtherNode( GNode _g )
141         {
142                 if( _g != null )
143                 {
144                         int pos = searchOther( _g ) ;
145                         
146                         if( pos != -1 )
147                         {
148                                 other.add( _g ) ;
149                         } else {
150                                 System.out.println( "This node already exist in OtherNodes! " +
151                                                 "I replace it" ) ;
152                                 other.set( pos, _g ) ;
153                         }
154                 }
155         }
156         
157         
158         /**
159          * Remove a node in the other nodes list.
160          * @param _g The node to be removed
161          */
162         public void removeOtherNode( GNode _g )
163         {
164                 if( _g != null )
165                 {
166                         int pos = searchOther( _g ) ;
167                         
168                         if( pos != -1 )
169                         {
170                                 other.remove( pos ) ;
171                         } else {
172                                 System.err.println( "This node does not exist in OtherNodes!" ) ;
173                         }
174                 }
175         }
176         
177         
178         /**
179          * Remove a failed node from the mapping.
180          * @param _deadNode The failed node
181          * @return The task associated with the failed node
182          */
183         public GTask removeGNode( GNode _deadNode )
184         {
185                 GTask gt = null ;
186                 
187                 for( int i = 0 ; i < mapping.size() ; i++ )
188                 {
189                         if( mapping.get( i ).getGNode().getId() == _deadNode.getId() ) 
190                         {
191                                 gt = mapping.get( i ).getGTask() ;
192                                 mapping.remove( i ) ;
193                                 break ;
194                         }
195                 }
196                 
197                 return gt ;
198         }
199         
200         
201         /**
202          * Return the list of GNodes on which tasks are mapped, in order
203          * of the task number.
204          * @return The ordered list, according to the GTasks id, of GNodes involved in the mapping
205          */
206         public ArrayList<GNode> getMappedGNodes()
207         {
208                 ArrayList<GNode> ar = new ArrayList<GNode>() ;
209
210                 if( mapping.size() != 0 )
211                 {
212                         for( int i = 0 ; i < mapping.size() ; i++ )
213                         {
214                                 ar.add( mapping.get( i ).getGNode() ) ;
215                         }
216                 }
217
218                 return ar ;
219         }
220
221         
222         /**
223          * Print the status of the mapping done, according to its type.
224          */
225         public void print( int _type )
226         {
227                 int type_print = _type ;
228                 
229                 System.out.println();
230                 System.out.println( "\t=> Mapping done:\n" ) ;
231                 
232                 if( type_print == 0 )
233                 {
234                         ArrayList<GNode> ar = getMappedGNodes() ;
235                         
236                         for( int i = 0 ; i < ar.size() ; i++ )
237                         {
238                                 System.out.println( "Task " + i + " on  " + ar.get( i ).getName() ) ; 
239                         }
240                         
241                         System.out.println() ;
242                 }
243                 
244                 if( type_print == 1 )
245                 {
246                         for( int i = 0 ; i < mapping2.size() ; i++ )
247                         {
248                                 System.out.print( "\t\tCluster \"" + mapping2.get( i ).getCluster().getName() + "\" => { ") ;
249                                 for( int j = 0 ; j < mapping2.get( i ).getGtask().size() ; j++ )
250                                 {
251                                         System.out.print( mapping2.get( i ).getGtask().get( j ).getNum() ) ;
252                                 
253                                         if( j != mapping2.get( i ).getGtask().size() - 1 )
254                                         {
255                                                 System.out.print( ", " ) ;
256                                         }
257                                 }
258                                 System.out.println( " } " ) ;
259                                 System.out.println() ;
260                         }
261                 }
262         }
263
264         
265         /**
266          * Return the mapping done.
267          * @return The mapping
268          */
269         public ArrayList<Association> getMapping() 
270         {
271                 return mapping ;
272         }
273         
274         
275         /**
276          * Return the position of the association containing 
277          * the GNode _g.
278          * @param _g The GNode to be search
279          * @return The position of the association
280          */
281         public int getIdOfAssociation( GNode _g )
282         {
283                 int ret = -1 ;
284                 
285                 for( int i = 0 ; i < mapping.size() ; i++ )
286                 {
287                         if( mapping.get( i ).getGNode().getId() == _g.getId() )
288                         {
289                                 ret = i ;
290                                 break ;
291                         }
292                 }
293                 
294                 return ret ;
295         }
296         
297         
298         /**
299          * Return the association of the given position.
300          * @param _id The position of the Association
301          * @return The Association requested
302          */
303         public Association getAssociation( int _id ) 
304         {
305                 if( _id >= 0 && _id < mapping.size() )
306                 {
307                         return mapping.get( _id ) ;
308                 } else { 
309                         return null ;
310                 }
311         }
312         
313         
314         /**
315          * Remove the association of the given position.
316          * @param _id The position of the Association
317          * @return The Association removed
318          */
319         public Association removeAssociation( int _id )
320         {
321                 if( _id >= 0 && _id < mapping.size() )
322                 {
323                         return mapping.remove( _id ) ;
324                 } else {
325                         return null ;
326                 }
327         }
328         
329         /**
330          * Return the position of the association containing 
331          * the GTask of a specified rank.
332          * @param _taskRank The rank of the task
333          * @return The position of the association
334          */
335         public int getIdOfAssociation( int _taskRank )
336         {
337                 int ret = -1 ;
338                 
339                 for( int i = 0 ; i < mapping.size() ; i++ )
340                 {
341                         if( mapping.get( i ).getGTask().getNum() == _taskRank )
342                         {
343                                 ret = i ;
344                                 break ;
345                         }
346                 }
347                 
348                 return ret ;
349         }
350         
351         /**
352          * Return the amount of external tasks dependencies, in cluster point of view.
353          * @return The amount of external dependencies
354          */
355         public int calcDepExt()
356         {
357                 int depExt = 0 ;
358                 ArrayList<GTask> ar ;
359                 ArrayList<GTask> deps ;
360                 
361                 for( int i = 0 ; i < mapping.size() ; i++ )
362                 {
363                         ar = mapping.get(i).getGtask() ;
364                         
365                         for( int j = 0 ; j < ar.size() ; j++ )
366                         {
367                                 deps = ar.get(j).getDependencies() ;
368                                 
369                                 for( int k = 0 ; k < deps.size() ; k++ )
370                                 {
371                                         if( ! ar.contains( deps.get(k) ) )
372                                         {
373                                                 depExt++ ;
374                                         }
375                                 }
376                         }
377                 }
378                 
379                 return ( depExt / 2 ) ;
380         }
381         
382 }
383
384 /** La programmation est un art, respectons ceux qui la pratiquent !! **/