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;
17 private ArrayList<Cluster> clusters ;
18 private ArrayList<GNode> gnodesList;
19 private boolean gnodesList_done;
27 clusters = new ArrayList<Cluster>() ;
28 gnodesList = new ArrayList<GNode>() ;
29 gnodesList_done = false ;
34 * Add a cluster in the grid.
35 * @param c Cluster to be add
37 public void addCluster( Cluster c )
43 for( int i = 0 ; i < c.getNbGNode() ; i++ )
45 gnodesList.add( c.getGNodes().get( i ) ) ;
52 * Add a clusters list in the grid.
53 * @param al List of clusters to be add
55 public void addClusters( ArrayList<Cluster> al )
59 int nbCLusterNodes = 0 ;
61 for( int i = 0 ; i < al.size() ; i++ )
63 clusters.add( al.get( i ) ) ;
64 nbCLusterNodes = al.get( i ).getNbGNode() ;
66 for( int j = 0 ; j < nbCLusterNodes ; j++ )
68 gnodesList.add( al.get( i ).getGNodes().get( j ) ) ;
76 * Return the amount of clusters in the grid.
77 * @return The amount of clusters
79 public int getNbCluster()
81 return clusters.size() ;
86 * Return the amount of computing nodes in the grid.
87 * @return The amount of computing nodes
89 public int getNbGNode()
91 return gnodesList.size() ;
96 * Return the grid in a clusters list view.
97 * @return Clusters list
99 public ArrayList<Cluster> getClusters()
106 * Search a cluster of the given name, and return it if it exists.
107 * @param _name The name of the cluster
108 * @return The cluster
110 public Cluster getCluster( String _name )
112 for( int i = 0 ; i < clusters.size() ; i++ )
114 if( clusters.get( i ).getName().equalsIgnoreCase( _name ) )
116 return clusters.get( i ) ;
120 System.err.println( "The cluster \"" + _name + "\" does not exist!" ) ;
127 * Compute and return the distance between two clusters.
128 * @param _g1 First cluster
129 * @param _g2 Second cluster
130 * @return The distance between the two clusters
132 public double getDistance( GNode _g1, GNode _g2 )
134 if( _g1 == null || _g2 == null )
141 String cluster1 = "c1", cluster2 = "c2", site1 = "s1", site2 = "s2" ;
143 cluster1 = _g1.getClusterName() ;
144 site1 = _g1.getSiteName() ;
145 cluster2 = _g2.getClusterName() ;
146 site2 = _g2.getSiteName() ;
148 if( cluster1.equalsIgnoreCase( cluster2 ) )
152 if( site1.equalsIgnoreCase( site2 ) )
165 * Return the list of computing nodes in the grid.
166 * @return The list of computing nodes
168 public ArrayList<GNode> getGNodes()
170 if( ! gnodesList_done )
172 gnodesList = new ArrayList<GNode>() ;
174 for( int i = 0 ; i < clusters.size() ; i++ )
176 ArrayList<GNode> ar = clusters.get( i ).getGNodes() ;
178 for( int j = 0 ; j < ar.size() ; j++ )
180 gnodesList.add( ar.get( j ) ) ;
184 gnodesList_done = true ;
192 * Return the list of free computing nodes in the grid.
193 * @return The list of free computing nodes
195 public ArrayList<GNode> getFreeGNodes()
197 ArrayList<GNode> ret = new ArrayList<GNode>() ;
199 for( int i = 0 ; i < clusters.size() ; i++ )
201 ArrayList<GNode> ar = clusters.get( i ).getFreeGNodes() ;
203 for( int j = 0 ; j < ar.size() ; j++ )
205 ret.add( ar.get( j ) ) ;
214 * Upgrade the grid with new nodes.
215 * @param _gnodes The list of new nodes
217 public void updateGrid( ArrayList<GNode> _gnodes )
219 if( _gnodes != null && _gnodes.size() != 0 )
221 for( int i = 0 ; i < _gnodes.size() ; i++ )
223 /** Searching the cluster in which the node should be added **/
227 for( j = 0 ; j < clusters.size() ; j++ )
229 if( _gnodes.get( i ).getClusterName().equalsIgnoreCase( clusters.get( j ).getName() )
230 && _gnodes.get( i ).getSiteName().equalsIgnoreCase( clusters.get( j ).getSite() ) )
232 int pos = clusters.get( j ).isIn( _gnodes.get( i ) ) ;
236 _gnodes.get( i ).setCluster( clusters.get( j ) ) ;
237 _gnodes.get( i ).setInCluster( true ) ;
238 _gnodes.get( i ).setMapped( false ) ;
240 clusters.get( j ).addGNode( _gnodes.get( i ) ) ;
241 gnodesList.add( _gnodes.get( i ) ) ;
244 clusters.get( j ).setGNodeStatus( _gnodes.get( i ), _gnodes.get( i ).getMapped() ) ;
252 /** The cluster was not found, so it is a new one **/
255 String site = "", cluster = "" ;
256 Cluster nClust = new Cluster() ;
258 cluster = _gnodes.get( i ).getClusterName() ; // names[ 1 ] ;
259 site = _gnodes.get( i ).getSiteName() ; // names[ 2 ] ;
261 System.out.println( "** (Grid) Creation of cluster " + cluster + " on site " + site ) ;
263 nClust.setName( cluster ) ;
264 nClust.setSite( site ) ;
266 _gnodes.get( i ).setInCluster( true ) ;
267 _gnodes.get( i ).setMapped( false ) ;
268 _gnodes.get( i ).setCluster( nClust ) ;
270 nClust.addGNode( _gnodes.get( i ) ) ;
272 clusters.add( nClust ) ;
273 gnodesList.add( _gnodes.get( i ) ) ;
277 gnodesList_done = false ;
283 * Upgrade the grid with a new node.
284 * @param _g The new node
286 public void addGNode( GNode _g )
290 /** Searching the cluster in which the node should be added **/
294 for( j = 0 ; j < clusters.size() ; j++ )
296 if( _g.getClusterName().equalsIgnoreCase( clusters.get( j ).getName() )
297 && _g.getSiteName().equalsIgnoreCase( clusters.get( j ).getSite() ) )
299 int pos = clusters.get( j ).isIn( _g ) ;
303 _g.setInCluster( true ) ;
304 _g.setMapped( false ) ;
305 _g.setCluster( clusters.get( j ) ) ;
307 clusters.get( j ).addGNode( _g ) ;
308 gnodesList.add( _g ) ;
311 _g.setCluster( clusters.get( j ) ) ;
312 clusters.get( j ).replaceGNode( _g ) ;
320 /** The cluster was not found, so it is a new one **/
323 String site = "", cluster = "" ;
324 Cluster nClust = new Cluster() ;
326 cluster = _g.getClusterName() ; // names[ 1 ] ;
327 site = _g.getSiteName() ; //names[ 2 ] ;
329 System.out.println( "** (Grid) Creation of cluster " + cluster + " on site " + site ) ;
331 nClust.setName( cluster ) ;
332 nClust.setSite( site ) ;
334 _g.setInCluster( true ) ;
335 _g.setMapped( false ) ;
336 _g.setCluster( nClust ) ;
338 nClust.addGNode( _g ) ;
340 clusters.add( nClust ) ;
341 gnodesList.add( _g ) ;
345 gnodesList_done = false ;
350 * Remove a computing node from the grid.
351 * @param _dead The node to be removed
353 public void removeGNode( GNode _dead )
357 /** Removing GNode from its cluster **/
358 int id = getClusterOfNode( _dead ) ;
362 clusters.get( id ).removeGNode( _dead ) ;
364 System.err.println( "(Grid) Cluster of dead node not found!" ) ;
367 /** Removing the dead node from the global list **/
369 for( i = 0 ; i < gnodesList.size() ; i++ )
371 if( _dead.getId() == gnodesList.get( i ).getId() )
373 gnodesList.remove( i ) ;
379 System.err.println( "(Grid) The GNode to be deleted is null!" ) ;
382 gnodesList_done = false ;
387 * Change the mapping status of a node in the grid and in its cluster.
388 * @param _g The node to change the mapping status
389 * @param _status The node's mapping status
391 public void setMappedStatus( GNode _g, boolean _status )
395 /** Change in the cluster **/
396 int id = getClusterOfNode( _g ) ;
400 clusters.get( id ).setGNodeStatus( _g, _status ) ;
402 System.err.println( "(Grid) Cluster " + _g.getClusterName() + " not found!" ) ;
405 /** Change in local list **/
406 for( int i = 0 ; i < gnodesList.size() ; i++ )
408 if( _g.getId() == gnodesList.get( i ).getId() )
410 gnodesList.get( i ).setMapped( _status ) ;
419 * Search and return the cluster containing the specified node.
421 * @return The cluster containing the node
423 public int getClusterOfNode( GNode _g )
429 for( int i = 0 ; i < clusters.size() ; i++ )
431 if( _g.getClusterName().equalsIgnoreCase( clusters.get( i ).getName() )
432 && _g.getSiteName().equalsIgnoreCase( clusters.get( i ).getSite() ) )
445 * Compute the heterogeneity degree of the grid.
446 * This is based on the relative standard deviation.
447 * @return The heterogeneity degree of the grid
449 public double getHeterogenityDegre()
451 if( ! ( gnodesList.size() > 0 ) )
453 System.err.println( "Mapping - Heterogeneity degree computation! " +
454 "List's size not corresponding!" ) ;
463 long nb_freenodes = 0 ;
465 /** Computation of the average power of computing nodes **/
466 for( int i = 0 ; i < gnodesList.size() ; i++ )
468 if( ! gnodesList.get( i ).getMapped() )
470 temp += gnodesList.get(i).getPower() ;
475 average = temp / nb_freenodes ; //gnodesList.size() ;
477 /** Computation of the variance **/
479 for( int i = 0 ; i < gnodesList.size() ; i++ )
481 if( ! gnodesList.get( i ).getMapped() )
483 temp += Math.pow( ( gnodesList.get(i).getPower() - average ), 2 ) ;
487 /** Computation of the standard deviation **/
488 temp = temp / nb_freenodes ; //gnodesList.size() ;
489 std = Math.sqrt( temp ) ;
491 /** Computation of the relative standard deviation **/
494 /** Correction of the maximum value **/
505 * Return the amount of nodes available in the cluster containing the
506 * maximum of available nodes.
507 * @return The maximum available nodes of the architecture
509 public int getMaxClusterNode()
513 for( int i = 0 ; i < clusters.size() ; i++ )
515 if( max < clusters.get( i ).getNbFreeNodes() )
517 max = clusters.get( i ).getNbFreeNodes() ;
526 * Return the average amount of nodes available in all clusters.
527 * @return The average available nodes of the architecture
529 public double getAvgClusterNode()
533 for( int i = 0 ; i < clusters.size() ; i++ )
535 nb += clusters.get( i ).getNbFreeNodes() ;
538 return ( nb / getNbFreenodes() ) ;
543 * Initialization of computing nodes in the grid. Set all
544 * of these nodes to be not mapped on, and do the same thing in each
547 public void initGNodes()
549 /** Initialization of the local nodes **/
550 for( int i = 0 ; i < gnodesList.size() ; i++ )
552 gnodesList.get( i ).setMapped( false ) ;
555 /** Initialization in clusters **/
556 for( int i = 0 ; i < clusters.size() ; i++ )
558 clusters.get( i ).initGNodes() ;
559 clusters.get( i ).initMoreGNode() ;
566 * Initialization of a set of computing nodes in the grid. Set all
567 * of these given nodes to be not mapped on, and propagates in each
569 * @param _ag The nodes' list to initialize
571 public void initGNodes( ArrayList<GNode> _ag )
573 if( _ag != null && _ag.size() > 0 )
575 /** Initialization of the local and clusters nodes **/
576 for( int i = 0 ; i < _ag.size() ; i++ )
578 setMappedStatus( _ag.get( i ), false ) ;
580 int id = getClusterOfNode( _ag.get( i ) ) ;
584 clusters.get(id).setGNodeStatus( _ag.get( i ), false ) ;
586 System.err.println( "Cluster not found" ) ;
595 * Print a comprehensible text version of the grid.
599 System.out.println();
600 System.out.println( "\t=> Grid composition:\n" ) ;
601 for( int i = 0 ; i < clusters.size() ; i++ )
603 System.out.println( "\t\tCluster \""+ clusters.get(i).getName() +"\" : " + clusters.get(i).getNbGNode() ) ;
606 System.out.println();
611 * Return the average power of the grid, by computing the average for
613 * @return The average power of the grid
615 public double getAvgFreePower()
620 if( clusters != null && clusters.size() > 0 )
622 for( int i = 0 ; i < clusters.size() ; i++ )
626 ret += clusters.get( i ).getAvailablePower() / clusters.get( i ).getNbFreeNodes() ;
638 * Return the amount of freenodes available in the whole grid.
639 * @return The amount of available nodes
641 public int getNbFreenodes()
645 for( int i = 0 ; i < clusters.size() ; i++ )
646 ret += clusters.get(i).getNbFreeNodes() ;
652 * Return the max distance it could exist between two computing nodes.
653 * @return The max distance
655 public double getMaxDistance()
664 /** La programmation est un art, respectons ceux qui la pratiquent !! **/