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 ;
202 public void updateGrid( ArrayList<GNode> _gnodes )
204 if( _gnodes != null && _gnodes.size() != 0 )
206 for( int i = 0 ; i < _gnodes.size() ; i++ )
211 gnodesList_done = false ;
217 * Remove a computing node from the grid.
218 * @param _dead The node to be removed
220 public void removeGNode( GNode _dead )
224 if( _dead.getMapped() )
226 String site = "", cluster = "" ;
228 site = _dead.getSite() ;
229 cluster = _dead.getCluster() ;
231 /* Removing GNode from its cluster */
232 for( int j = 0 ; j < clusters.size() ; j++ )
234 if( clusters.get( j ).getName().equals( cluster ) && clusters.get( j ).getSite().equals( site ))
236 clusters.get( j ).removeGNode( _dead ) ;
243 /* Removing the dead node from the global list */
244 for( int i = 0 ; i < nb_node ; i++ )
246 if( _dead.getId() == gnodesList.get( i ).getId() )
248 gnodesList.remove( i ) ;
255 gnodesList_done = false ;
260 * Compute the heterogeneity degree of the grid.
261 * This is based on the relative standard deviation.
262 * @return The heterogeneity degree of the grid
264 public double getHeterogenityDegre()
266 if( nb_node != gnodesList.size() )
268 System.err.println( "Mapping - Heterogeneity degree computation! " +
269 "List's size not corresponding!" ) ;
279 /** Computation of the average power of computing nodes **/
280 for( int i = 0 ; i < nb_node ; i++ )
282 temp += gnodesList.get(i).getPower() ;
285 average = temp / nb_node ;
287 /** Computation of the variance **/
289 for( int i = 0 ; i < nb_node ; i++ )
291 temp += Math.pow( ( gnodesList.get(i).getPower() - average ), 2 ) ;
294 /** Computation of the standard deviation **/
295 temp = temp / nb_node ;
296 std = Math.sqrt( temp ) ;
298 /** Computation of the relative standard deviation
301 hd = 100 * std / average / 10 ;
309 * Print a comprehensible text version of the grid.
313 System.out.println();
314 System.out.println( "\t=> Grid composition :\n" ) ;
315 for( int i = 0 ; i < nb_cluster ; i++ )
317 System.out.println( "\t\tCluster \""+ clusters.get(i).getName() +"\" : " + clusters.get(i).getNbGNode() ) ;
320 System.out.println();
326 /** La programmation est un art, respectons ceux qui la pratiquent !! **/