3 import java.util.ArrayList;
4 import java.util.Iterator;
8 public class Maheve extends Algo
10 private static final long serialVersionUID = 1L;
12 private static int minNode ;
13 private static int nbSave ;
16 private ArrayList<Cluster> sortedCluster = null ;
17 private ArrayList<GTask> tasks = null ;
21 * Empty default constructor.
33 * Constructor of this algorithm, which takes into parameter
34 * the application graph, the grid architecture, and the correction
35 * parameter which indicates the threshold of the heterogeneity degree.
36 * @param _gr The application graph
37 * @param _gl The grid architecture
38 * @param _threshold The heterogeneity threshold
40 public Maheve( Graph _gr, Grid _gl )
45 sortedCluster = new ArrayList<Cluster>() ;
46 tasks = new ArrayList<GTask>() ;
52 * Setter for the amount of nodes to preserve on each cluster
53 * for the fault tolerance (should be 0 in an environment without fault).
54 * @param _nbSave The amount of nodes to preserve
56 public void setNbSave( int _nbSave )
66 * Getter to retrieving the amount of nodes preserved for fault tolerance.
67 * @return The amount of preserved nodes
69 public int getNbSave()
76 * Setter for the minimum amount of nodes which should be present in
77 * a cluster to consider this latter in the mapping process.
78 * @param _minNode The minimum amount of nodes a cluster should have to be
81 public void setMinNode( int _minNode )
91 * Getter to retrieve the minimum amount of nodes a cluster should have
92 * to be considered in the mapping process.
93 * @return The minimum amount of nodes a cluster should have to be
96 public int getMinNode()
104 /** If the mapping is possible ... **/
105 if( ( gr != null ) && ( gr.getNbGTask() <= gl.getNbFreenodes() ) )
107 System.out.println( "******************************************" ) ;
108 System.out.println( "* Launching the MAHEVE Mapping algorithm *" ) ;
109 System.out.println( "******************************************\n\n" ) ;
111 /** Initialization of heterogeneity degree (hd) **/
112 double hd_g = gl.getHeterogenityDegre() ;
114 /* Correction of hd */
115 // correction is function of the application and platform characteristics
116 hd = calcNewHd( hd_g ) ;
118 System.out.println( "Corrected hd value: " + hd + " (" + hd_g + ")" ) ;
121 /** Ordering clusters according to their real power **/
122 updateSortedClusters() ;
125 /** Ordering tasks **/
128 if( tasks == null || tasks.size() == 0 )
130 System.err.println( "Unable to retrieve tasks to be mapped on!" ) ;
134 /** Mapping of tasks on nodes/clusters **/
138 System.err.println( "\n\n!!! Unable to map application!\n\n" ) ;
144 private void mapping()
151 ArrayList<Cluster> cl = new ArrayList<Cluster>() ;
153 for( int i = 0 ; i < sortedCluster.size() ; i++ )
155 cl.add( sortedCluster.get( i ).clone() ) ;
158 for( int i = 0 ; i < tasks.size() ; i++ )
164 nbDep = (int) ( tasks.get( i ).getNbDep() * (1.0 - hd) / div ) + 1 ;
168 if( (cl.get( ind ).getNbFreeNodes() - nbSave) >= nbDep )
170 GNode gn = cl.get( ind ).getBetterFreeGNode() ;
173 cl.get( ind ).setGNodeStatus( gn, true ) ;
174 mp.addMapping( (gl.getCluster( cl.get( ind ).getName()).exists( gn )) , tasks.get( i ) ) ;
187 if( ind == cl.size() )
198 private void orderTasks()
200 ArrayList<GTask> l1 = sortTasks() ;
202 ArrayList<Integer> st = new ArrayList<Integer>() ;
204 if( l1 != null && l1.size() > 0 )
206 ArrayList<GTask> l2 = new ArrayList<GTask>() ;
207 ArrayList<GTask> tmp = new ArrayList<GTask>() ;
209 while( l1.size() > 0 )
213 l2.add( l1.get( 0 ) ) ;
214 st.add( l1.get( 0 ).getNum() ) ;
217 while( l2.size() > 0 )
219 l1.remove( l2.get( 0 ) ) ;
220 tmp = addTask( l2.remove( 0 ), l1, st ) ;
222 for( int i = 0 ; i < tmp.size() ; i++ )
224 l2.add( tmp.get( i ) ) ;
225 st.add( tmp.get( i ).getNum() ) ;
233 private ArrayList<GTask> addTask(GTask _gt, ArrayList<GTask> _ar,
234 ArrayList<Integer> _st )
237 ArrayList<GTask> ret = null ;
239 if( _gt != null && _ar != null )
241 ret = new ArrayList<GTask>() ;
243 // ** Adding the task in the main list ** //
246 // ** Searching its dependencies ** //
247 int nbDep = (int) ( _gt.getNbDep() * (1.0 - hd) + 1 ) ;
251 ArrayList<Integer> dep = new ArrayList<Integer>() ;
253 // ** Retrieving dependencies and eliminating tasks already treated
254 // ** or in instance to be treated. **//
255 for( int i = 0 ; i < _gt.getDependencies().size() ; i++ )
257 if( ! _st.contains( _gt.getDependencies().get( i ).getNum() ) )
259 dep.add( _gt.getDependencies().get( i ).getNum() ) ;
264 // ** Searching dependencies in sorted tasks list ** //
265 Iterator<GTask> iter = _ar.iterator() ;
266 while( iter.hasNext() )
268 GTask gt = iter.next() ;
271 if( dep.contains( num ) )
289 private ArrayList<GTask> sortTasks()
291 ArrayList<GTask> ret = null ;
292 double maxW = 0, maxD = 0 ;
294 ArrayList<GTask> tmp = gr.getGraph() ;
296 maxD = gr.getMaxDep() ;
298 for( int i = 0 ; i < gr.getNbGTask() ; i++ )
300 if( gr.getGraph().get( i ).getWeight() > maxW )
302 maxW = gr.getGraph().get( i ).getWeight() ;
306 if( tmp != null && tmp.size() > 0 )
308 ret = new ArrayList<GTask>() ;
310 ArrayList<Double> mt = new ArrayList<Double>() ;
315 for( int i = 0 ; i < tmp.size() ; i++ )
317 W = tmp.get( i ).getWeight() / maxW ;
318 D = tmp.get( i ).getNbDep() / maxD ;
322 MT = Math.pow( W, hd ) + Math.pow( D, ( 1.0 - hd) ) ;
324 if( ret.size() == 0 )
326 ret.add( tmp.get( i ) ) ;
329 for( int j = 0 ; j < ret.size() ; j++ )
331 if( MT > mt.get( j ) )
333 ret.add( j, tmp.get( i ) ) ;
343 ret.add( tmp.get( i ) ) ;
355 * Sort clusters according to the heterogeneity degree of the platform and
356 * the eventual application's threshold.
358 private void updateSortedClusters()
362 /** Purging the local list **/
363 sortedCluster = null ;
364 sortedCluster = new ArrayList<Cluster>() ;
367 /** Sorting clusters **/
368 ArrayList<Cluster> tmp = gl.getClusters() ;
370 double[] marks = new double[ tmp.size() ] ;
377 double Nm = 0, Pm = 0 ;
382 for( i = 0 ; i < tmp.size() ; i++ )
384 Nm += tmp.get( i ).getNbFreeNodes() ;
385 Pm += tmp.get( i ).getAvgAvailablePower() ;
389 for( i = 0 ; i < tmp.size() ; i++ )
391 normN = tmp.get( i ).getNbFreeNodes() * 100 / Nm ;
392 normP = tmp.get( i ).getAvgAvailablePower() * 100 / Pm ;
394 /** The magic formula :P **/
395 calcLoc = Math.pow( normP, hd) +
396 Math.pow( normN, (1 - hd) ) ;
398 marks[ i ] = calcLoc ;
401 // Taking into account the network latency
402 ArrayList<Couple> couples = new ArrayList<Couple>() ;
405 for( i = 0 ; i < tmp.size() - 1 ; i++ )
407 for( int j = i+1 ; j < tmp.size() ; j++ )
409 couples.add( new Couple( tmp.get( i ), marks[i],
410 tmp.get( j ), marks[j] ) ) ;
414 couples.add( new Couple( tmp.get(0), marks[0], null, -1 ) ) ;
419 ArrayList<Couple> Scouples = new ArrayList<Couple>() ;
420 for( i = 0 ; i < couples.size() ; i++ )
424 if( Scouples.size() == 0 )
426 Scouples.add( couples.get( i ) ) ;
429 for( int j = 0 ; j < Scouples.size() ; j++ )
431 if( couples.get( i ).getCoupleMark() > Scouples.get( j ).getCoupleMark() )
433 Scouples.add( j, couples.get( i ) ) ;
441 Scouples.add( couples.get( i ) ) ;
446 // Extracting clusters
447 for( i = 0 ; i < Scouples.size() ; i++ )
449 if( ! sortedCluster.contains( Scouples.get(i).getBetterCluster() ) )
451 sortedCluster.add( Scouples.get( i ).getBetterCluster() ) ;
454 if( Scouples.get( i ).getOtherCluster() != null &&
455 ! sortedCluster.contains( Scouples.get(i).getOtherCluster() ))
457 sortedCluster.add( Scouples.get( i ).getOtherCluster() ) ;
469 * Compute the new value of hd, by taking care of the application
470 * and execution architecture characteristics.
471 * @param hd_g Original heterogeneity degree
472 * @return The new (corrected) heterogeneity degree
474 private double calcNewHd( double hd_g )
479 double nbTask = gr.getNbGTask() ;
480 double avgNodesCluster = gl.getAvgClusterNode() ;
481 double nbAvgDep = gr.getAverageDep() ;
484 double correc_appli = 1 - ((nbTask / nbAvgDep +1) / nbTask) ;
485 double correc_archi = 1 - (( avgNodesCluster / (nbAvgDep + 1) ) / avgNodesCluster) ;
486 System.out.println( correc_appli + " " +correc_archi ) ;
488 nhd = hd_g + (correc_appli - 0.5) + (correc_archi - 0.5) ;
490 if( nhd >= 1 ) nhd = 0.99 ;
491 if( nhd <= 0 ) nhd = 0.01 ;
499 public GNode replaceNode( GNode _dead, ArrayList<GNode> _ag )
503 /** If something has to be done **/
507 /** Any place on the same cluster ? **/
508 for( int i = 0 ; i < gl.getNbCluster() ; i++ )
512 int id = gl.getClusterOfNode( _dead ) ;
516 cl = gl.getClusters().get(id) ;
518 if( cl.getNbFreeNodes() > 0 )
520 ret = cl.nextGNode() ;
524 System.err.println( "Cluster not found!" ) ;
528 /** If there is no more place, try other clusters **/
531 updateSortedClusters() ;
533 for( int i = 0 ; i < sortedCluster.size() ; i++ )
535 if( sortedCluster.get( i ).getNbFreeNodes() > 0 )
537 ret = sortedCluster.get( i ).nextGNode() ;
546 System.err.println( "No repalacing node found! No left places on any cluster!" ) ;
557 public GNode getOtherGNode( ArrayList<GNode> _ag )
561 /** Searching the cluster which has the more free nodes **/
562 int pos = -1, max = 0, cur = 0 ;
563 for( int i = 0 ; i < gl.getNbCluster() ; i++ )
565 cur = gl.getClusters().get( i ).getNbFreeNodes() ;
573 ret = gl.getClusters().get( pos ).nextGNode() ;
580 public boolean setParams(Object[] _params) {
591 Couple( Cluster _c1, double _m1, Cluster _c2, double _m2 )
593 c1 = _c1 ; m1 = _m1 ;
594 c2 = _c2 ; m2 = _m2 ;
604 protected double getCoupleMark()
612 g1 = c1.getGNodes().get( i ) ;
613 g2 = c2.getGNodes().get( i ) ;
615 if( g1 == null || g2 == null )
617 System.err.println( "There is no more node in at least one cluster" ) ;
621 d = Math.pow(gl.getDistance( g1, g2 ), (1- hd)) ;
625 return ( m1 + m2 ) / d ;
628 protected Cluster getBetterCluster()
643 protected Cluster getOtherCluster()
661 /** La programmation est un art, respectons ceux qui la pratiquent !! **/