001: /**
002: * This file or a portion of this file is licensed under the terms of
003: * the Globus Toolkit Public License, found at $PEGASUS_HOME/GTPL or
004: * http://www.globus.org/toolkit/download/license.html.
005: * This notice must appear in redistributions of this file
006: * with or without modification.
007: *
008: * Redistributions of this Software, with or without modification, must reproduce
009: * the GTPL in:
010: * (1) the Software, or
011: * (2) the Documentation or
012: * some other similar material which is provided with the Software (if any).
013: *
014: * Copyright 1999-2004
015: * University of Chicago and The University of Southern California.
016: * All rights reserved.
017: */package org.griphyn.cPlanner.selector.site.heft;
018:
019: import org.griphyn.cPlanner.partitioner.graph.Bag;
020:
021: /**
022: * A data class that implements the Bag interface and stores the extra information
023: * that is required by the HEFT algorithm for each node.
024: *
025: * @author Karan Vahi
026: * @version $Revision: 298 $
027: */
028: public class HeftBag implements Bag {
029:
030: /**
031: * Array storing the names of the attributes that are stored with the
032: * site.
033: */
034: public static final String HEFTINFO[] = { "avg-compute-time",
035: "downward-rank", "upward-rank", "start-time", "end-time",
036: "scheduled-site" };
037:
038: /**
039: * The constant to be passed to the accessor functions to get or set the
040: * average compute time for the node.
041: */
042: public static final Integer AVG_COMPUTE_TIME = new Integer(0);
043:
044: /**
045: * The constant to be passed to the accessor functions to get or set the
046: * downward rank for a node.
047: */
048: public static final Integer DOWNWARD_RANK = new Integer(1);
049:
050: /**
051: * The constant to be passed to the accessor functions to get or set the
052: * upward rank for a node.
053: */
054: public static final Integer UPWARD_RANK = new Integer(2);
055:
056: /**
057: * The constant to be passed to the accessor functions to get or set the
058: * actual start time for a job.
059: */
060: public static final Integer ACTUAL_START_TIME = new Integer(3);
061:
062: /**
063: * The constant to be passed to the accessor functions to get or set the
064: * actual end time for a job.
065: */
066: public static final Integer ACTUAL_FINISH_TIME = new Integer(4);
067:
068: /**
069: * The site where the job is scheduled.
070: */
071: public static final Integer SCHEDULED_SITE = new Integer(5);
072:
073: /**
074: * The average compute time for a node.
075: */
076: private float mAvgComputeTime;
077:
078: /**
079: * The downward rank for a node.
080: */
081: private float mDownwardRank;
082:
083: /**
084: * The upward rank for a node.
085: */
086: private float mUpwardRank;
087:
088: /**
089: * The estimated start time for a job.
090: */
091: private long mStartTime;
092:
093: /**
094: * The estimated end time for a job.
095: */
096: private long mEndTime;
097:
098: /**
099: * The site where a job is scheduled to run.
100: */
101: private String mScheduledSite;
102:
103: /**
104: * The default constructor.
105: */
106: public HeftBag() {
107: mAvgComputeTime = 0;
108: mDownwardRank = 0;
109: mUpwardRank = 0;
110: mStartTime = 0;
111: mEndTime = 0;
112: mScheduledSite = "";
113: }
114:
115: /**
116: * Adds an object to the underlying bag corresponding to a particular key.
117: *
118: * @param key the key with which the value has to be associated.
119: * @param value the value to be associated with the key.
120: *
121: * @return boolean indicating if insertion was successful.
122: *
123: */
124: public boolean add(Object key, Object value) {
125: boolean result = true;
126:
127: int k = getIntValue(key);
128:
129: float fv = 0;
130: long lv = 0;
131:
132: //short cut for scheduled site
133: if (k == this .SCHEDULED_SITE) {
134: mScheduledSite = (String) value;
135: return result;
136: }
137:
138: //parse the value correctly
139: switch (k) {
140: case 0:
141: case 1:
142: case 2:
143: fv = ((Float) value).floatValue();
144: break;
145: case 3:
146: case 4:
147: lv = ((Long) value).longValue();
148: break;
149: default:
150: }
151:
152: switch (k) {
153: case 0:
154: this .mAvgComputeTime = fv;
155: break;
156:
157: case 1:
158: this .mDownwardRank = fv;
159: break;
160:
161: case 2:
162: this .mUpwardRank = fv;
163: break;
164:
165: case 3:
166: this .mStartTime = lv;
167: break;
168:
169: case 4:
170: this .mEndTime = lv;
171: break;
172:
173: default:
174: result = false;
175: }
176:
177: return result;
178:
179: }
180:
181: /**
182: * Returns true if the namespace contains a mapping for the specified key.
183: *
184: * @param key The key that you want to search for in the bag.
185: *
186: * @return boolean
187: */
188: public boolean containsKey(Object key) {
189:
190: int k = -1;
191: try {
192: k = ((Integer) key).intValue();
193: } catch (Exception e) {
194: }
195:
196: return (k >= this .AVG_COMPUTE_TIME.intValue() && k <= this .UPWARD_RANK
197: .intValue());
198: }
199:
200: /**
201: * Returns an objects corresponding to the key passed.
202: *
203: * @param key the key corresponding to which the objects need to be
204: * returned.
205: *
206: * @return the object that is found corresponding to the key or null.
207: */
208: public Object get(Object key) {
209:
210: int k = getIntValue(key);
211:
212: switch (k) {
213: case 0:
214: return this .mAvgComputeTime;
215:
216: case 1:
217: return this .mDownwardRank;
218:
219: case 2:
220: return this .mUpwardRank;
221:
222: case 3:
223: return this .mStartTime;
224:
225: case 4:
226: return this .mEndTime;
227:
228: case 5:
229: return this .mScheduledSite;
230:
231: default:
232: throw new RuntimeException(
233: " Wrong Heft key. Please use one of the predefined key types "
234: + key);
235: }
236: }
237:
238: /**
239: * A convenience method to get the intValue for the object passed.
240: *
241: * @param key the key to be converted
242: *
243: * @return the int value if object an integer, else -1
244: */
245: private int getIntValue(Object key) {
246:
247: int k = -1;
248: try {
249: k = ((Integer) key).intValue();
250: } catch (Exception e) {
251: }
252:
253: return k;
254:
255: }
256: }
|