3 import java.io.Serializable;
4 import java.util.ArrayList;
8 * Class representing the tasks mapping on clusters and/or nodes
9 * @author Sébastien Miquée
12 public class Mapping implements Serializable
14 private static final long serialVersionUID = 1L;
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
28 mapping = new ArrayList<Association>() ;
29 mapping2 = new ArrayList<Association>() ;
30 other = new ArrayList<GNode>() ;
36 * Initialization of the Mapping variables
38 public void initMapping()
40 mapping = new ArrayList<Association>() ;
41 mapping2 = new ArrayList<Association>() ;
42 other = new ArrayList<GNode>() ;
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
52 public void addMapping( Cluster c, ArrayList<GTask> at )
54 mapping2.add( new Association( c, at ) ) ;
56 if( type == 1 || type == -1 )
60 System.err.println( "Mapping type mismatch !" ) ;
64 /** For the usage of algorithms which map groups of tasks on cluster **/
66 for( int i = 0 ; i < at.size() ; i++ )
71 insertMapping( new Association( tmp, at.get( i ) ) ) ;
73 System.err.println( "Error during reception of the next GNode !" ) ;
81 * Add a mapping association in the general mapping.
82 * @param _a Association between a task and a node
84 public void addMapping( Association _a )
86 if( type == 0 || type == -1 )
90 System.err.println( "Mapping type mismatch !" ) ;
99 * Insert the association at the right place.
100 * @param _a The association to be inserted
102 public void insertMapping( Association _a )
104 if( _a != null && _a.getGNode() != null && _a.getGTask() != null )
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
116 private int searchOther( GNode _g )
122 for( int i = 0 ; i < other.size() ; i++ )
124 if( _g.getId() == other.get( i ).getId() )
137 * Add a new node in the other nodes list.
138 * @param _g The other node
140 public void addOtherNode( GNode _g )
144 int pos = searchOther( _g ) ;
150 System.out.println( "This node already exist in OtherNodes! " +
152 other.set( pos, _g ) ;
159 * Remove a node in the other nodes list.
160 * @param _g The node to be removed
162 public void removeOtherNode( GNode _g )
166 int pos = searchOther( _g ) ;
170 other.remove( pos ) ;
172 System.err.println( "This node does not exist in OtherNodes!" ) ;
179 * Remove a failed node from the mapping.
180 * @param _deadNode The failed node
181 * @return The task associated with the failed node
183 public GTask removeGNode( GNode _deadNode )
187 for( int i = 0 ; i < mapping.size() ; i++ )
189 if( mapping.get( i ).getGNode().getId() == _deadNode.getId() )
191 gt = mapping.get( i ).getGTask() ;
192 mapping.remove( i ) ;
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
206 public ArrayList<GNode> getMappedGNodes()
208 ArrayList<GNode> ar = new ArrayList<GNode>() ;
210 if( mapping.size() != 0 )
212 for( int i = 0 ; i < mapping.size() ; i++ )
214 ar.add( mapping.get( i ).getGNode() ) ;
223 * Print the status of the mapping done, according to its type.
225 public void print( int _type )
227 int type_print = _type ;
229 System.out.println();
230 System.out.println( "\t=> Mapping done:\n" ) ;
232 if( type_print == 0 )
234 ArrayList<GNode> ar = getMappedGNodes() ;
236 for( int i = 0 ; i < ar.size() ; i++ )
238 System.out.println( "Task " + i + " on " + ar.get( i ).getName() ) ;
241 System.out.println() ;
244 if( type_print == 1 )
246 for( int i = 0 ; i < mapping2.size() ; i++ )
248 System.out.print( "\t\tCluster \"" + mapping2.get( i ).getCluster().getName() + "\" => { ") ;
249 for( int j = 0 ; j < mapping2.get( i ).getGtask().size() ; j++ )
251 System.out.print( mapping2.get( i ).getGtask().get( j ).getNum() ) ;
253 if( j != mapping2.get( i ).getGtask().size() - 1 )
255 System.out.print( ", " ) ;
258 System.out.println( " } " ) ;
259 System.out.println() ;
266 * Return the mapping done.
267 * @return The mapping
269 public ArrayList<Association> getMapping()
276 * Return the position of the association containing
278 * @param _g The GNode to be search
279 * @return The position of the association
281 public int getIdOfAssociation( GNode _g )
285 for( int i = 0 ; i < mapping.size() ; i++ )
287 if( mapping.get( i ).getGNode().getId() == _g.getId() )
299 * Return the association of the given position.
300 * @param _id The position of the Association
301 * @return The Association requested
303 public Association getAssociation( int _id )
305 if( _id >= 0 && _id < mapping.size() )
307 return mapping.get( _id ) ;
315 * Remove the association of the given position.
316 * @param _id The position of the Association
317 * @return The Association removed
319 public Association removeAssociation( int _id )
321 if( _id >= 0 && _id < mapping.size() )
323 return mapping.remove( _id ) ;
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
335 public int getIdOfAssociation( int _taskRank )
339 for( int i = 0 ; i < mapping.size() ; i++ )
341 if( mapping.get( i ).getGTask().getNum() == _taskRank )
352 * Return the amount of external tasks dependencies, in cluster point of view.
353 * @return The amount of external dependencies
355 public int calcDepExt()
358 ArrayList<GTask> ar ;
359 ArrayList<GTask> deps ;
361 for( int i = 0 ; i < mapping.size() ; i++ )
363 ar = mapping.get(i).getGtask() ;
365 for( int j = 0 ; j < ar.size() ; j++ )
367 deps = ar.get(j).getDependencies() ;
369 for( int k = 0 ; k < deps.size() ; k++ )
371 if( ! ar.contains( deps.get(k) ) )
379 return ( depExt / 2 ) ;
384 /** La programmation est un art, respectons ceux qui la pratiquent !! **/