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
29 mapping = new ArrayList<Association>() ;
30 mapping2 = new ArrayList<Association>() ;
31 other = new ArrayList<GNode>() ;
38 * Initialization of the Mapping variables
40 public void initMapping()
42 mapping = new ArrayList<Association>() ;
43 mapping2 = new ArrayList<Association>() ;
44 other = new ArrayList<GNode>() ;
51 * Set the grid in which the mapping is done.
54 public void setGrid( Grid _gd )
61 * Add in the mapping an association between a cluster and tasks set.
62 * @param c Cluster of the association
63 * @param at Tasks set to be associated
65 public void addMapping( Cluster c, ArrayList<GTask> at )
67 mapping2.add( new Association( c, at ) ) ;
69 if( type == 1 || type == -1 )
73 System.err.println( "Mapping type mismatch !" ) ;
77 /** For the usage of algorithms which map groups of tasks on cluster **/
80 for( int i = 0 ; i < at.size() ; i++ )
85 insertMapping( new Association( tmp, at.get( i ) ), false ) ;
87 System.err.println( "Error during reception of the next GNode !" ) ;
95 * Add a mapping association between a task and a node
96 * in the general mapping.
97 * @param _gn The node on which the task is mapped
98 * @param _gt The task mapped on the node
100 public void addMapping( GNode _gn, GTask _gt )
102 if( type == 0 || type == -1 )
106 System.err.println( "Mapping type mismatch !" ) ;
110 insertMapping( new Association( _gn, _gt ), true ) ;
115 * Insert the association at the right place.
116 * @param _a The association to be inserted
118 public void insertMapping( Association _a, boolean _other )
120 if( _a != null && _a.getGNode() != null && _a.getGTask() != null )
126 updateMapping2( _a ) ;
132 private void updateMapping2( Association _a )
136 for( int i = 0 ; i < mapping2.size() ; i++ )
138 if( mapping2.get( i ).getCluster().getName().equalsIgnoreCase( _a.getGNode().getClusterName() ) )
140 mapping2.get( i ).getGtasks().add( _a.getGTask() ) ;
149 Cluster cl = _a.getGNode().getCluster() ;
150 ArrayList<GTask> gr = new ArrayList<GTask>() ;
151 gr.add( _a.getGTask() ) ;
152 mapping2.add( new Association( cl, gr ) ) ;
158 * Determine if a node is used as an other node in the mapping.
159 * @param _g The node to search
160 * @return The position of the node
162 private int searchOther( GNode _g )
168 for( int i = 0 ; i < other.size() ; i++ )
170 if( _g.getId() == other.get( i ).getId() )
183 * Add a new node in the other nodes list.
184 * @param _g The other node
186 public void addOtherNode( GNode _g )
190 int pos = searchOther( _g ) ;
196 System.out.println( "This node already exist in OtherNodes! " +
198 other.set( pos, _g ) ;
205 * Remove a node in the other nodes list.
206 * @param _g The node to be removed
208 public void removeOtherNode( GNode _g )
212 int pos = searchOther( _g ) ;
216 other.remove( pos ) ;
218 System.err.println( "This node does not exist in OtherNodes!" ) ;
225 * Remove a failed node from the mapping.
226 * @param _deadNode The failed node
227 * @return The task associated with the failed node
229 public GTask removeGNode( GNode _deadNode )
233 for( int i = 0 ; i < mapping.size() ; i++ )
235 if( mapping.get( i ).getGNode().getId() == _deadNode.getId() )
237 gt = mapping.get( i ).getGTask() ;
238 mapping.remove( i ) ;
248 * Return the list of GNodes on which tasks are mapped, in order
249 * of the task number.
250 * @return The ordered list, according to the GTasks id, of GNodes involved in the mapping
252 public ArrayList<GNode> getMappedGNodes()
254 ArrayList<GNode> ar = new ArrayList<GNode>() ;
256 if( mapping.size() != 0 )
258 ArrayList<Association> mp = organizeMapping() ;
260 for( int i = 0 ; i < mp.size() ; i++ )
262 ar.add( mp.get( i ).getGNode() ) ;
270 private ArrayList<Association> organizeMapping()
272 ArrayList<Association> ret = null ;
274 if( mapping.size() > 0 )
276 ret = new ArrayList<Association>( mapping.size() ) ;
277 for( int i = 0 ; i < mapping.size() ; i++ )
280 for( int i = 0 ; i < mapping.size() ; i++ )
282 ret.set( mapping.get( i ).getGTask().getNum(), mapping.get( i ) ) ;
285 System.out.println( "No mapping..." ) ;
293 * Print the status of the mapping done, according to its type.
295 public void print( int _type )
297 int type_print = _type ;
299 System.out.println();
300 System.out.println( "\t=> Mapping done:\n" ) ;
302 if( type_print == 0 )
304 ArrayList<GNode> ar = getMappedGNodes() ;
306 for( int i = 0 ; i < ar.size() ; i++ )
308 System.out.println( "Task " + i + " on " + ar.get( i ).getName() ) ;
311 System.out.println() ;
314 if( type_print == 1 )
316 for( int i = 0 ; i < mapping2.size() ; i++ )
318 System.out.print( "\t\tCluster \"" + mapping2.get( i ).getCluster().getName() + "\" => { ") ;
319 for( int j = 0 ; j < mapping2.get( i ).getGtasks().size() ; j++ )
321 System.out.print( mapping2.get( i ).getGtasks().get( j ).getNum() ) ;
323 if( j != mapping2.get( i ).getGtasks().size() - 1 )
325 System.out.print( ", " ) ;
328 System.out.println( " } " ) ;
329 System.out.println() ;
336 * Return the mapping done.
337 * @return The mapping
339 public ArrayList<Association> getMapping()
346 * Return the position of the association containing
348 * @param _g The GNode to be search
349 * @return The position of the association
351 public int getIdOfAssociation( GNode _g )
355 for( int i = 0 ; i < mapping.size() ; i++ )
357 if( mapping.get( i ).getGNode().getId() == _g.getId() )
369 * Return the association of the given position.
370 * @param _id The position of the Association
371 * @return The Association requested
373 public Association getAssociation( int _id )
375 if( _id >= 0 && _id < mapping.size() )
377 return mapping.get( _id ) ;
385 * Remove the association of the given position.
386 * @param _id The position of the Association
387 * @return The Association removed
389 public Association removeAssociation( int _id )
391 if( _id >= 0 && _id < mapping.size() )
393 return mapping.remove( _id ) ;
400 * Return the position of the association containing
401 * the GTask of a specified rank.
402 * @param _taskRank The rank of the task
403 * @return The position of the association
405 public int getIdOfAssociation( int _taskRank )
409 for( int i = 0 ; i < mapping.size() ; i++ )
411 if( mapping.get( i ).getGTask().getNum() == _taskRank )
422 * Return the amount of external tasks dependencies, in cluster point of view.
423 * @return The amount of external dependencies
425 public int calcDepExt()
429 ArrayList<Association> mp = organizeMapping() ;
431 for( int i = 0 ; i < mp.size() ; i++ )
433 ArrayList<Integer> dids = mp.get( i ).getGTask().getDependenciesIds() ;
435 for( int j = 0 ; j < dids.size() ; j++ )
437 if( ! mp.get( i ).getGNode().getSiteName().equalsIgnoreCase(
438 mp.get( dids.get(j) ).getGNode().getSiteName() ) )
445 return ( depExt / 2 ) ;
450 * ** TO BE MODIFIED !!!
453 public double calcAsyncMark()
457 ArrayList<Double> comput = new ArrayList<Double>() ;
458 ArrayList<Double> comput2 = new ArrayList<Double>() ;
459 double max_time = 0 ;
461 ArrayList<Association> mp = organizeMapping() ;
463 /** Initialization **/
464 for( int i = 0 ; i < mp.size() ; i++ )
466 Double calc = new Double( mp.get( i ).getGTask().getWeight() /
467 mp.get( i ).getGNode().getPower() ) ;
470 comput2.add( new Double ( -1 ) ) ;
472 if( calc > max_time )
478 /** Normalization **/
479 for( int i = 0 ; i < mp.size() ; i++ )
481 comput.set( i, comput.get( i ) / max_time ) ;
485 for( int k = 0 ; k < 2 ; k++ )
486 for( int i = 0 ; i < mp.size() ; i++ )
488 ArrayList<Integer> tmp = mp.get(i).getGTask().getDependenciesIds() ;
493 for( int j = 0 ; j < tmp.size() ; j++ )
495 if( comput2.get( j ) != -1 )
497 valv = comput2.get( j ) ;
499 valv = comput.get( j ) ;
502 calc += ( valv + ( gd.getDistance( mp.get( i ).getGNode(), mp.get( j ).getGNode() )
503 / gd.getMaxDistance() ) ) ;
506 comput2.set( i, comput.get( i ) * calc ) ;
510 for( int i = 0 ; i < comput2.size() ; i++ )
512 if( mark < comput2.get( i ) )
514 mark = comput2.get( i ) ;
523 /** La programmation est un art, respectons ceux qui la pratiquent !! **/