Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
New version of MAHEVE plus corrections.
[mapping.git] / src / and / Mapping / Simple.java
1 package and.Mapping ;
2
3
4 import java.util.ArrayList;
5
6
7 /**
8  * Implementation of the Simple Mapping algorithm (no fault tolerance mechanism)
9  * @author Sébastien Miquée
10  * @version 1.0
11  */
12 public class Simple extends Algo
13 {
14         private static final long serialVersionUID = 1L;
15         
16         
17         private ArrayList<GTask> atraiter ;
18         private ArrayList<GNode> archi ;        
19         
20         /**
21          * Default constructor.
22          */
23         public Simple()
24         {
25                 super() ;
26                 name = "Simple" ;
27         }
28         
29
30         /**
31          * Constructor.
32          * @param _gr Application graph to be mapped on
33          * @param _gd Grid graph
34          */
35         public Simple( Graph _gr, Grid _gd )
36         {
37                 super( _gr, _gd ) ;
38                 
39                 name = "Simple" ;
40         }
41         
42         /**
43          * 
44          * @return
45          */
46         private ArrayList<GNode> sortInitGNode() 
47         {
48                 ArrayList<GNode> grn = null ;
49                 
50                 if( gl != null )
51                 {
52                         grn = new ArrayList<GNode>() ;
53                         
54                         // Sorting clusters according to their "size"
55                     ArrayList<Cluster> cl = gl.getClusters() ;
56                     ArrayList<Cluster> cl2 = new ArrayList<Cluster>() ;
57                     
58                     int max = 0 ;
59                     Cluster c = null ;
60                     Cluster c2 = null ;
61         
62                     while( cl2.size() != gl.getNbCluster() )
63                     {
64                     
65                         max = 0 ;
66                     
67                         for( int i = 0 ; i < cl.size() ; i++ )
68                         {
69                             c = cl.get( i ) ;
70                             if( c.getNbGNode() > max )
71                             {
72                                 c2 = c ;
73                                 max = c.getNbGNode() ;
74                             }
75                         }
76                     
77                         cl2.add( c2 ) ;
78                         cl.remove( c2 ) ;
79                     }
80         
81                         for( int i = 0 ; i < cl2.size() ; i++ )
82                         {
83                             Cluster tmp = cl2.get( i ) ;
84                             
85                             for( int j = 0 ; j < tmp.getNbGNode() ; j++ )
86                             {
87                                 grn.add( tmp.getGNodes().get( j ) ) ;
88                             }
89                         }
90                 }
91                 
92                 return grn ;
93         }
94
95
96         @Override
97         public void map() 
98         {
99                 /* If the mapping is possible ... */
100                 if( gr.getNbGTask() <= gl.getNbGNode() )
101                 {
102                         archi = sortInitGNode() ;
103                         atraiter = gr.getGraph() ;
104                         
105                         System.out.println( "******************************************" ) ;
106                         System.out.println( "* Launching the Simple Mapping algorithm *" ) ;
107                         System.out.println( "******************************************\n\n" ) ;
108                         
109                         /** Save the Mapping **/
110                         for( int i = 0 ; i < atraiter.size() ; i++ )
111                         {
112                                 mp.addMapping( archi.get( i ), atraiter.get( i ) ) ;
113                         }
114                 
115                 } else {
116                         System.err.println( "\n\n!!! Unable to map application !\n\n" ) ;
117                 }
118                 
119         }
120
121
122         @Override
123         public GNode replaceNode(GNode _dead, ArrayList<GNode> _ag ) 
124         {
125                 GNode ret = null ;
126                 
127                 if( _dead != null )
128                 {
129                         int pos = 0 ;
130                         pos = mp.getIdOfAssociation( _dead ) ;
131                         
132                         if( pos == -1 )
133                         {
134                                 System.err.println( "GNode "+_dead+" does not exist in the mapping!" ) ;
135                                 return null ;
136                         }
137                         
138                         if( _ag.size() > 0 )
139                         {
140                                 ret = _ag.get( 0 ) ;
141                                 
142                                 
143                         } else {
144                                 System.err.println( "Not enought available nodes in gnodes to replace one !" ) ;
145                                 return null ;
146                         }       
147                         
148                         nb_fault++ ;
149                 }
150                 
151                 return ret ;
152         }
153
154
155         @Override
156         public GNode getOtherGNode( ArrayList<GNode> _ag ) 
157         {
158                 /** Returning the first node in the list **/
159                 if( _ag.size() > 0 )
160                 {
161                         return _ag.get( 1 ) ;
162                 } 
163                 
164                 return null ;
165         }
166
167
168         @Override
169         public boolean setParams(Object[] _params) {
170                 return true ;
171         }
172 }
173
174
175
176 /** La programmation est un art, respectons ceux qui la pratiquent !! **/