A practical introduction to OptimalGrid - Part V  
 

(Post 05/09/2006) The OptimalGrid system then handles all of the details of mapping this into a problem with thousands or millions of cells and running it on a computing Grid consisting of hundreds of computers...

Các phần đã đăng:

Section 7. Appendices (Cont)

Code Listing for EntityEden class

The following is the complete code for the EntityEden class.

// EntityEden.java
package com.ibm.almaden.smartgrid.apps.eden;
/*
** Licensed Materials - Property of IBM
**
** (C) COPYRIGHT IBM Corp. 2002 All rights reserved
**
** US Government Users Restricted Rights - Use, duplication or
** disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
**
*/
import java.awt.Color;
import org.jdom.Element;
import com.ibm.almaden.smartgrid.EntityAbstract;
import com.ibm.almaden.smartgrid.util.Debug;
// ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
// || Class EntityEden ||
// |||||||||||||||||||
/**
**
** EntityEden is a class of entities used in the Eden model
** of bacterial growth. EntityEden extends EntityAbstract
**
* An entity is any object or OPC characteristic which is capable
** of Propagating, or FLOWING from one OPC to another.
** Characteristics of OPCs which regulate this flow should be
** class implementations of PropertyAbstract. Entities might be
** a fluid, an electron or electron density,
** local stress, bacteria, heat, temperature, etc.
** Properties might be local elastic constants, local hardness,
** local conductivity, thermal conductivity, heat capacity etc.
**
**
* For the Eden model, an EntityEden instance represents
** a type of bacteria of a specified type TYPE_A, TYPE_B
** or TYPE_C. It has a "food" attribute that represents the
** type of bacteria it will eat.
*For this example, A eats B; B eats C and C eats A.
*
* The EntityEden also has an assigned propagation probability.
*
*
** @author ames Kaufman
** @version $Revision: 1.3 $ $Date: 2003/05/22 15:19:32 $
**
*/
// ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
public class EntityEden extends EntityAbstract {
public static final String FOOD_ID = "food";
public static final String PROBABILITY = "prob";
/**
* Indicator if this entity is alive or dead
* This is only used to pass information internally and is not needed
* externally.
* Because it is transient, it will always start out as false therefore
* the default value must be false. This value is also not written to XML
*/
private transient boolean _dead = false;
/**
* propagation probability for this entity.
*
*/
private double _propagationProb;
/** * The Type of bacteria that this entity will consume.
*/
private int _foodID = 0;
/**
* Types of bacteria are TYPE_A, TYPE_B and TYPE_C
*
* A eats B; B eats C and C eats A
*/
public static final int TYPE_A = 5;
public static final int TYPE_B = 3;
public static final int TYPE_C = 2;
/**
* ENTITY_A is a predefined EntityEden that has an ID of TYPE_A
* and eats TYPE_B and propagates with a probability of 0.4
*/
public static final EntityEden ENTITY_A = \
new EntityEden(TYPE_A,TYPE_B,0.4);
/**
* ENTITY_B is a predefined EntityEden that has an ID of TYPE_B
* and eats TYPE_C and propagates with a probability of 0.4
*/
public static final EntityEden ENTITY_B = \
new EntityEden(TYPE_B,TYPE_C,0.4);
/**
* ENTITY_C is a predefined EntityEden that has
* an ID of TYPE_C
* and eats TYPE_A and propagates with a probability
* of 0.4
*/
public static final EntityEden ENTITY_C = \
new EntityEden(TYPE_C,TYPE_A,0.4);
/**
* public EntityEden()
*
* Construct an EntityEden with default color and no requirements
* This public constructor is required so that the XML support
* can dynamically build from an XML file.
*/
public EntityEden() {
}
/**
* Builds an Eden entity instance which describes a
* bacteria, the type of bacteria that it eats and
* the propagation probability for this bacteria.
*
*
* @param entityID_ The type of bacteria TYPE_A, TYPE_B or TYPE_C
* @param foodID_ Food: The type of bacteria this one eats
* @param double propagationProb_ The propagation probability
*
*/
public EntityEden(int entityID_, int foodID_, double propagationProb_) {
_propagationProb = propagationProb_;
_entityID = entityID_;
_foodID = foodID_;
// define the color based on type
if (_entityID == TYPE_A) {
_rgbColor = Color.red.getRGB();
} else if (_entityID == TYPE_B) {
_rgbColor = Color.green.getRGB();
} else if (_entityID == TYPE_C) {
_rgbColor = Color.blue.getRGB();
} else {
Debug.error("EntityEden:"," Invalid Entity. entityID="+_entityID);
}
}
/** * getChild() returns a new copy of the entity when the entity
* flows from a neighboring cell.
* If the entity was immutable, we could just return
* a reference to the same EntityEden but because we will
* dynamically modify _isAlive, we have to return a
* copy of the entity.
* If a method is not supplied, then a clone will be
* returned, which is what this method does.
* But this is here to show that the method exists.
*
* @return EntityAbstract EntityEden implementation
*/
public EntityAbstract getChild() {
EntityEden e = null;
try {
e = (EntityEden)this.clone();
} catch (CloneNotSupportedException cnse) {
Debug.error("EntityEden.getChild","Error",cnse);
}
return e;
}
/**
* Returns the _alive boolean value.
* @return boolean
*/
final boolean isAlive() {
return ! _dead;
}
/**
* getPropProb()()
* returns the propagation probability for this instance
* @return double
*/
public double getPropProb() {
return _propagationProb;
}
/**
* Returns the foodID.
* @return int
*/
public int getFoodID() {
return _foodID;
}
/**
* Sets the foodID.
* @param foodID The foodID to set
*/
public void setFoodID(int foodID) {
_foodID = foodID;
}
/**
* Sets _propagationProb.
* @param propProb The propagationProb to set
*/
public final void setPropProb(double propagationProb) {
_propagationProb = propagationProb;
}
/**
* Sets the alive.
* @param alive true if alive, false if died.
*/
final void setAlive(boolean alive) {
_dead = ! alive;
} /****************************************************************
** getXML() **
*******************/
/**
*
* Generates an XML representation of an entity object.
* It will be appended to the EntityElement by the XMLOPCCollectionWriter
* These elements contain any data unique to a class
* extending EntityAbstract (this class).
*
* The XML support will generate something like the following for
* the EntityAbstract object. Note that the code in this method has
* added "food="3" to the attributes.
*
*
* <entityAbstract Id="5" color="16711680" \
prob="0.4" food="3">
* <class name="class com.ibm.almaden.\
smartgrid.apps.eden.EntityEden" />
* </entityAbstract>
*
*
*
* This element must contain all data unique to a class
* extending entityAbstract. The first thing the getXML() \
method implementation
* should do in any class extending entityAbstract is to call
* the getXmlElement() method from its superclass to get the basic
* xml for an abstract entity.
*
* Uses the libraries defining Element defined in org.jdom
*
* @see com.ibm.almaden.smartgrid.EntityAbstract
*
*
* @return Element Returns the XML representation for an OPC entity
*
*
*****************************************************************
*/
public Element getXML() {
// It must first call the getXMLElement for the entityAbstract
Element entityElement = super.getXmlElement();
// now add the foodID attribute to the XML.
entityElement.setAttribute(PROBABILITY, "" + this._propagationProb);
entityElement.setAttribute(FOOD_ID, "" + this._foodID);
return entityElement;
}// getXML()
/*************************************************************
** initFromXML() **
*******************/
/**
** This generates a new EntityEden instance from
* an XML representation of the entity.
*
* This method initializes an EntityEden object based on the
* XML that was created by the getXML() method.
* Using the following line of XML, the EntityEden instance
* will be recreated including the "foodID value that this class
* defines.
*
*
* <entityAbstract Id="5" color="16711680" prob="0.4" food="3">
* <class name="class com.ibm.almaden.smartgrid.\
apps.eden.EntityEden" />
* </entityAbstract>
*
*
** Generates a new EntityEden instance from an XML element
* The first thing any entity.initFromXML() method must do
* is call super. initCommonSubsetFromXML() to initialize * those entities characteristic of any class extending
* entityAbstract.
*
* Uses the libraries
* org.jdom.Element;
*
* @see com.ibm.almaden.smartgrid.EntityAbstract
*
* @param entityElement_ the xml representation of an entity object
*
*
*
***********************************************************
*/
public void initFromXML(Element entityElement_) {
super.initCommonSubsetFromXML(entityElement_);
this._propagationProb = getDoubleAttribute(entityElement_, PROBABILITY);
this._foodID = getIntAttribute(entityElement_, FOOD_ID);
}// initFromXMLL()
} // Class entityEden

Code listing for PropertyEden class

Following is the complete code for the PropertyEden class.

// PropertyEden.java
package com.ibm.almaden.smartgrid.apps.eden;
/*
** Licensed Materials - Property of IBM
**
** (C) COPYRIGHT IBM Corp. 2002 All rights reserved
**
** US Government Users Restricted Rights - Use, duplication or
** disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
**
*/
import org.jdom.Element;
import com.ibm.almaden.smartgrid.entityAbstract;
import com.ibm.almaden.smartgrid.PropertyAbstract;
// |||||||||||||||||||||||||||||||||||||||||||||||||||||||
// || Class PropertyEden ||
// |||||||||||||||||||
/**
* The Property class defines any properties that may apply to
* the OPC or its entities.
* In the case of the Eden model, the propertyEden class is
* used in the propagation
* of the entityEden objects.
* If the PropertyId matches the entry ID then the
* propagation probability is increased by the probability value.
* Otherwise it is decreased.
* @see PropertyAbstract
* @see entityAbstract
* @see EntityEden
**
** @author James Kaufman
** @version $Revision: 1.3 $ $Date: 2003/05/22 15:19:32 $
**
*/ public class PropertyEden extends PropertyAbstract {
/**
* Probability of propagation adjustment
*/
private double _probability = 0.0;
/**
* empty constructor.
* Required for XML support.
*/
public PropertyEden() {
super.setPropertyID(0);
super.setPropertyColor(255);
this.setProbability(0.0);
}
/**
* constructor
*
* @param propertyID_ The unique Id associated with this property
* @param probability_ The adjustment to the random probability
* used when determining the propagation of the
* EntityEden with the same ID value.
*
*/
public PropertyEden(int propertyID_, double probability_) {
super.setPropertyID(propertyID_);
super.setPropertyColor(255);
this.setProbability(probability_);
}
/**
* Returns the Probability.
* @return double
*/
public final double getProbability() {
return _probability;
}
/**
* Sets the Probability.
* @param probability The Probability to set
*/
public final void setProbability(double probability_) {
_probability = probability_;
}
/******************************************************
** getXML() **
*******************/
/**
* Generates an Element with the basic information required
* to define a property in the Eden model
* The first thing the getXML() method implementation
* should do in any class extending property abstract is to call
* the getXmlElement() method from its superclass to get the basic
* xml for an abstract property.
*
* Uses the libraries
* org.jdom.Document;
* org.jdom.Element; *
* @see com.ibm.almaden.smartgrid.PropertyAbstract
*
* @return propertyElement the XML representation of
* an abstract property
*
*
*******************************************************
*/
public Element getXML() {
Element propertyElement = super.getXmlElement();
propertyElement.setAttribute(PROBABILITY, "" + this._probability);
return propertyElement;
}// getXML()
/******************************************************
** initFromXML() **
*******************/
/**
** Required method. Generates a new PropertyEden instance from
* an XML representation of the property.
* The first thing any property.initFromXML() method must do
* is call super. initCommonSubsetFromXML() to initialize
* those properties characteristic of any class extending
* propertyAbstract.
*
* Uses the libraries
* org.jdom.Element;
*
* @see com.ibm.almaden.smartgrid.PropertyAbstract
*
* @param propertyElement_ the xml representation of a property object
*
*******************************************************
*/
public void initFromXML(Element propertyElement_) {
super.initCommonSubsetFromXML(propertyElement_);
this._probability = \
getDoubleAttribute(propertyElement_, PROBABILITY);
}// initFromXMLL()
} // Class Property

Code Listing for VppEdenInitializer class

The following is the complete code for the VppEdenInitializer class.


// VppEdenInitializer3D.java
package com.ibm.almaden.smartgrid.apps.eden;
/*
** Licensed Materials - Property of IBM
**
** (C) COPYRIGHT IBM Corp. 2002 All rights reserved
**
** US Government Users Restricted Rights - Use, ** duplication or disclosure restricted
** by GSA ADP Schedule Contract with IBM Corp.
**
*/
import java.util.Random;
import java.util.Vector;
import com.ibm.almaden.smartgrid.Constants;
import com.ibm.almaden.smartgrid.EntityAbstract;
import com.ibm.almaden.smartgrid.OPCAbstract; import com.ibm.almaden.smartgrid.OPCCollection;
import com.ibm.almaden.smartgrid.SmartGrid;
import com.ibm.almaden.smartgrid.VppDataInitializerAbstract;
import com.ibm.almaden.smartgrid.SGSystemConfig;
import com.ibm.almaden.smartgrid.VppInfo;
import com.ibm.almaden.smartgrid.util.ConfigFile;
import com.ibm.almaden.smartgrid.util.Debug;
// ||||||||||||||||||||||||||||||||||||||||||||||||||
// || Class VppEdenInitializer ||
// ||||||||||||||||||||||||||
/**
**
** The VppEdenInitializer is responsible for setting the initial
** state of a Vpp Instances created by the ProblemBuilder.
*
* This class extends VPPDataInitializerAbstract to do any initialization
* that is unique to our problem. In this case, it will add one of each type
* of bacteria to a collection in the first VPP.
* It will check the eden.cfg file for specified settings and if not found
* then it will randomly choose the OPCs to be initialized.
*
**
** @see com.ibm.almaden.smartgrid.pb.ProblemAutoBuilder
** @see com.ibm.almaden.smartgrid.SmartGrid
** @see com.ibm.almaden.smartgrid.OPCCollection
** @author James Kaufman
** @author Toby Lehman
** @version $Revision: 1.3 $ $Date: 2003/05/22 15:19:32 $
*/
// ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
public class VppEdenInitializer extends VppDataInitializerAbstract {
/**
*** Classname for tracing and debugging
**/
public static final String _className = "VppEdenInitializer";
/**
** We need this for various random numbers.
**
** random number generation
**/
public Random _rnd = new Random(Constants.RANDOM_NUMBER_SEED);
/**
* Some sample entity instances.
*
*/
public EntityEden _eA = EntityEden.ENTITY_A;
public EntityEden _eB = EntityEden.ENTITY_B;
public EntityEden _eC = EntityEden.ENTITY_C;
/*
*************************************************************
** VppEdenInitializer **
****************
*/
/**
** Constructor with the Problem configuration information
*
* @param config_ Reference to the configuration information
*
*************************************************************
*/
public VppEdenInitializer(SGSystemConfig config_) {
super(config_);
} // Constructor() /************************************************************
** initialize **
********************/
/**
*** Add The initial entities.
* This is called for each of the VPPs that are created by the
* Problem Builder.
*
* @param vppInfo_ VppInfo object describing the VPP that is
* being initialized.
*** @param collectionVect_ Vector of OPCCollection objects.
***
********************************
**/
public void initialize(VppInfo vppInfo_, Vector collectionVect_) {
String methodName = _className + ".initialize: ";
// Only initialize the first VPP
if (vppInfo_._vppNumber == 0) {
// check if index to initialize is set in config entry
ConfigFile problemCfg = _config.getProblemConfigFile();
//
// look for setting in eden.cfg file
// Stanza: [problem] Attribute: SetA
// default to -1 if not found.
int setA = problemCfg.getInt("problem","SetA",-1);
int setB = problemCfg.getInt("problem","SetB",-1);
int setC = problemCfg.getInt("problem","SetC",-1);
this.seedVPP(collectionVect_, _eA,setA);
this.seedVPP(collectionVect_, _eB,setB);
this.seedVPP(collectionVect_, _eC,setC);
}
} // initialize
/************************************************************
** seedVPP **
********************/
/**
* Seed the VPP with some initial entities.
*
* This code selects a middle OPCCollections
* (number/2) and adds
* the specified entity to a random OPC in that
* OPCCollection.
*
* In the future we'd like to add some control
* to the random placement of the coordinates.
*
* @param collectionVect_ Vector of OPCCollection
* objects @param e_ Entity object we will add to a
* random OPC in one of the OPCCollections
***
*** @return void
*************************************************************
**/
private void seedVPP(Vector \
collectionVect_, EntityEden e_, int index_) {
String methodName = _className + ".seedVPP: ";
// loop through the collections
int numCollections = collectionVect_.size();
int middle = numCollections / 2;
// find the middle one
OPCCollection OPCCollection = \
(OPCCollection) collectionVect_.elementAt(middle);
int numOPC = OPCCollection._collectionArray.length;
int index = index_;
if (index < 0 || index >= numOPC) {
// use random OPC
index = _rnd.nextInt(numOPC);
}
OPCAbstract OPC = (OPCAbstract) \
OPCCollection._collectionArray[index];
Debug.msg(methodName, " ***** Seeding collection " + \
middle + " at OPC # = " + index + " ********"); super.addOccupants(OPC, e_);
return;
} // seedVPP()
} // end VppEdenInitializer class

Code listing for AppUtilsGeneric class

The following is the complete code for the AppUtilsGeneric class.

// AppUtilsGeneric.java
package com.ibm.almaden.smartgrid;
/*
** Licensed Materials - Property of IBM
**
** (C) COPYRIGHT IBM Corp. 2002 All rights reserved
**
** US Government Users Restricted Rights - Use, duplication or
** disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
**
*/
import java.io.Serializable;
import com.ibm.almaden.smartgrid.util.ConfigFile;
// |||||||||||||||||||||||||||||||||||||||||||||||||||||||
// || Class AppUtilsGeneric ||
// |||||||||||||||||||||||
/**
* This class is provided to hold utility methods and data that is shared
* by all of the OPCs. This particular class is only a placeholder
* and example. An application programmer would write their own
* implementation of AppUtilsAbstract. The OPC methods can
refer to method * and data in this class as a static reference
in OPCAbstract.
*
* For Example
*
* AppUtilsGeneric utils = \
(AppUtilsGeneric)OPCAbstract.getAppUtils();
* long x =utils.example(10,5);
*
* @see AppUtilsAbstract for additional details.
* @author James Kaufman
* @author John Thomas
*
**
**
**/
// ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
public class AppUtilsGeneric
extends AppUtilsAbstract implements Serializable {
static final long serialVersionUID = -5934169214775107543L;
long runtimeValue = 0;
/**********************************************************
** AppUtilsGeneric ** (constructor)
**************/
/**
* Constructor for the implementation of the AppUtilAbstract class
*
* @param sysConfigFile_ Reference to the grid.cfg contents * @param problemConfigFile_ Reference to the problem.cfg contents
***********************************************************
*/
public AppUtilsGeneric(ConfigFile \
sysConfigFile_,ConfigFile problemConfigFile_) {
super(sysConfigFile_,problemConfigFile_); // \
set the system config File
runtimeValue = problemConfigFile_.getLong \
("Problem","Runtimevalue",0);
}
/**********************************************************
** example **
**************/
/**
* An example of a utility method using a value originally
* obtained from Config File
*
* @param arg1_ argument 1
* @param arg2 argument 2
* @return value
***********************************************************
*/
public final long example(long arg1_, long arg2_) {
return (arg1_+arg2_)*runtimeValue;
}
} // Class AppUtilsGeneric

Resources

About the authors

Glenn Deen

Glenn Deen is a senior software engineer and a member of the OptimalGrid research team at the IBM Almaden Research Center. Since joining IBM in 1989, he has been involved in security architecture and distributed computing. His interests include XML and grid computing.

James Kaufman

James Kaufman is a research staff member in the Distributed and Cluster Systems Department at the IBM Almaden Research Center. He received a bachelor's degree in physics from Cornell University and a doctorate in physics from UCSB. He has made contributions to several fields of research at IBM. He is a Fellow of the American Physical Society. His research interests include distributed computing, simulation and modeling, and grid middleware.

Tobin Lehman

Tobin (Toby) Lehman joined the IBM Almaden Research Center in 1986, shortly after finishing his doctorate at the University of Wisconsin-Madison. His research interests include server-based backup systems, object-relational database systems, large object management, memory-resident database systems, Tuplespace systems, and computing grids. He is working on an autonomic grid computing infrastructure for solving large connected problems on a heterogeneous collection of Internet machines.

John Thomas

John Thomas is a Java developer for IBM. He was previously one of the lead programmers for the IBM Almaden TSpaces project. He is a member of the OptimalGrid project at the Almaden Research Center, working out of his home in Santa Cruz, Calif.

(Copyright IBM Corporation)


 
 

 
     
 
Công nghệ khác:


A practical introduction to OptimalGrid - Part IVA practical introduction to OptimalGrid - Part III
A practical introduction to OptimalGrid - Part IIA practical introduction to OptimalGrid - Part I
Những Keyboard cho dân chơi Game - phần 1Kiến trúc đa nhân, thời đại mới của vi xử lý (Phần II)
  Xem tiếp    
 
Lịch khai giảng của hệ thống
 
Ngày
Giờ
T.Tâm
TP Hồ Chí Minh
Hà Nội
 
   
New ADSE - Nhấn vào để xem chi tiết
Mừng Sinh Nhật Lần Thứ 20 FPT-APTECH
Nhấn vào để xem chi tiết
Bảng Vàng Thành Tích Sinh Viên FPT APTECH - Nhấn vào để xem chi tiết
Cập nhật công nghệ miễn phí cho tất cả cựu sinh viên APTECH toàn quốc
Tiết Thực Vì Cộng Đồng
Hội Thảo CNTT
Những khoảnh khắc không phai của Thầy Trò FPT-APTECH Ngày 20-11