Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Improvement of the Maheve algorithm.
[mapping.git] / src / and / Mapping / Maheve.java
1 package and.Mapping;
2
3 import java.util.ArrayList;
4
5
6
7 public class Maheve extends Algo
8 {
9         private static final long serialVersionUID = 1L;
10
11         private static int minNode ;
12         private static int nbSave ;
13         
14         private double hd ;
15         private ArrayList<Cluster> sortedCluster = null ;
16         private ArrayList<GTask> tasks = null ;
17         
18                 
19         /**
20          * Empty default constructor.
21          */
22         public Maheve()
23         {
24                 super() ;
25                 name = "MAHEVE_2" ;
26                 minNode = 5 ;
27                 nbSave = 2 ;
28                 sortedCluster = new ArrayList<Cluster>() ;
29         }
30         
31         
32         /**
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
39          */
40         public Maheve( Graph _gr, Grid _gl )
41         {
42                 super( _gr, _gl ) ;
43                 
44                 hd = 0 ;
45                 sortedCluster = new ArrayList<Cluster>() ;
46                 tasks = new ArrayList<GTask>() ;
47                 name = "MAHEVE_2" ;
48         }
49         
50         
51         /**
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
55          */
56         public void setNbSave( int _nbSave )
57         {
58                 if( _nbSave >= 0 )
59                 {
60                         nbSave = _nbSave ;
61                 }
62         }
63         
64         
65         /**
66          * Getter to retrieving the amount of nodes preserved for fault tolerance.
67          * @return The amount of preserved nodes
68          */
69         public int getNbSave()
70         {
71                 return nbSave ;
72         }
73         
74         
75         /**
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
79          * considered
80          */
81         public void setMinNode( int _minNode )
82         {
83                 if( _minNode >= 0 )
84                 {
85                         minNode = _minNode ;
86                 }
87         }
88         
89         
90         /**
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 
94          * considered
95          */
96         public int getMinNode()
97         {
98                 return minNode ;
99         }
100         
101         
102         @Override
103         public void map() {
104                 /** If the mapping is possible ... **/
105                 if( ( gr != null ) && ( gr.getNbGTask() <= gl.getNbGNode() ) )
106                 {
107                         System.out.println( "******************************************" ) ;
108                         System.out.println( "* Launching the MAHEVE Mapping algorithm *" ) ;
109                         System.out.println( "******************************************\n\n" ) ;
110                         
111                         /** Local variables **/
112                         ArrayList<GNode> used = null ;
113                         
114                         /** Initialization of heterogeneity degree **/
115                         hd = gl.getHeterogenityDegre() ;
116                         
117                         
118                         /** Ordering clusters according to their real power **/
119                         updateSortedClusters() ;
120                 
121                         /** Searching the corresponding nodes **/
122                         used = searchNodes( gr.getNbGTask() ) ;
123                         
124                         if( used == null || used.size() == 0 )
125                         {
126                                 System.err.println( "No node returned!" ) ;
127                                 return ;
128                         }
129                 
130                         
131                         /** Ordering tasks **/
132                         orderTasks() ;
133                         
134                         if( tasks == null || tasks.size() == 0 )
135                         {
136                                 System.err.println( "Unable to retrieve tasks to be mapped on!" ) ;
137                                 return ;
138                         }
139                         
140                         
141                         /** Save the Mapping **/
142                         for( int i = 0 ; i < tasks.size() ; i++ )
143                         {
144                                 mp.addMapping( new Association( used.get( i ), tasks.get( i ) ) ) ;
145                         }
146                 
147                 } else {
148                         System.err.println( "\n\n!!! Unable to map application!\n\n" ) ;
149                         return ;
150                 }
151         }
152
153         
154         private void orderTasks()
155         {               
156                 ArrayList<GTask> l1 = sortTasks() ;
157                 
158                 ArrayList<Integer> st = new ArrayList<Integer>() ;
159                 
160                 if( l1 != null && l1.size() > 0 )
161                 {                       
162                         ArrayList<GTask> l2 = new ArrayList<GTask>() ;
163                         ArrayList<GTask> tmp = new ArrayList<GTask>() ;
164                         
165                         while( l1.size() > 0 )
166                         {
167                                 if( l2.size() == 0 )
168                                 {
169                                         l2.add( l1.get( 0 ) ) ;
170                                         st.add( l1.get( 0 ).getNum() ) ;
171                                 }
172                                 
173                                 while( l2.size() > 0 )
174                                 {
175                                         l1.remove( l2.get( 0 ) ) ;
176                                         tmp = addTask( l2.remove( 0 ), l1, st ) ;
177                                         
178                                         for( int i = 0 ; i < tmp.size() ; i++ )
179                                         {
180                                                 l2.add( tmp.get( i ) ) ;
181                                                 st.add( tmp.get( i ).getNum() ) ;
182                                         }
183                                 }
184                         }
185                 }
186         }
187         
188         
189         private ArrayList<GTask> addTask(GTask _gt, ArrayList<GTask> _ar, 
190                                                                          ArrayList<Integer> _st )
191         {
192         
193                 ArrayList<GTask> ret = null ;
194                 
195                 if( _gt != null && _ar != null )
196                 {
197                         ret = new ArrayList<GTask>() ;
198                         
199                         // ** Adding the task in the main list ** //
200                         tasks.add( _gt ) ;
201                         
202                         // ** Searching its dependencies ** //
203                         int nbDep = (int) (_gt.getNbDep() * (1.0 - hd)) ;
204                         int cmpt = 0 ;
205                         int num = 0 ;
206                         
207                         ArrayList<Integer> dep = new ArrayList<Integer>() ;
208                         
209                         // ** Retrieving dependencies and eliminating tasks already treated 
210                         // ** or in instance to be treated. **//
211                         for( int i = 0 ; i < _gt.getDependencies().size() ; i++ )
212                         {
213                                 if( ! _st.contains( _gt.getDependencies().get( i ).getNum() ) )
214                                 {
215                                         dep.add( _gt.getDependencies().get( i ).getNum() ) ;
216                                 }
217                         }
218                         
219
220                         // ** Searching dependencies in sorted tasks list ** //
221                         for( int i = 0 ; i < _ar.size() ; i++ )
222                         {
223                                 num = _ar.get( i ).getNum() ;
224                                 
225                                 if( dep.contains( num ) ) 
226                                 {
227 //                              for( int j = 0 ; j < dep.size() ; j++ )
228 //                              {
229 //                                      if( num == dep.get( j ) )
230 //                                      {
231                                                 ret.add( _ar.remove( i ) ) ;
232                                                 cmpt++ ;
233 //                                              dep.remove( j ) ;
234 //                                              break ;
235 //                                      }
236                                 }
237                                 
238                                 if( cmpt == nbDep )
239                                 {
240                                         break ;
241                                 }
242                         }
243                 }
244                 
245                 return ret;
246         }
247
248
249         private ArrayList<GTask> sortTasks()
250         {
251                 ArrayList<GTask> ret = null ;
252                 
253                 ArrayList<GTask> tmp = gr.getGraph() ;
254                 
255                 if( tmp != null && tmp.size() > 0 )
256                 {
257                         ret = new ArrayList<GTask>() ;
258                         
259                         ArrayList<Double> mt = new ArrayList<Double>() ;
260                         
261                         double W, D, MT ;
262                         boolean ok = false ;
263                         
264                         for( int i = 0 ; i < tmp.size() ; i++ )
265                         {
266                                 W = tmp.get( i ).getWeight() ;
267                                 D = tmp.get( i ).getNbDep() ;
268                                 
269                                 ok = false ;
270                                 
271                                 MT = Math.pow( W, hd ) * Math.pow( D, ( 1.0 - hd) ) ;
272                                 
273                                 if( ret.size() == 0 )
274                                 {
275                                         ret.add( tmp.get( i ) ) ;
276                                         mt.add( MT ) ;
277                                 } else {
278                                         for( int j = 0 ; j < ret.size() ; j++ )
279                                         {
280                                                 if( MT > mt.get( j ) )
281                                                 {
282                                                         ret.add( j, tmp.get( i ) ) ;
283                                                         mt.add( j, MT ) ;
284                                                         
285                                                         ok = true ;
286                                                         break ;
287                                                 }
288                                         }
289                                         
290                                         if( ! ok )
291                                         {
292                                                 ret.add( tmp.get( i ) ) ;
293                                                 mt.add( MT ) ;
294                                         }
295                                 }
296                         }
297                 }
298                         
299                 return ret ;
300         }
301
302
303         private ArrayList<GNode> searchNodes( int _nbTask ) 
304         {
305                 ArrayList<GNode> ret = null ;
306                 
307                 if( _nbTask > 0 )
308                 {
309                         ret = new ArrayList<GNode>() ;
310                         int nbFound = 0 ;
311                         int max = 0 ;
312                         GNode g = null ;
313                         boolean changeParameter = false ;
314                         
315                         while( nbFound < _nbTask )
316                         {
317                                 int i = 0 ;
318                                 
319                                 for( i = 0 ; i < sortedCluster.size() ; i++ )
320                                 {
321                                         /** If there is enough nodes ... **/
322                                         if( sortedCluster.get( i ).getNbFreeNodes() >= minNode )
323                                         {
324                                                 max = 0 ;
325                                                 sortedCluster.get( i ).initMoreGNode() ;
326                                         
327                                                 max = sortedCluster.get( i ).getNbFreeNodes() - nbSave ;
328                                         
329                                                 for( int j = 0 ; j < max ; j++ )
330                                                 {
331                                                         g = sortedCluster.get( i ).moreGNode() ;
332                                                         ret.add( g ) ;
333                                                 
334                                                         nbFound ++ ;
335                                                 
336                                                         if( nbFound >= _nbTask )
337                                                                 break ;
338                                                 }
339                                         }
340                                 
341                                         if( nbFound >= _nbTask )
342                                                 break ;
343                                 }
344                                 
345                                 if( i == sortedCluster.size() && nbFound < _nbTask )
346                                 {
347                                         changeParameter = true ;
348                                         minNode-- ;
349                                 }
350                         }
351                         
352                         if( changeParameter )
353                         {
354                                 System.err.println( "The parameter \"minNode\" has been reduced " +
355                                                 "to allow the mapping process to be done." ) ;
356                         }
357                 }
358                 
359                 return ret ;
360         }
361
362
363         /**
364          * Sort clusters according to the heterogeneity degree of the platform and
365          * the eventual application's threshold. 
366          */
367         private void updateSortedClusters() 
368         {
369                 if( gl != null )
370                 {
371                         /** Purging the local list **/
372                         sortedCluster = null ;
373                         sortedCluster = new ArrayList<Cluster>() ;
374                         
375                         ArrayList<Double> calcMark = new ArrayList<Double>() ;
376                         
377                         hd = gl.getHeterogenityDegre() ;
378
379                         /** Sorting clusters **/
380                         ArrayList<Cluster> tmp = gl.getClusters() ;
381                         
382                         boolean ok ;
383                         
384                         double calcLoc = 0 ;
385                         double normN ;
386                         double normP ;
387                         
388                         double Nm = 0, Pm = 0 ;
389                         
390                         for( int i = 0 ; i < tmp.size() ; i++ )
391                         {
392                                 Nm += tmp.get( i ).getNbFreeNodes() ;
393                                 Pm += tmp.get( i ).getAvgAvailablePower() ;
394                         }
395                         
396                         for( int i = 0 ; i < tmp.size() ; i++ )
397                         {
398                                 normN = tmp.get( i ).getNbFreeNodes() * 100 / Nm ;
399                                 normP = tmp.get( i ).getAvgAvailablePower() * 100 / Pm ;
400                                 
401                                 /** The magic formula :P **/
402                                 calcLoc = Math.sqrt( Math.pow( (normP * hd), 2) +
403                                                   Math.pow( (normN *(1 - hd)), 2 ) ) ;
404                                 
405                                 ok = false ;
406                                 
407                                 if( sortedCluster.size() == 0 )
408                                 {
409                                         sortedCluster.add( tmp.get( i ) ) ;
410                                         calcMark.add( calcLoc ) ;
411                                 } else {
412                                         
413                                         for( int j = 0 ; j < sortedCluster.size() ; j++ )
414                                         {
415                                                 if( calcLoc > calcMark.get( j ) )
416                                                 {
417                                                         sortedCluster.add( j, tmp.get( i ) ) ;
418                                                         calcMark.add( j, calcLoc ) ;
419                                                         ok = true ;
420                                                         break ;
421                                                 }
422                                         }
423                                         
424                                         if( ! ok )
425                                         {
426                                                 sortedCluster.add( tmp.get( i ) ) ;
427                                                 calcMark.add( calcLoc ) ;
428                                         }
429                                 }
430                         }
431                 }
432         }
433
434
435         @Override
436         public GNode replaceNode( GNode _dead, ArrayList<GNode> _ag ) 
437         {
438                 GNode ret = null ;      
439                 
440                 /** If something has to be done **/
441                 if( _dead != null )
442                 {
443                         boolean ok = false ;
444                         /** Any place on the same cluster ? **/
445                         for( int i = 0 ; i < gl.getNbCluster() ; i++ )
446                         {
447                                 Cluster cl = null ;
448                                 
449                                 int id = gl.getClusterOfNode( _dead ) ;
450                                 
451                                 if( id != -1 )
452                                 {
453                                         cl = gl.getClusters().get(id) ;
454                                         
455                                         if( cl.getNbFreeNodes() > 0 )
456                                         {
457                                                 ret = cl.nextGNode() ;
458                                                 ok = true ;
459                                         }
460                                 } else {
461                                         System.err.println( "Cluster not found!" ) ;
462                                 }
463                         }
464                         
465                         /** If there is no more place, try other clusters **/
466                         if( ! ok )
467                         {
468                                 updateSortedClusters() ;
469                                 
470                                 for( int i = 0 ; i < sortedCluster.size() ; i++ )
471                                 {
472                                         if( sortedCluster.get( i ).getNbFreeNodes() > 0 )
473                                         {
474                                                 ret = sortedCluster.get( i ).nextGNode() ;
475                                                 ok = true ;
476                                                 break ;
477                                         }
478                                 }
479                         }
480                         
481                         nb_fault++ ;
482                 }
483                 
484                 return ret ;
485         }
486         
487         
488         @Override
489         public GNode getOtherGNode( ArrayList<GNode> _ag ) 
490         {
491                 GNode ret = null ;      
492                 
493                 /** Searching the cluster which has the more free nodes **/
494                 int pos = -1, max = 0, cur = 0 ;
495                 for( int i = 0 ; i < gl.getNbCluster() ; i++ )
496                 {
497                         cur = gl.getClusters().get( i ).getNbFreeNodes() ;
498                         if( cur > max)
499                         {
500                                 pos = i ;
501                                 max = cur ;
502                         }
503                 }
504
505                 ret = gl.getClusters().get( pos ).nextGNode() ;
506                 
507                 return ret ;
508         }
509         
510 }
511
512 /** La programmation est un art, respectons ceux qui la pratiquent !! **/