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 ;
19 // private int nb_node ;
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 for( int i = 0 ; i < c.getNbGNode() ; i++ )
50 gnodesList.add( c.getGNodes().get( i ) ) ;
53 // nb_node += c.getNbGNode() ;
59 * Add a clusters list in the grid.
60 * @param al List of clusters to be add
62 public void addClusters( ArrayList<Cluster> al )
66 int nbCLusterNodes = 0 ;
68 for( int i = 0 ; i < al.size() ; i++ )
70 clusters.add( al.get( i ) ) ;
72 nbCLusterNodes = al.get( i ).getNbGNode() ;
73 // nb_node += nbCLusterNodes ;
75 for( int j = 0 ; j < nbCLusterNodes ; j++ )
77 gnodesList.add( al.get( i ).getGNodes().get( j ) ) ;
85 * Return the amount of clusters in the grid.
86 * @return The amount of clusters
88 public int getNbCluster()
90 return clusters.size() ;
91 // return nb_cluster ;
96 * Return the amount of computing nodes in the grid.
97 * @return The amount of computing nodes
99 public int getNbGNode()
101 return gnodesList.size() ;
107 * Return the grid in a clusters list view.
108 * @return Clusters list
110 public ArrayList<Cluster> getClusters()
117 // * Initialization of clusters.
119 // public void initClusters()
121 // for( int i = 0 ; i < nb_cluster ; i++ )
123 // clusters.get( i ).initIndice() ;
129 * Compute and return the distance between two clusters.
130 * @param _g1 First cluster
131 * @param _g2 Second cluster
132 * @return The distance between the two clusters
134 public double getDistance( GNode _g1, GNode _g2 )
138 if( _g1.equals( _g2 ) )
143 String cluster1 = "c1", cluster2 = "c2", site1 = "s1", site2 = "s2" ;
145 for( int i = 0 ; i < clusters.size() ; i++ )
147 if( clusters.get( i ).isIn( _g1 ) != -1 )
149 cluster1 = clusters.get( i ).getName() ;
150 site1 = clusters.get( i ).getSite() ;
153 if( clusters.get( i ).isIn( _g2 ) != -1 )
155 cluster2 = clusters.get( i ).getName() ;
156 site2 = clusters.get( i ).getSite() ;
162 if( cluster1.compareTo( cluster2 ) == 0 )
166 if( site1.compareTo( site2 ) == 0 )
179 * Return the list of computing nodes in the grid.
180 * @return The list of computing nodes
182 public ArrayList<GNode> getGNodes()
184 if( ! gnodesList_done )
186 gnodesList = new ArrayList<GNode>() ;
188 for( int i = 0 ; i < clusters.size() ; i++ )
190 ArrayList<GNode> ar = clusters.get( i ).getGNodes() ;
192 for( int j = 0 ; j < ar.size() ; j++ )
194 gnodesList.add( ar.get( j ) ) ;
198 gnodesList_done = true ;
206 * Upgrade the grid with new nodes.
207 * @param _gnodes The list of new nodes
209 public void updateGrid( ArrayList<GNode> _gnodes )
211 if( _gnodes != null && _gnodes.size() != 0 )
213 for( int i = 0 ; i < _gnodes.size() ; i++ )
215 /** Searching the cluster in which the node should be added **/
217 for( j = 0; j < clusters.size(); j++ )
219 if( _gnodes.get( i ).getCluster().equalsIgnoreCase( clusters.get( j ).getName() ) )
221 int pos = clusters.get( j ).isIn( _gnodes.get( i ) ) ;
225 _gnodes.get( i ).setCluster( clusters.get( j ).getName() ) ;
226 _gnodes.get( i ).setSite( clusters.get( j ).getSite() ) ;
227 _gnodes.get( i ).setInCluster( true ) ;
228 _gnodes.get( i ).setMapped( false ) ;
230 clusters.get( i ).addGNode( _gnodes.get( j ) ) ;
232 gnodesList.add( _gnodes.get( i ) ) ;
235 clusters.get( j ).setGNodeStatus( _gnodes.get( i ), _gnodes.get( i ).getMapped() ) ;
242 /** The cluster was not found, so it is a new one **/
243 if( j == clusters.size() )
245 String site = "", cluster = "" ;
246 Cluster nClust = new Cluster() ;
249 String names[] = Utils.decodeG5Knames( _gnodes.get( i ).getName() ) ;
251 cluster = names[ 1 ] ;
254 nClust.setName( cluster ) ;
255 nClust.setSite( site ) ;
257 _gnodes.get( i ).setInCluster( true ) ;
258 _gnodes.get( i ).setMapped( false ) ;
259 _gnodes.get( i ).setSite( site ) ;
260 _gnodes.get( i ).setCluster( cluster ) ;
262 nClust.addGNode( _gnodes.get( i ) ) ;
265 clusters.add( nClust ) ;
267 gnodesList.add( _gnodes.get( i ) ) ;
271 gnodesList_done = false ;
277 * Remove a computing node from the grid.
278 * @param _dead The node to be removed
280 public void removeGNode( GNode _dead )
284 if( _dead.getMapped() )
286 String site = "", cluster = "" ;
288 site = _dead.getSite() ;
289 cluster = _dead.getCluster() ;
291 /** Removing GNode from its cluster **/
292 for( int j = 0 ; j < clusters.size() ; j++ )
294 if( clusters.get( j ).getName().equals( cluster ) && clusters.get( j ).getSite().equals( site ))
296 clusters.get( j ).removeGNode( _dead ) ;
303 /** Removing the dead node from the global list **/
304 for( int i = 0 ; i < gnodesList.size() ; i++ )
306 if( _dead.getId() == gnodesList.get( i ).getId() )
308 gnodesList.remove( i ) ;
315 gnodesList_done = false ;
320 * Change the mapping status of a node in the grid and in its cluster.
321 * @param _g The node to change the mapping status
322 * @param _status The node's mapping status
324 public void setMappedStatus( GNode _g, boolean _status )
328 /** Change in the cluster **/
329 getClusterOfNode( _g ).setGNodeStatus( _g, _status ) ;
331 /** Change in local list **/
332 for( int i = 0 ; i < gnodesList.size() ; i++ )
334 if( _g.getId() == gnodesList.get( i ).getId() )
336 gnodesList.get( i ).setMapped( _status ) ;
345 * Search and return the cluster containing the specified node.
347 * @return The cluster containing the node
349 public Cluster getClusterOfNode( GNode _g )
353 for( int i = 0 ; i < clusters.size() ; i++ )
355 if( _g.getCluster().equalsIgnoreCase( clusters.get( i ).getName() ) )
357 return clusters.get( i ) ;
366 * Compute the heterogeneity degree of the grid.
367 * This is based on the relative standard deviation.
368 * @return The heterogeneity degree of the grid
370 public double getHeterogenityDegre()
372 if( ! ( gnodesList.size() > 0 ) )
374 System.err.println( "Mapping - Heterogeneity degree computation! " +
375 "List's size not corresponding!" ) ;
385 /** Computation of the average power of computing nodes **/
386 for( int i = 0 ; i < gnodesList.size() ; i++ )
388 temp += gnodesList.get(i).getPower() ;
391 average = temp / gnodesList.size() ;
393 /** Computation of the variance **/
395 for( int i = 0 ; i < gnodesList.size() ; i++ )
397 temp += Math.pow( ( gnodesList.get(i).getPower() - average ), 2 ) ;
400 /** Computation of the standard deviation **/
401 temp = temp / gnodesList.size() ;
402 std = Math.sqrt( temp ) ;
404 /** Computation of the relative standard deviation
407 hd = 100 * std / average / 10 ;
414 * Initialization of computing nodes in the grid. Set all
415 * of these nodes to be not mapped on, and do the same thing in each
418 public void initGNodes()
420 /** Initialization of the local nodes **/
421 for( int i = 0 ; i < gnodesList.size() ; i++ )
423 gnodesList.get( i ).setMapped( false ) ;
426 /** Initialization in clusters **/
427 for( int i = 0 ; i < clusters.size() ; i++ )
429 clusters.get( i ).initGNodes() ;
436 * Initialization of a set of computing nodes in the grid. Set all
437 * of these given nodes to be not mapped on, and propagates in each
439 * @param _ag The nodes' list to initialize
441 public void initGNodes( ArrayList<GNode> _ag )
443 if( _ag != null && _ag.size() > 0 )
445 /** Initialization of the local and clusters nodes **/
446 for( int i = 0 ; i < _ag.size() ; i++ )
448 setMappedStatus( _ag.get( i ), false ) ;
450 getClusterOfNode( _ag.get( i ) ).setGNodeStatus( _ag.get( i ), false ) ;
458 * Print a comprehensible text version of the grid.
462 System.out.println();
463 System.out.println( "\t=> Grid composition:\n" ) ;
464 for( int i = 0 ; i < clusters.size() ; i++ )
466 System.out.println( "\t\tCluster \""+ clusters.get(i).getName() +"\" : " + clusters.get(i).getNbGNode() ) ;
469 System.out.println();
475 /** La programmation est un art, respectons ceux qui la pratiquent !! **/