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 **/
62 for( int i = 0 ; i < at.size() ; i++ )
64 insertMapping( new Association( c.nextGNode(), at.get( i ) ) ) ;
70 * Add a mapping association in the general mapping.
71 * @param _a Association between a task and a node
73 public void addMapping( Association _a )
75 if( type == 0 || type == -1 )
79 System.err.println( "Mapping type mismatch !" ) ;
88 * Insert the association at the right place.
89 * @param _a The association to be inserted
91 public void insertMapping( Association _a )
93 if( _a != null && _a.getGNode() != null && _a.getGTask() != null )
95 // int ind = _a.getGTask().getNum() ;
97 // mapping.add( ind - 1, _a ) ;
104 * Remove a failed node from the mapping.
105 * @param _deadNode The failed node
106 * @return The task associated with the failed node
108 public GTask removeGNode( GNode _deadNode )
112 for( int i = 0 ; i < mapping.size() ; i++ )
114 if( mapping.get( i ).getGNode().getId() == _deadNode.getId() )
116 gt = mapping.get( i ).getGTask() ;
117 mapping.remove( i ) ;
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
131 public ArrayList<GNode> getMappedGNodes()
133 ArrayList<GNode> ar = new ArrayList<GNode>() ;
135 if( mapping.size() != 0 )
139 // ArrayList<Association> tmp = (ArrayList<Association>) mapping.clone() ;
141 for( int i = 0 ; i < mapping.size() ; i++ )
143 // for( int j = 0 ; j < tmp.size() ; j++ )
145 // if( tmp.get( j ).getGTask().getNum() == i )
147 ar.add( mapping.get( i ).getGNode() ) ;
149 // j = tmp.size() + 2 ;
157 // ArrayList<Association> tmp = (ArrayList<Association>) mapping2.clone() ;
159 // for( int i = 0 ; i < mapping2.size() ; i++ )
161 // for( int j = 0 ; j < tmp.size() ; j++ )
163 // if( tmp.get( j ).getGTask().getNum() == i )
165 // ar.add( tmp.get( j ).getGNode() ) ;
167 // j = tmp.size() + 2 ;
179 * Print the status of the mapping done, according to its type.
183 System.out.println();
184 System.out.println( "\t=> Mapping done :\n" ) ;
188 ArrayList<GNode> ar = getMappedGNodes() ;
190 for( int i = 0 ; i < ar.size() ; i++ )
192 System.out.println( "Task " + i + " on " + ar.get( i ).getName() ) ;
195 System.out.println() ;
200 for( int i = 0 ; i < mapping2.size() ; i++ )
202 System.out.print( "\t\tCluster \"" + mapping2.get( i ).getCluster().getName() + "\" => { ") ;
203 for( int j = 0 ; j < mapping2.get( i ).getGtask().size() ; j++ )
205 System.out.print( mapping2.get( i ).getGtask().get( j ).getNum() ) ;
207 if( j != mapping2.get( i ).getGtask().size() - 1 )
209 System.out.print( ", " ) ;
212 System.out.println( " } " ) ;
213 System.out.println() ;
220 * Return the mapping done.
221 * @return The mapping
223 public ArrayList<Association> getMapping()
230 * Return the position of the association containing
232 * @param _g The GNode to be search
233 * @return The position of the association
235 public int getIdOfAssociation( GNode _g )
239 for( int i = 0 ; i < mapping.size() ; i++ )
241 if( mapping.get( i ).getGNode().getId() == _g.getId() )
253 * Return the amount of external tasks dependencies, in cluster point of view.
254 * @return The amount of external dependencies
256 public int calcDepExt()
259 ArrayList<GTask> ar ;
260 ArrayList<GTask> deps ;
262 for( int i = 0 ; i < mapping.size() ; i++ )
264 ar = mapping.get(i).getGtask() ;
266 for( int j = 0 ; j < ar.size() ; j++ )
268 deps = ar.get(j).getDependencies() ;
270 for( int k = 0 ; k < deps.size() ; k++ )
272 if( ! ar.contains( deps.get(k) ) )
280 return ( depExt / 2 ) ;
285 /** La programmation est un art, respectons ceux qui la pratiquent !! **/