4 import java.io.Serializable;
5 import java.util.ArrayList;
10 * Class representing a computing grid, composed of multiple clusters
11 * @author Sébastien Miquée
13 public class Grid implements Serializable
15 private static final long serialVersionUID = 1L;
18 private int nb_cluster ;
20 private ArrayList<Cluster> clusters ;
21 private ArrayList<GNode> gnodesList;
22 private boolean gnodesList_done;
32 clusters = new ArrayList<Cluster>() ;
33 gnodesList = new ArrayList<GNode>() ;
34 gnodesList_done = false ;
39 * Add a cluster in the grid.
40 * @param c Cluster to be add
42 public void addCluster( Cluster c )
48 nb_node += c.getNbGNode() ;
54 * Add a clusters list in the grid.
55 * @param al List of clusters to be add
57 public void addClusters( ArrayList<Cluster> al )
61 int nbCLusterNodes = 0 ;
63 for( int i = 0 ; i < al.size() ; i++ )
65 clusters.add( al.get( i ) ) ;
67 nbCLusterNodes = al.get( i ).getNbGNode() ;
68 nb_node += nbCLusterNodes ;
70 for( int j = 0 ; j < nbCLusterNodes ; j++ )
72 gnodesList.add( al.get( i ).getGNodes().get( j ) ) ;
80 * Return the amount of clusters in the grid.
81 * @return The amount of clusters
83 public int getNbCluster()
90 * Return the amount of computing nodes in the grid.
91 * @return The amount of computing nodes
93 public int getNbGNode()
100 * Return the grid in a clusters list view.
101 * @return Clusters list
103 public ArrayList<Cluster> getClusters()
110 * Initialization of clusters.
112 public void initClusters()
114 for( int i = 0 ; i < nb_cluster ; i++ )
116 clusters.get( i ).initIndice() ;
122 * Compute and return the distance between two clusters.
123 * @param _g1 First cluster
124 * @param _g2 Second cluster
125 * @return The distance between the two clusters
127 public double getDistance( GNode _g1, GNode _g2 )
131 if( _g1.equals( _g2 ) )
136 String cluster1 = "c1", cluster2 = "c2", site1 = "s1", site2 = "s2" ;
138 for( int i = 0 ; i < clusters.size() ; i++ )
140 if( clusters.get( i ).isIn( _g1) )
142 cluster1 = clusters.get( i ).getName() ;
143 site1 = clusters.get( i ).getSite() ;
146 if( clusters.get( i ).isIn( _g2) )
148 cluster2 = clusters.get( i ).getName() ;
149 site2 = clusters.get( i ).getSite() ;
155 if( cluster1.compareTo( cluster2 ) == 0 )
159 if( site1.compareTo( site2 ) == 0 )
172 * Return the list of computing nodes in the grid.
173 * @return The list of computing nodes
175 public ArrayList<GNode> getGNodes()
177 if( ! gnodesList_done )
179 gnodesList = new ArrayList<GNode>() ;
181 for( int i = 0 ; i < clusters.size() ; i++ )
183 ArrayList<GNode> ar = clusters.get( i ).getGNodes() ;
185 for( int j = 0 ; j < ar.size() ; j++ )
187 gnodesList.add( ar.get( j ) ) ;
191 gnodesList_done = true ;
199 * Upgrade the grid with new nodes.
200 * @param _gnodes The list of new nodes
202 public void updateGrid( ArrayList<GNode> _gnodes )
204 if( _gnodes != null && _gnodes.size() != 0 )
206 for( int i = 0 ; i < _gnodes.size() ; i++ )
208 /** Searching the cluster in which the node should be added **/
210 for( j = 0; j < clusters.size(); j++ )
212 if( _gnodes.get( i ).getCluster().equalsIgnoreCase( clusters.get( j ).getName() ) )
214 if( ! clusters.get( j ).isIn( _gnodes.get( i ) ) )
216 _gnodes.get( i ).setCluster( clusters.get( j ).getName() ) ;
217 _gnodes.get( i ).setSite( clusters.get( j ).getSite() ) ;
218 _gnodes.get( i ).setInCluster( true ) ;
219 _gnodes.get( i ).setMapped( false ) ;
221 clusters.get( i ).addGNode( _gnodes.get( j ) ) ;
223 gnodesList.add( _gnodes.get( i ) ) ;
230 /** The cluster was not found, so it is a new one **/
231 if( j == clusters.size() )
233 String site = "", cluster = "" ;
234 Cluster nClust = new Cluster() ;
237 String names[] = Utils.decodeG5Knames( _gnodes.get( i ).getName() ) ;
239 cluster = names[ 1 ] ;
242 nClust.setName( cluster ) ;
243 nClust.setSite( site ) ;
245 _gnodes.get( i ).setInCluster( true ) ;
246 _gnodes.get( i ).setMapped( false ) ;
247 _gnodes.get( i ).setSite( site ) ;
248 _gnodes.get( i ).setCluster( cluster ) ;
250 nClust.addGNode( _gnodes.get( i ) ) ;
253 clusters.add( nClust ) ;
255 gnodesList.add( _gnodes.get( i ) ) ;
259 gnodesList_done = false ;
265 * Remove a computing node from the grid.
266 * @param _dead The node to be removed
268 public void removeGNode( GNode _dead )
272 if( _dead.getMapped() )
274 String site = "", cluster = "" ;
276 site = _dead.getSite() ;
277 cluster = _dead.getCluster() ;
279 /** Removing GNode from its cluster **/
280 for( int j = 0 ; j < clusters.size() ; j++ )
282 if( clusters.get( j ).getName().equals( cluster ) && clusters.get( j ).getSite().equals( site ))
284 clusters.get( j ).removeGNode( _dead ) ;
291 /** Removing the dead node from the global list **/
292 for( int i = 0 ; i < nb_node ; i++ )
294 if( _dead.getId() == gnodesList.get( i ).getId() )
296 gnodesList.remove( i ) ;
303 gnodesList_done = false ;
308 * Compute the heterogeneity degree of the grid.
309 * This is based on the relative standard deviation.
310 * @return The heterogeneity degree of the grid
312 public double getHeterogenityDegre()
314 if( nb_node != gnodesList.size() )
316 System.err.println( "Mapping - Heterogeneity degree computation! " +
317 "List's size not corresponding!" ) ;
327 /** Computation of the average power of computing nodes **/
328 for( int i = 0 ; i < nb_node ; i++ )
330 temp += gnodesList.get(i).getPower() ;
333 average = temp / nb_node ;
335 /** Computation of the variance **/
337 for( int i = 0 ; i < nb_node ; i++ )
339 temp += Math.pow( ( gnodesList.get(i).getPower() - average ), 2 ) ;
342 /** Computation of the standard deviation **/
343 temp = temp / nb_node ;
344 std = Math.sqrt( temp ) ;
346 /** Computation of the relative standard deviation
349 hd = 100 * std / average / 10 ;
357 * Print a comprehensible text version of the grid.
361 System.out.println();
362 System.out.println( "\t=> Grid composition :\n" ) ;
363 for( int i = 0 ; i < nb_cluster ; i++ )
365 System.out.println( "\t\tCluster \""+ clusters.get(i).getName() +"\" : " + clusters.get(i).getNbGNode() ) ;
368 System.out.println();
374 /** La programmation est un art, respectons ceux qui la pratiquent !! **/