Algorithmique Numérique Distribuée Public GIT Repository
70799f2351a0c4f931cdce9dba7ae96dfcf8d3cc
1 package and.Mapping ;
4 import java.util.ArrayList;
5 import java.util.Random;
8 /**
9  * Implementation of the AIAC Quick Quality Map (AIAC-QM) algorithm
10  * @author S&eacute;bastien Miqu&eacute;e
11  * @version 1.0
12  */
13 public class QM extends Algo
14 {
15         private static final long serialVersionUID = 1L;
19         private ArrayList<GNode> archi ;
20         private double f ; // search factor
23         /**
24          * Default constructor.
25          */
26         public QM()
27         {
28                 super() ;
29         }
32         /**
33          * Constructor
34          * @param _gr Application graph to be mapped on
35          * @param _gd Grid graph
36          * @param _f Search factor
37          */
38         public QM( Graph _gr, Grid _gd, double _f )
39         {
40                 super( _gr, _gd ) ;
42                 f = _f ;
43         }
45         /**
46          *
47          * @return
48          */
49         private ArrayList<GNode> sortInitGNode()
50         {
51                 ArrayList<GNode> grn = null ;
53                 if( gl != null )
54                 {
55                         grn = new ArrayList<GNode>() ;
57                         ArrayList<GNode> tmp = gl.getGNodes() ;
59                         grn.add( tmp.get( 0 ) ) ;
61                         boolean ok ;
63                         for( int i = 1 ; i < tmp.size() ; i++ )
64                         {
65                                 ok = false ;
67                                 for( int j = 0 ; j < grn.size() ; j++ )
68                                 {
69                                         if( tmp.get( i ).getPower() > grn.get( j ).getPower() )
70                                         {
71                                                 grn.add( j, tmp.get( i ) ) ;
72                                                 ok = true ;
73                                                 break ;
74                                         }
75                                 }
77                                 if( ok == false )
78                                 {
79                                         grn.add( tmp.get( i ) ) ;
80                                 }
81                         }
82                 }
84                 return grn ;
85         }
89         {
90                 ArrayList<GTask2> gr2 = null ;
92                 if( _gr != null )
93                 {
94                         gr2 = new ArrayList<GTask2>() ;
96                         for( int i = 0 ; i < _gr.getNbGTask() ; i++ )
97                         {
99                         }
100                 }
102                 return gr2 ;
103         }
106         @Override
107         public void map()
108         {
109                 /* If the mapping is possible ... */
110                 if( gr.getNbGTask() <= gl.getNbGNode() )
111                 {
112                         atraiter = listToGTask2( gr ) ;
113                         archi = sortInitGNode() ;
115                         /** Local Variables **/
116                         GNode nc = null ;
117                         double yc = -1 ;
118                         GNode nb = null ;
119                         double yb = -1 ;
120                         GNode nr = null ;
121                         double yr = -1 ;
122                         double ynb = -1 ;
125                         System.out.println( "*******************************************" ) ;
126                         System.out.println( "* Launching the AIAC-QM Mapping algorithm *" ) ;
127                         System.out.println( "*******************************************\n\n" ) ;
129                         /** Initial values **/
130                         int r = 1 ; // Number of rounds
133                         /** Initial mapping **/
134                         initMapping() ;
136                         /** Main loop **/
137                         while( isOneMoveable() )
138                         {
139                                 for( int ti = 0 ; ti < atraiter.size() ; ti++ )
140                                 {
141                                         if( atraiter.get( ti ).isMoveable() )
142                                         {
143                                                 nc = atraiter.get( ti ).getMapedOn() ;
144                                                 yc = atraiter.get( ti ).getExecTime() ;
145                                                 nb = nc ;
146                                                 yb = yc ;
148                                                 /** Search a node to map on **/
149                                                 for( int k = 0 ; k < ( f * n / r ) ; k++ )
150                                                 {
151                                                         nr = selectRandomGNode( n, r ) ;
152                                                         yr = execTimeOn( atraiter.get( ti ).clone(), nr ) ;
154                                                         if( yr < yb )
155                                                         {
156                                                                 nb = nr ;
157                                                                 yb = yr ;
158                                                         }
159                                                 }
161                                                 /** Research of the neighbours' nodes **/
162                                                 ArrayList<GNode> neighbours = researchNeighbours( atraiter.get( ti ), 1 ) ;
164                                                 for( int ni = 0 ; ni < neighbours.size() ; ni++ )
165                                                 {
166                                                         ynb = execTimeOn( atraiter.get( ti ).clone(), neighbours.get( ni ) ) ;
168                                                         if( ynb < yb )
169                                                         {
170                                                                 nb = neighbours.get( ni ) ;
171                                                                 yb = ynb ;
172                                                         }
173                                                 }
176                                                 /** Mapping of the task **/
177                                                 if( ! nb.equals( nc ) )
178                                                 {
180                                                         if( t_ != null && t_.isMoveable() )
181                                                         {
182                                                                 t_.setGNode( null ) ;
183                                                         }
185                                                         atraiter.get( ti ).setGNode( nb ) ;
187                                                         updateExecTimeNeighbours( atraiter.get( ti ) ) ;
188                                                 }
189                                         }
191                                         /** The task is fixed on this node **/
192                                         atraiter.get( ti ).setMoveable( false ) ;
194                                         /** If all tasks have been considered **/
195                                         if( ti == atraiter.size() - 1 )
196                                         {
197                                                 r++ ;
198                                         }
199                                 }
200                         }
202                         /** Save the Mapping **/
203                         for( int i = 0 ; i < atraiter.size() ; i++ )
204                         {
205                                 mp.addMapping( new Association( atraiter.get( i ).getMapedOn(), atraiter.get( i ).getGTask() ) ) ;
206                         }
208                 } else {
209                         System.err.println( "\n\n!!! Unable to map application !\n\n" ) ;
210                 }
212                 /** Update in cluster the status of nodes **/
213                 updateGrid() ;
214         }
216         /**
217          *
218          * @param _nb
219          * @return
220          */
222         {
223                 for( int i = 0 ; i < atraiter.size() ; i++ )
224                 {
225                         if( atraiter.get( i ).getMapedOn().equals( _nb ) )
226                         {
227                                 return atraiter.get( i ) ;
228                         }
229                 }
231                 return null;
232         }
234         /**
235          *
236          * @param _g
237          * @param _deep
238          * @return
239          */
240         private ArrayList<GNode> researchNeighbours( GTask2 _g, double _deep)
241         {
242                 ArrayList<GNode> nb = new ArrayList<GNode>() ;
245                 for( int i = 0 ; i < _g.getGTask().getNbDep() ; i++ )
246                 {
248                 }
250                 for( int i = 0 ; i < archi.size() ; i++ )
251                 {
252                         for( int j = 0 ; j < neighbours.size() ; j++ )
253                         {
254                                 GNode tmp = neighbours.get( j ).getMapedOn() ;
256                                 if( gl.getDistance( tmp, archi.get( i ) ) <= _deep && ! nb.contains( tmp ) )
257                                 {
259                                 }
260                         }
261                 }
263                 return nb ;
264         }
267         /**
268          * Initialization of the mapping. Each task is mapped on computing
269          * nodes in order of their rank.
270          */
271         private void initMapping()
272         {
273                 for( int i = 0 ; i < atraiter.size() ; i++ )
274                 {
275                         atraiter.get( i ).setGNode( archi.get( i ) ) ;
276                 }
277         }
280         /**
281          *
282          * @param _g
283          * @param _n
284          * @return
285          */
286         private double execTimeOn( GTask2 _g, GNode _n )
287         {
288                 _g.setGNode( _n ) ;
290                 return calcExecTime( _g ) ;
291         }
293         /**
294          *
295          * @param _n
296          * @param _r
297          * @return
298          */
299         private GNode selectRandomGNode( int _n, int _r )
300         {
301                 GNode g = null ;
303                 Random rand = new Random() ;
305                 g = archi.get( rand.nextInt( _n / _r ) ) ;
307                 while( isTaskNotMoveableOn( g ) )
308                 {
309                         g = archi.get( rand.nextInt( _n / _r ) ) ;
310                 }
312                 return g ;
313         }
316         /**
317          *
318          * @param _g
319          * @return
320          */
321         private boolean isTaskNotMoveableOn( GNode _g )
322         {
323                 for( int i = 0 ; i < atraiter.size() ; i++ )
324                 {
325                         if( atraiter.get( i ).getMapedOn().equals( _g ) && ! atraiter.get( i ).isMoveable() )
326                         {
327                                 return true ;
328                         }
329                 }
331                 return false ;
332         }
335         /**
336          *
337          * @return
338          */
339         private boolean isOneMoveable()
340         {
341                 if( atraiter != null && atraiter.size() > 0 )
342                 {
343                         for( int i = 0 ; i < atraiter.size() ; i++ )
344                         {
345                                 if( atraiter.get( i ).isMoveable() )
346                                 {
347                                         return true ;
348                                 }
349                         }
350                 }
352                 return false ;
353         }
356         /**
357          *
358          * @param _g
359          * @return
360          */
361         private double calcExecTime( GTask2 _g )
362         {
363                 double w = -1 ;
365                 if( _g != null )
366                 {
367                         // Weight of computation
368                         w = ( _g.getGTask().getWeight() / _g.mappedOn.getPower() ) ;
370                         // Weight of communications
371                         int tab[] = new int[ _g.getGTask().getNbDep() ] ;
372                         for( int i = 0 ; i < _g.getGTask().getNbDep() ; i++ )
373                         {
374                                 tab[ i ] = _g.getGTask().getDependencies().get( i ).getNum() ;
375                         }
377                         for( int i = 0 ; i < tab.length ; i++ )
378                         {
379                                 double tmp = gl.getDistance( _g.getMapedOn(), atraiter.get( tab[i] ).getMapedOn() ) ;
381                                 if( tmp >= 0 )
382                                 {
383                                         w += tmp ;
384                                 }
385                         }
386                 }
388                 return w ;
389         }
392         /**
393          *
394          * @param _g
395          */
396         private void updateExecTime( GTask2 _g )
397         {
398                 double w = calcExecTime( _g ) ;
400                 if( w >= 0 )
401                 {
402                         if( w > _g.getExecTime() )
403                         {
404                                 _g.setMoveable( true ) ;
405                         }
407                         _g.setExecTime( w ) ;
408                 }
409         }
412         /**
413          *
414          * @param _g
415          */
416         private void updateExecTimeNeighbours( GTask2 _g )
417         {
418                 int tab[] = new int[ _g.getGTask().getNbDep() ] ;
419                 for( int i = 0 ; i < _g.getGTask().getNbDep() ; i++ )
420                 {
421                         tab[ i ] = _g.getGTask().getDependencies().get( i ).getNum() ;
422                 }
424                 for( int i = 0 ; i < tab.length ; i++ )
425                 {
426                         updateExecTime( atraiter.get( tab[i] ) ) ;
427                 }
428         }
432         /** Intern class **/
433         /**
434          * Temporary class.
435          */
437         {
439                 private boolean moveable ;
440                 private double execTime ;
441                 private GNode mappedOn ;
444                 {
445                         g = null ;
446                         moveable = false ;
447                         execTime = -1 ;
448                         mappedOn = null ;
449                 }
452                 {
453                         g = _g ;
454                         moveable = false ;
455                         execTime = -1 ;
456                         mappedOn = null ;
457                 }
459                 public boolean isMoveable()
460                 {
461                         return moveable ;
462                 }
464                 public void setMoveable( boolean b )
465                 {
466                         moveable = b ;
467                 }
469                 public void setGNode( GNode _g )
470                 {
471                         mappedOn = _g ;
472                 }
476                 {
477                         return g ;
478                 }
481                 public double getExecTime()
482                 {
483                         return execTime ;
484                 }
487                 public void setExecTime( double _d )
488                 {
489                         execTime = _d ;
490                 }
492                 public GNode getMapedOn()
493                 {
494                         return mappedOn ;
495                 }
499                 {
501                         g_new.execTime = this.execTime ;
502                         g_new.g = this.g ;
503                         g_new.mappedOn = this.mappedOn ;
504                         g_new.moveable = this.moveable ;
506                         return g_new ;
507                 }
508         }
512         @Override
513         public GNode replaceNode(GNode _dead, ArrayList<GNode> _ag )
514         {
515                 /** Update in cluster the status of nodes **/
516                 updateGrid() ;
517                 return null;
518         }
521         @Override
522         public GNode getOtherGNode( ArrayList<GNode> _ag ) {
523                 // TODO Auto-generated method stub
524                 return null;
525         }
526 }
530 /** La programmation est un art, respectons ceux qui la pratiquent !! **/