14 #include "TopoManager.h"
16 // Hash table mostly based on open hash table from Introduction to
17 // Algorithms by Cormen, Leiserson, and Rivest
19 // Moved comparison function to LDObjIDEqual
21 // static inline bool ObjIDEqual(const LDObjid i1, const LDObjid i2)
23 // return (bool)(i1.id[0] == i2.id[0]
24 // && i1.id[1] == i2.id[1] && i1.id[2] == i2.id[2]
25 // && i1.id[3] == i2.id[3]);
28 LBCommData* LBCommTable::HashInsert(const LBCommData &data)
30 if (in_use > cur_sz/2)
35 j = data.hash(i,cur_sz);
36 // CmiPrintf("Hashing to %d, %d %d\n",j,i,cur_sz);
37 if (state[j] == nil) {
43 } while (i != cur_sz);
45 // No room for item, but I should never get here, because I would have
47 CmiPrintf("HashInsert Couldn't insert!\n");
51 LBCommData* LBCommTable::HashSearch(const LBCommData &data)
56 j = data.hash(i,cur_sz);
57 if (state[j] != nil && set[j].equal(data)) {
61 } while (state[j] != nil && i != cur_sz);
65 LBCommData* LBCommTable::HashInsertUnique(const LBCommData &data)
67 LBCommData* item = HashSearch(data);
69 item = HashInsert(data);
74 void LBCommTable::Resize()
76 LBCommData* old_set = set;
77 TableState* old_state = state;
81 for(int i=0; i < old_sz; i++) {
82 if (old_state[i] == InUse)
83 HashInsert(old_set[i]);
89 bool LBCommData::equal(const LBCommData &d2) const
92 if (src_proc != d2.src_proc)
95 if (srcObj.omID() != d2.srcObj.omID()
96 || srcObj.objID() != d2.srcObj.objID() )
99 return (bool)(destObj == d2.destObj);
102 int LBCommData::compute_key()
105 char* kptr = (char*)((void*)(&(kstring[0])));
109 pcount = sprintf(kptr,"%d",src_proc);
112 pcount = sprintf(kptr,"%d%" PRIu64 "",srcObj.omID().id.idx,
117 //CmiAssert(destObj.get_type() == LD_OBJ_MSG);
118 switch (destObj.get_type()) {
120 pcount += sprintf(kptr,"%d", destObj.proc());
123 LDObjKey &destKey = destObj.get_destObj();
124 pcount += sprintf(kptr,"%d%" PRIu64 "XXXXXXXX",destKey.omID().id.idx,
126 pcount -= 8; /* The 'X's insure that the next few bytes are fixed */
129 case LD_OBJLIST_MSG: {
131 const LDObjKey *destKeys = destObj.get_destObjs(len);
133 pcount += sprintf(kptr,"%d%" PRIu64 "XXXXXXXX",destKeys[0].omID().id.idx,
134 destKeys[0].objID());
135 pcount -= 8; /* The 'X's insure that the next few bytes are fixed */
141 for(int i=0; i < (pcount+3)/4; i++)
144 // CmiPrintf("New key %d, %s\n",k,kstring);
149 int LBCommData::hash(const int i, const int m) const
151 const double a = 0.6803398875;
153 const double ka = k * a;
155 int h1 = (int) floor(m*(ka-floor(ka)));
156 int h2 = 1; // Should be odd, to guarantee that h2 and size of table
157 // are relatively prime.
159 // CmiPrintf("k=%d h1=%d h2=%d m=%d\n",k,h1,h2,m);
160 return (h1 + i * h2) % m;
163 void LBCommTable::GetCommData(LDCommData* data)
165 LDCommData* out=data;
166 LBCommData* curtable=set;
167 TableState* curstate=state;
170 for(i=0; i < cur_sz; i++, curtable++, curstate++) {
171 if (*curstate == InUse) {
173 if (curtable->from_proc()) {
174 out->src_proc = curtable->src_proc;
177 out->sender.omID() = curtable->srcObj.omID();
178 out->sender.objID() = curtable->srcObj.objID();
180 out->receiver = curtable->destObj;
181 out->messages = curtable->n_messages;
182 out->bytes = curtable->n_bytes;
188 struct LDCommDescComp {
189 bool operator() (const LDCommDesc& lhs, const LDCommDesc &rhs) const {
190 return (lhs.get_destObj() < rhs.get_destObj());
194 void LBCommTable::GetCommInfo(int& bytes, int& msgs, int& outsidepemsgs, int&
195 outsidepebytes, int& num_nghbor, int& hops, int& hopbytes) {
197 LBCommData* curtable=set;
198 TableState* curstate=state;
206 std::set<LDCommDesc, LDCommDescComp> num_neighbors;
210 for(i=0; i < cur_sz; i++, curtable++, curstate++) {
211 if (*curstate == InUse) {
212 msgs += curtable->n_messages;
213 bytes += curtable->n_bytes;
214 if (curtable->destObj.get_type() == LD_OBJ_MSG) {
215 num_neighbors.insert(curtable->destObj);
218 if (curtable->destObj.lastKnown() != CkMyPe()) {
219 outsidepebytes += curtable->n_bytes;
220 outsidepemsgs += curtable->n_messages;
221 if(curtable->destObj.lastKnown()>=0 && curtable->destObj.lastKnown()<CkNumPes()){
222 TopoManager_getHopsBetweenPeRanks(CkMyPe(), curtable->destObj.lastKnown(), &h);
223 hops += curtable->n_messages * h;
224 hopbytes += curtable->n_bytes * h;
229 num_nghbor = num_neighbors.size();
232 #endif // CMK_LBDB_ON