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 * Compute and return the distance between two clusters.
107 * @param _g1 First cluster
108 * @param _g2 Second cluster
109 * @return The distance between the two clusters
111 public double getDistance( GNode _g1, GNode _g2 )
115 if( _g1.equals( _g2 ) )
120 String cluster1 = "c1", cluster2 = "c2", site1 = "s1", site2 = "s2" ;
122 for( int i = 0 ; i < clusters.size() ; i++ )
124 if( clusters.get( i ).isIn( _g1 ) != -1 )
126 cluster1 = clusters.get( i ).getName() ;
127 site1 = clusters.get( i ).getSite() ;
130 if( clusters.get( i ).isIn( _g2 ) != -1 )
132 cluster2 = clusters.get( i ).getName() ;
133 site2 = clusters.get( i ).getSite() ;
137 if( cluster1.compareTo( cluster2 ) == 0 )
141 if( site1.compareTo( site2 ) == 0 )
154 * Return the list of computing nodes in the grid.
155 * @return The list of computing nodes
157 public ArrayList<GNode> getGNodes()
159 if( ! gnodesList_done )
161 gnodesList = new ArrayList<GNode>() ;
163 for( int i = 0 ; i < clusters.size() ; i++ )
165 ArrayList<GNode> ar = clusters.get( i ).getGNodes() ;
167 for( int j = 0 ; j < ar.size() ; j++ )
169 gnodesList.add( ar.get( j ) ) ;
173 gnodesList_done = true ;
181 * Upgrade the grid with new nodes.
182 * @param _gnodes The list of new nodes
184 public void updateGrid( ArrayList<GNode> _gnodes )
186 if( _gnodes != null && _gnodes.size() != 0 )
188 for( int i = 0 ; i < _gnodes.size() ; i++ )
190 /** Searching the cluster in which the node should be added **/
192 for( j = 0; j < clusters.size(); j++ )
194 if( _gnodes.get( i ).getCluster().equalsIgnoreCase( clusters.get( j ).getName() ) )
196 int pos = clusters.get( j ).isIn( _gnodes.get( i ) ) ;
200 _gnodes.get( i ).setCluster( clusters.get( j ).getName() ) ;
201 _gnodes.get( i ).setSite( clusters.get( j ).getSite() ) ;
202 _gnodes.get( i ).setInCluster( true ) ;
203 _gnodes.get( i ).setMapped( false ) ;
205 clusters.get( j ).addGNode( _gnodes.get( i ) ) ;
206 gnodesList.add( _gnodes.get( i ) ) ;
209 clusters.get( j ).setGNodeStatus( _gnodes.get( i ), _gnodes.get( i ).getMapped() ) ;
216 /** The cluster was not found, so it is a new one **/
217 if( j == clusters.size() )
219 String site = "", cluster = "" ;
220 Cluster nClust = new Cluster() ;
223 String names[] = Utils.decodeG5Knames( _gnodes.get( i ).getName() ) ;
225 cluster = names[ 1 ] ;
228 nClust.setName( cluster ) ;
229 nClust.setSite( site ) ;
231 _gnodes.get( i ).setInCluster( true ) ;
232 _gnodes.get( i ).setMapped( false ) ;
233 _gnodes.get( i ).setSite( site ) ;
234 _gnodes.get( i ).setCluster( cluster ) ;
236 nClust.addGNode( _gnodes.get( i ) ) ;
238 clusters.add( nClust ) ;
239 gnodesList.add( _gnodes.get( i ) ) ;
243 gnodesList_done = false ;
249 * Upgrade the grid with a new node.
250 * @param _g The new node
252 public void addGNode( GNode _g )
256 /** Searching the cluster in which the node should be added **/
258 for( j = 0; j < clusters.size(); j++ )
260 if( _g.getCluster().equalsIgnoreCase( clusters.get( j ).getName() ) )
262 int pos = clusters.get( j ).isIn( _g ) ;
266 _g.setSite( clusters.get( j ).getSite() ) ;
267 _g.setInCluster( true ) ;
268 _g.setMapped( false ) ;
270 clusters.get( j ).addGNode( _g ) ;
271 gnodesList.add( _g ) ;
274 clusters.get( j ).removeGNode( _g ) ;
275 clusters.get( j ).addGNode( _g ) ;
282 /** The cluster was not found, so it is a new one **/
283 if( j == clusters.size() )
285 String site = "", cluster = "" ;
286 Cluster nClust = new Cluster() ;
288 String names[] = Utils.decodeG5Knames( _g.getName() ) ;
290 cluster = names[ 1 ] ;
293 System.out.println("** (Grid) Creation of cluster: "+cluster);
295 nClust.setName( cluster ) ;
296 nClust.setSite( site ) ;
298 _g.setInCluster( true ) ;
299 _g.setMapped( false ) ;
301 _g.setCluster( cluster ) ;
303 nClust.addGNode( _g ) ;
305 clusters.add( nClust ) ;
306 gnodesList.add( _g ) ;
310 gnodesList_done = false ;
315 * Remove a computing node from the grid.
316 * @param _dead The node to be removed
318 public void removeGNode( GNode _dead )
322 /** Removing GNode from its cluster **/
323 int id = getClusterOfNode( _dead ) ;
327 clusters.get( id ).removeGNode( _dead ) ;
329 System.err.println( "(Grid) Cluster of dead node not found!" ) ;
332 /** Removing the dead node from the global list **/
334 for( i = 0 ; i < gnodesList.size() ; i++ )
336 if( _dead.getId() == gnodesList.get( i ).getId() )
338 gnodesList.remove( i ) ;
344 System.err.println( "(Grid) The GNode to be deleted is null!" ) ;
347 gnodesList_done = false ;
352 * Change the mapping status of a node in the grid and in its cluster.
353 * @param _g The node to change the mapping status
354 * @param _status The node's mapping status
356 public void setMappedStatus( GNode _g, boolean _status )
360 /** Change in the cluster **/
361 int id = getClusterOfNode( _g ) ;
365 clusters.get(id).setGNodeStatus( _g, _status ) ;
367 System.err.println( "(Grid) Cluster "+_g.getCluster()+" not found!" ) ;
370 /** Change in local list **/
371 for( int i = 0 ; i < gnodesList.size() ; i++ )
373 if( _g.getId() == gnodesList.get( i ).getId() )
375 gnodesList.get( i ).setMapped( _status ) ;
384 * Search and return the cluster containing the specified node.
386 * @return The cluster containing the node
388 public int getClusterOfNode( GNode _g )
394 for( int i = 0 ; i < clusters.size() ; i++ )
396 if( _g.getCluster().equalsIgnoreCase( clusters.get( i ).getName() ) )
398 if( _g.getSite().equalsIgnoreCase( clusters.get( i ).getSite() ) )
412 * Compute the heterogeneity degree of the grid.
413 * This is based on the relative standard deviation.
414 * @return The heterogeneity degree of the grid
416 public double getHeterogenityDegre()
418 if( ! ( gnodesList.size() > 0 ) )
420 System.err.println( "Mapping - Heterogeneity degree computation! " +
421 "List's size not corresponding!" ) ;
430 long nb_freenodes = 0 ;
432 /** Computation of the average power of computing nodes **/
433 for( int i = 0 ; i < gnodesList.size() ; i++ )
435 if( ! gnodesList.get(i).getMapped() )
437 temp += gnodesList.get(i).getPower() ;
442 average = temp / nb_freenodes ; //gnodesList.size() ;
444 /** Computation of the variance **/
446 for( int i = 0 ; i < gnodesList.size() ; i++ )
448 if( ! gnodesList.get(i).getMapped() )
450 temp += Math.pow( ( gnodesList.get(i).getPower() - average ), 2 ) ;
454 /** Computation of the standard deviation **/
455 temp = temp / nb_freenodes ; //gnodesList.size() ;
456 std = Math.sqrt( temp ) ;
458 /** Computation of the relative standard deviation **/
461 /** Correction of the maximum value **/
473 * Return the amount of nodes available in the cluster containing the
474 * maximum of available nodes.
475 * @return The maximum available nodes of the architecture
477 public int getMaxClusterNode()
481 for( int i = 0 ; i < clusters.size() ; i++ )
483 if( max < clusters.get( i ).getNbFreeNodes() )
485 max = clusters.get( i ).getNbFreeNodes() ;
494 * Initialization of computing nodes in the grid. Set all
495 * of these nodes to be not mapped on, and do the same thing in each
498 public void initGNodes()
500 /** Initialization of the local nodes **/
501 for( int i = 0 ; i < gnodesList.size() ; i++ )
503 gnodesList.get( i ).setMapped( false ) ;
506 /** Initialization in clusters **/
507 for( int i = 0 ; i < clusters.size() ; i++ )
509 clusters.get( i ).initGNodes() ;
516 * Initialization of a set of computing nodes in the grid. Set all
517 * of these given nodes to be not mapped on, and propagates in each
519 * @param _ag The nodes' list to initialize
521 public void initGNodes( ArrayList<GNode> _ag )
523 if( _ag != null && _ag.size() > 0 )
525 /** Initialization of the local and clusters nodes **/
526 for( int i = 0 ; i < _ag.size() ; i++ )
528 setMappedStatus( _ag.get( i ), false ) ;
530 int id = getClusterOfNode( _ag.get( i ) ) ;
534 clusters.get(id).setGNodeStatus( _ag.get( i ), false ) ;
536 System.err.println( "Cluster not found" ) ;
545 * Print a comprehensible text version of the grid.
549 System.out.println();
550 System.out.println( "\t=> Grid composition:\n" ) ;
551 for( int i = 0 ; i < clusters.size() ; i++ )
553 System.out.println( "\t\tCluster \""+ clusters.get(i).getName() +"\" : " + clusters.get(i).getNbGNode() ) ;
556 System.out.println();
561 * Return the average power of the grid, by computing the average for
563 * @return The average power of the grid
565 public double getAvgFreePower()
570 if( clusters != null && clusters.size() > 0 )
572 for( int i = 0 ; i < clusters.size() ; i++ )
576 ret += clusters.get( i ).getAvailablePower() / clusters.get( i ).getNbFreeNodes() ;
588 * Return the amount of freenodes available in the whole grid.
589 * @return The amount of available nodes
591 public int getNbFreenodes()
595 for( int i = 0 ; i < clusters.size() ; i++ )
596 ret += clusters.get(i).getNbFreeNodes() ;
603 /** La programmation est un art, respectons ceux qui la pratiquent !! **/