Logo AND Algorithmique Numérique Distribuée

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