3 import java.util.ArrayList;
7 public class Maheve extends Algo
9 private static final long serialVersionUID = 1L;
11 private static final int minNode = 5 ;
12 private static final int nbSave = 2 ;
15 private ArrayList<Cluster> sortedCluster = null ;
16 private ArrayList<GTask> tasks = null ;
20 * Empty default constructor.
26 sortedCluster = new ArrayList<Cluster>() ;
31 * Constructor of this algorithm, which takes into parameter
32 * the application graph, the grid architecture, and the correction
33 * parameter which indicates the threshold of the heterogeneity degree.
34 * @param _gr The application graph
35 * @param _gl The grid architecture
36 * @param _threshold The heterogeneity threshold
38 public Maheve( Graph _gr, Grid _gl )
43 sortedCluster = new ArrayList<Cluster>() ;
44 tasks = new ArrayList<GTask>() ;
51 /** If the mapping is possible ... **/
52 if( ( gr != null ) && ( gr.getNbGTask() <= gl.getNbGNode() ) )
54 System.out.println( "******************************************" ) ;
55 System.out.println( "* Launching the MAHEVE Mapping algorithm *" ) ;
56 System.out.println( "******************************************\n\n" ) ;
58 /** Local variables **/
59 ArrayList<GNode> used = null ;
61 /** Initialization of heterogeneity degree **/
62 hd = gl.getHeterogenityDegre() ;
65 /** Ordering clusters according to their real power **/
66 updateSortedClusters() ;
68 /** Searching the corresponding nodes **/
69 used = searchNodes( gr.getNbGTask() ) ;
71 if( used == null || used.size() == 0 )
73 System.err.println( "No node returned!" ) ;
78 /** Ordering tasks **/
81 if( tasks == null || tasks.size() == 0 )
83 System.err.println( "Unable to retrieve tasks to be mapped on!" ) ;
88 /** Save the Mapping **/
89 for( int i = 0 ; i < tasks.size() ; i++ )
91 mp.addMapping( new Association( used.get( i ), tasks.get( i ) ) ) ;
95 System.err.println( "\n\n!!! Unable to map application!\n\n" ) ;
101 private void orderTasks()
103 ArrayList<GTask> l1 = sortTasks() ;
105 if( l1 != null && l1.size() > 0 )
107 ArrayList<GTask> l2 = new ArrayList<GTask>() ;
108 ArrayList<GTask> tmp = new ArrayList<GTask>() ;
110 while( l1.size() > 0 )
114 l2.add( l1.get( 0 ) ) ;
117 while( l2.size() > 0 )
119 l1.remove( l2.get( 0 ) ) ;
120 tmp = addTask( l2.remove( 0 ), l1 ) ;
122 for( int i = 0 ; i < tmp.size() ; i++ )
124 l2.add( tmp.get( i ) ) ;
132 private ArrayList<GTask> addTask(GTask _gt, ArrayList<GTask> _ar)
135 ArrayList<GTask> ret = null ;
137 if( _gt != null && _ar != null )
139 ret = new ArrayList<GTask>() ;
141 // ** Adding the task in the main list ** //
144 // ** Searching its dependencies ** //
145 int nbDep = (int) (_gt.getNbDep() * (1.0 - hd)) ;
149 ArrayList<Integer> dep = new ArrayList<Integer>() ;
151 for( int i = 0 ; i < _gt.getDependencies().size() ; i++ )
153 dep.add( _gt.getDependencies().get( i ).getNum() ) ;
156 for( int i = 0 ; i < _ar.size() ; i++ )
158 num = _ar.get( i ).getNum() ;
160 for( int j = 0 ; j < dep.size() ; j++ )
162 if( num == dep.get( j ) )
164 ret.add( _ar.remove( i ) ) ;
182 private ArrayList<GTask> sortTasks()
184 ArrayList<GTask> ret = null ;
186 ArrayList<GTask> tmp = gr.getGraph() ;
188 if( tmp != null && tmp.size() > 0 )
190 ret = new ArrayList<GTask>() ;
192 ArrayList<Double> mt = new ArrayList<Double>() ;
197 for( int i = 0 ; i < tmp.size() ; i++ )
199 W = tmp.get( i ).getWeight() ;
200 D = tmp.get( i ).getNbDep() ;
204 MT = Math.pow( W, hd ) * Math.pow( D, ( 1.0 - hd) ) ;
206 if( ret.size() == 0 )
208 ret.add( tmp.get( i ) ) ;
211 for( int j = 0 ; j < ret.size() ; j++ )
213 if( MT > mt.get( j ) )
215 ret.add( j, tmp.get( i ) ) ;
225 ret.add( tmp.get( i ) ) ;
236 private ArrayList<GNode> searchNodes( int _nbTask )
238 ArrayList<GNode> ret = null ;
242 ret = new ArrayList<GNode>() ;
247 for( int i = 0 ; i < sortedCluster.size() ; i++ )
249 /** If there is enough nodes ... **/
250 if( sortedCluster.get( i ).getNbFreeNodes() >= minNode )
254 max = sortedCluster.get( i ).getNbFreeNodes() - nbSave ;
256 for( int j = 0 ; j < max ; j++ )
258 g = sortedCluster.get( i ).nextGNode() ;
260 sortedCluster.get( i ).setGNodeStatus( g, true ) ;
264 if( nbFound >= _nbTask )
269 if( nbFound >= _nbTask )
279 * Sort clusters according to the heterogeneity degree of the platform and
280 * the eventual application's threshold.
282 private void updateSortedClusters()
286 /** Purging the local list **/
287 sortedCluster = null ;
288 sortedCluster = new ArrayList<Cluster>() ;
290 ArrayList<Double> calcMark = new ArrayList<Double>() ;
292 hd = gl.getHeterogenityDegre() ;
294 /** Sorting clusters **/
295 ArrayList<Cluster> tmp = gl.getClusters() ;
304 for( int i = 0 ; i < tmp.size() ; i++ )
306 N = tmp.get( i ).getNbFreeNodes() ;
307 P = tmp.get( i ).getAvailablePower() ;
310 /** The magic formula :P **/
311 calcLoc = Math.sqrt( locP * Math.pow((1.5 * hd + 0.3), 2) +
312 N * Math.pow((1.1 - hd), 2 ) ) ;
316 if( sortedCluster.size() == 0 )
318 sortedCluster.add( tmp.get( i ) ) ;
319 calcMark.add( calcLoc ) ;
322 for( int j = 0 ; j < sortedCluster.size() ; j++ )
324 if( calcLoc > calcMark.get( j ) )
326 sortedCluster.add( j, tmp.get( i ) ) ;
327 calcMark.add( j, calcLoc ) ;
335 sortedCluster.add( tmp.get( i ) ) ;
336 calcMark.add( calcLoc ) ;
345 public GNode replaceNode( GNode _dead, ArrayList<GNode> _ag )
349 /** If something has to be done **/
353 /** Any place on the same cluster ? **/
354 for( int i = 0 ; i < gl.getNbCluster() ; i++ )
358 int id = gl.getClusterOfNode( _dead ) ;
362 cl = gl.getClusters().get(id) ;
364 if( cl.getNbFreeNodes() > 0 )
366 ret = cl.nextGNode() ;
370 System.err.println( "Cluster not found!" ) ;
374 /** If there is no more place, try other clusters **/
377 updateSortedClusters() ;
379 for( int i = 0 ; i < sortedCluster.size() ; i++ )
381 if( sortedCluster.get( i ).getNbFreeNodes() > 0 )
383 ret = sortedCluster.get( i ).nextGNode() ;
398 public GNode getOtherGNode( ArrayList<GNode> _ag )
402 /** Searching the cluster which has the more free nodes **/
403 int pos = -1, max = 0, cur = 0 ;
404 for( int i = 0 ; i < gl.getNbCluster() ; i++ )
406 cur = gl.getClusters().get( i ).getNbFreeNodes() ;
414 ret = gl.getClusters().get( pos ).nextGNode() ;
421 /** La programmation est un art, respectons ceux qui la pratiquent !! **/