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 int type ; // 0 : mapping task/node ; 1 : mapping tasks/cluster
27 mapping = new ArrayList<Association>() ;
28 mapping2 = new ArrayList<Association>() ;
34 * Initialization of the Mapping variables
36 public void initMapping()
38 mapping = new ArrayList<Association>() ;
39 mapping2 = new ArrayList<Association>() ;
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
49 public void addMapping( Cluster c, ArrayList<GTask> at )
51 mapping2.add( new Association( c, at ) ) ;
53 if( type == 1 || type == -1 )
57 System.err.println( "Mapping type mismatch !" ) ;
61 /** For the usage of algorithms which map groups of tasks on cluster **/
63 for( int i = 0 ; i < at.size() ; i++ )
68 insertMapping( new Association( tmp, at.get( i ) ) ) ;
70 System.err.println( "Error during reception of the next GNode !" ) ;
78 * Add a mapping association in the general mapping.
79 * @param _a Association between a task and a node
81 public void addMapping( Association _a )
83 if( type == 0 || type == -1 )
87 System.err.println( "Mapping type mismatch !" ) ;
96 * Insert the association at the right place.
97 * @param _a The association to be inserted
99 public void insertMapping( Association _a )
101 if( _a != null && _a.getGNode() != null && _a.getGTask() != null )
103 // int ind = _a.getGTask().getNum() ;
105 // mapping.add( ind - 1, _a ) ;
112 * Remove a failed node from the mapping.
113 * @param _deadNode The failed node
114 * @return The task associated with the failed node
116 public GTask removeGNode( GNode _deadNode )
120 for( int i = 0 ; i < mapping.size() ; i++ )
122 if( mapping.get( i ).getGNode().getId() == _deadNode.getId() )
124 gt = mapping.get( i ).getGTask() ;
125 mapping.remove( i ) ;
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
139 public ArrayList<GNode> getMappedGNodes()
141 ArrayList<GNode> ar = new ArrayList<GNode>() ;
143 if( mapping.size() != 0 )
147 // ArrayList<Association> tmp = (ArrayList<Association>) mapping.clone() ;
149 for( int i = 0 ; i < mapping.size() ; i++ )
151 // for( int j = 0 ; j < tmp.size() ; j++ )
153 // if( tmp.get( j ).getGTask().getNum() == i )
155 ar.add( mapping.get( i ).getGNode() ) ;
157 // j = tmp.size() + 2 ;
165 // ArrayList<Association> tmp = (ArrayList<Association>) mapping2.clone() ;
167 // for( int i = 0 ; i < mapping2.size() ; i++ )
169 // for( int j = 0 ; j < tmp.size() ; j++ )
171 // if( tmp.get( j ).getGTask().getNum() == i )
173 // ar.add( tmp.get( j ).getGNode() ) ;
175 // j = tmp.size() + 2 ;
187 * Print the status of the mapping done, according to its type.
191 System.out.println();
192 System.out.println( "\t=> Mapping done:\n" ) ;
196 ArrayList<GNode> ar = getMappedGNodes() ;
198 for( int i = 0 ; i < ar.size() ; i++ )
200 System.out.println( "Task " + i + " on " + ar.get( i ).getName() ) ;
203 System.out.println() ;
208 for( int i = 0 ; i < mapping2.size() ; i++ )
210 System.out.print( "\t\tCluster \"" + mapping2.get( i ).getCluster().getName() + "\" => { ") ;
211 for( int j = 0 ; j < mapping2.get( i ).getGtask().size() ; j++ )
213 System.out.print( mapping2.get( i ).getGtask().get( j ).getNum() ) ;
215 if( j != mapping2.get( i ).getGtask().size() - 1 )
217 System.out.print( ", " ) ;
220 System.out.println( " } " ) ;
221 System.out.println() ;
228 * Return the mapping done.
229 * @return The mapping
231 public ArrayList<Association> getMapping()
238 * Return the position of the association containing
240 * @param _g The GNode to be search
241 * @return The position of the association
243 public int getIdOfAssociation( GNode _g )
247 for( int i = 0 ; i < mapping.size() ; i++ )
249 if( mapping.get( i ).getGNode().getId() == _g.getId() )
261 * Return the amount of external tasks dependencies, in cluster point of view.
262 * @return The amount of external dependencies
264 public int calcDepExt()
267 ArrayList<GTask> ar ;
268 ArrayList<GTask> deps ;
270 for( int i = 0 ; i < mapping.size() ; i++ )
272 ar = mapping.get(i).getGtask() ;
274 for( int j = 0 ; j < ar.size() ; j++ )
276 deps = ar.get(j).getDependencies() ;
278 for( int k = 0 ; k < deps.size() ; k++ )
280 if( ! ar.contains( deps.get(k) ) )
288 return ( depExt / 2 ) ;
293 /** La programmation est un art, respectons ceux qui la pratiquent !! **/