Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
1136d5590773ac537726eb215908ae0ad2bb3383
[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                         mapping.add( _a ) ;
99                 }
100         }
101         
102         
103         /**
104          * Remove a failed node from the mapping.
105          * @param _deadNode The failed node
106          * @return The task associated with the failed node
107          */
108         public GTask removeGNode( GNode _deadNode )
109         {
110                 GTask gt = null ;
111                 
112                 for( int i = 0 ; i < mapping.size() ; i++ )
113                 {
114                         if( mapping.get( i ).getGNode().getId() == _deadNode.getId() ) 
115                         {
116                                 gt = mapping.get( i ).getGTask() ;
117                                 mapping.remove( i ) ;
118                                 break ;
119                         }
120                 }
121                 
122                 return gt ;
123         }
124         
125         
126         /**
127          * Return the list of GNodes on which tasks are mapped, in order
128          * of the task number.
129          * @return The ordered list, according to the GTasks id, of GNodes involved in the mapping
130          */
131         public ArrayList<GNode> getMappedGNodes()
132         {
133                 ArrayList<GNode> ar = new ArrayList<GNode>() ;
134                 
135                 if( mapping.size() != 0 )
136                 {
137 //                      if( type == 0 )
138 //                      {
139 //                              ArrayList<Association> tmp = (ArrayList<Association>) mapping.clone() ;
140                         
141                                 for( int i = 0 ; i < mapping.size() ; i++ )
142                                 {
143 //                                      for( int j = 0 ; j < tmp.size() ; j++ )
144 //                                      {
145 //                                              if( tmp.get( j ).getGTask().getNum() == i )
146 //                                              {
147                                                         ar.add( mapping.get( i ).getGNode() ) ;
148 //                                                      tmp.remove( j ) ;
149 //                                                      j = tmp.size() + 2 ;
150 //                                              }
151 //                                      }
152                                 }
153 //                      }
154 //                      
155 //                      if( type == 1 )
156 //                      {
157 //                              ArrayList<Association> tmp = (ArrayList<Association>) mapping2.clone() ;
158 //                              
159 //                              for( int i = 0 ; i < mapping2.size() ; i++ )
160 //                              {
161 //                                      for( int j = 0 ; j < tmp.size() ; j++ )
162 //                                      {
163 //                                              if( tmp.get( j ).getGTask().getNum() == i )
164 //                                              {
165 //                                                      ar.add( tmp.get( j ).getGNode() ) ;
166 //                                                      tmp.remove( j ) ;
167 //                                                      j = tmp.size() + 2 ;
168 //                                              }
169 //                                      }
170 //                              }
171 //                      }
172                 }
173                 
174                 return ar ;
175         }
176         
177         
178         /**
179          * Print the status of the mapping done, according to its type.
180          */
181         public void print()
182         {
183                 System.out.println();
184                 System.out.println( "\t=> Mapping done :\n" ) ;
185                 
186                 if( type == 0 )
187                 {
188                         ArrayList<GNode> ar = getMappedGNodes() ;
189                         
190                         for( int i = 0 ; i < ar.size() ; i++ )
191                         {
192                                 System.out.println( "Task " + i + " on  " + ar.get( i ).getName() ) ; 
193                         }
194                         
195                         System.out.println() ;
196                 }
197                 
198                 if( type == 1 )
199                 {
200                         for( int i = 0 ; i < mapping2.size() ; i++ )
201                         {
202                                 System.out.print( "\t\tCluster \"" + mapping2.get( i ).getCluster().getName() + "\" => { ") ;
203                                 for( int j = 0 ; j < mapping2.get( i ).getGtask().size() ; j++ )
204                                 {
205                                         System.out.print( mapping2.get( i ).getGtask().get( j ).getNum() ) ;
206                                 
207                                         if( j != mapping2.get( i ).getGtask().size() - 1 )
208                                         {
209                                                 System.out.print( ", " ) ;
210                                         }
211                                 }
212                                 System.out.println( " } " ) ;
213                                 System.out.println() ;
214                         }
215                 }
216         }
217
218         
219         /**
220          * Return the mapping done.
221          * @return The mapping
222          */
223         public ArrayList<Association> getMapping() 
224         {
225                 return mapping ;
226         }
227         
228         
229         /**
230          * Return the position of the association containing 
231          * the GNode _g.
232          * @param _g The GNode to be search
233          * @return The position of the association
234          */
235         public int getIdOfAssociation( GNode _g )
236         {
237                 int ret = -1 ;
238                 
239                 for( int i = 0 ; i < mapping.size() ; i++ )
240                 {
241                         if( mapping.get( i ).getGNode().getId() == _g.getId() )
242                         {
243                                 ret = i ;
244                                 break ;
245                         }
246                 }
247                 
248                 return ret ;
249         }
250         
251         
252         /**
253          * Return the amount of external tasks dependencies, in cluster point of view.
254          * @return The amount of external dependencies
255          */
256         public int calcDepExt()
257         {
258                 int depExt = 0 ;
259                 ArrayList<GTask> ar ;
260                 ArrayList<GTask> deps ;
261                 
262                 for( int i = 0 ; i < mapping.size() ; i++ )
263                 {
264                         ar = mapping.get(i).getGtask() ;
265                         
266                         for( int j = 0 ; j < ar.size() ; j++ )
267                         {
268                                 deps = ar.get(j).getDependencies() ;
269                                 
270                                 for( int k = 0 ; k < deps.size() ; k++ )
271                                 {
272                                         if( ! ar.contains( deps.get(k) ) )
273                                         {
274                                                 depExt++ ;
275                                         }
276                                 }
277                         }
278                 }
279                 
280                 return ( depExt / 2 ) ;
281         }
282         
283 }
284
285 /** La programmation est un art, respectons ceux qui la pratiquent !! **/