/home/runner/work/charm/charm/src/ck-ldb/LBComm.C
Line | Count | Source (jump to first uncovered line) |
1 | | /** |
2 | | * \addtogroup CkLdb |
3 | | */ |
4 | | /*@{*/ |
5 | | |
6 | | #include "charm-config.h" |
7 | | #include "converse.h" |
8 | | |
9 | | #if CMK_LBDB_ON |
10 | | |
11 | | #include <math.h> |
12 | | #include "LBComm.h" |
13 | | #include <set> |
14 | | |
15 | | #include "TopoManager.h" |
16 | | |
17 | | // Hash table mostly based on open hash table from Introduction to |
18 | | // Algorithms by Cormen, Leiserson, and Rivest |
19 | | |
20 | | // Moved comparison function to LDObjIDEqual |
21 | | |
22 | | // static inline bool ObjIDEqual(const LDObjid i1, const LDObjid i2) |
23 | | // { |
24 | | // return (bool)(i1.id[0] == i2.id[0] |
25 | | // && i1.id[1] == i2.id[1] && i1.id[2] == i2.id[2] |
26 | | // && i1.id[3] == i2.id[3]); |
27 | | // }; |
28 | | |
29 | | LBCommData* LBCommTable::HashInsert(const LBCommData &data) |
30 | 537k | { |
31 | 537k | if (in_use > cur_sz/2) |
32 | 163 | Resize(); |
33 | 537k | int i = 0; |
34 | 537k | int j; |
35 | 12.2M | do { |
36 | 12.2M | j = data.hash(i,cur_sz); |
37 | | // CmiPrintf("Hashing to %d, %d %d\n",j,i,cur_sz); |
38 | 12.2M | if (state[j] == nil) { |
39 | 538k | state[j] = InUse; |
40 | 538k | set[j] = data; |
41 | 538k | in_use++; |
42 | 538k | return &set[j]; |
43 | 11.6M | } else i++; |
44 | 12.2M | } while (i != cur_sz); |
45 | | |
46 | | // No room for item, but I should never get here, because I would have |
47 | | // resized the list |
48 | 18.4E | CmiPrintf("HashInsert Couldn't insert!\n"); |
49 | 18.4E | return 0; |
50 | 537k | } |
51 | | |
52 | | LBCommData* LBCommTable::HashSearch(const LBCommData &data) |
53 | 1.53M | { |
54 | 1.53M | int i=0; |
55 | 1.53M | int j; |
56 | 10.6M | do { |
57 | 10.6M | j = data.hash(i,cur_sz); |
58 | 10.6M | if (state[j] != nil && set[j].equal(data)) { |
59 | 1.31M | return &set[j]; |
60 | 1.31M | } |
61 | 9.34M | i++; |
62 | 9.34M | } while (state[j] != nil && i != cur_sz); |
63 | 216k | return 0; |
64 | 1.53M | } |
65 | | |
66 | | LBCommData* LBCommTable::HashInsertUnique(const LBCommData &data) |
67 | 1.54M | { |
68 | 1.54M | LBCommData* item = HashSearch(data); |
69 | 1.54M | if (!item) { |
70 | 222k | item = HashInsert(data); |
71 | 222k | } |
72 | 1.54M | return item; |
73 | 1.54M | } |
74 | | |
75 | | void LBCommTable::Resize() |
76 | 163 | { |
77 | 163 | LBCommData* old_set = set; |
78 | 163 | TableState* old_state = state; |
79 | 163 | int old_sz = cur_sz; |
80 | | |
81 | 163 | NewTable(old_sz*2); |
82 | 631k | for(int i=0; i < old_sz; i++) { |
83 | 631k | if (old_state[i] == InUse) |
84 | 315k | HashInsert(old_set[i]); |
85 | 631k | } |
86 | 163 | delete [] old_set; |
87 | 163 | delete [] old_state; |
88 | 163 | } |
89 | | |
90 | | bool LBCommData::equal(const LBCommData &d2) const |
91 | 10.4M | { |
92 | 10.4M | if (from_proc()) { |
93 | 179k | if (src_proc != d2.src_proc) |
94 | 1.39k | return false; |
95 | 10.2M | } else { |
96 | 10.2M | if (srcObj.omID() != d2.srcObj.omID() |
97 | 10.2M | || srcObj.objID() != d2.srcObj.objID() ) |
98 | 9.12M | return false; |
99 | 10.2M | } |
100 | 1.32M | return (bool)(destObj == d2.destObj); |
101 | 10.4M | } |
102 | | |
103 | | int LBCommData::compute_key() |
104 | 1.54M | { |
105 | 1.54M | char kstring[320]; |
106 | 1.54M | char* kptr = &kstring[0]; |
107 | 1.54M | int* kintarr = (int*)((void*)(&kstring[0])); |
108 | 1.54M | int pcount; |
109 | | |
110 | 1.54M | if (from_proc()) { |
111 | 185k | pcount = snprintf(kptr,sizeof(kstring),"%d",src_proc); |
112 | 185k | kptr += pcount; |
113 | 1.35M | } else { |
114 | 1.35M | pcount = snprintf(kptr,sizeof(kstring),"%d%" PRIu64 "",srcObj.omID().id.idx, |
115 | 1.35M | srcObj.id); |
116 | 1.35M | kptr += pcount; |
117 | 1.35M | } |
118 | | |
119 | | //CmiAssert(destObj.get_type() == LD_OBJ_MSG); |
120 | 1.54M | switch (destObj.get_type()) { |
121 | 0 | case LD_PROC_MSG: |
122 | 0 | pcount += snprintf(kptr, sizeof(kstring) - pcount, "%d", destObj.proc()); |
123 | 0 | break; |
124 | 1.53M | case LD_OBJ_MSG: { |
125 | 1.53M | LDObjKey &destKey = destObj.get_destObj(); |
126 | 1.53M | pcount += snprintf(kptr, sizeof(kstring) - pcount, "%d%" PRIu64 "XXXXXXXX",destKey.omID().id.idx, |
127 | 1.53M | destKey.objID()); |
128 | 1.53M | pcount -= 8; /* The 'X's insure that the next few bytes are fixed */ |
129 | 1.53M | break; |
130 | 0 | } |
131 | 120 | case LD_OBJLIST_MSG: { |
132 | 120 | int len; |
133 | 120 | const LDObjKey *destKeys = destObj.get_destObjs(len); |
134 | 120 | CmiAssert(len>0); |
135 | 120 | pcount += snprintf(kptr, sizeof(kstring) - pcount, "%d%" PRIu64 "XXXXXXXX",destKeys[0].omID().id.idx, |
136 | 120 | destKeys[0].objID()); |
137 | 120 | pcount -= 8; /* The 'X's insure that the next few bytes are fixed */ |
138 | 120 | break; |
139 | 0 | } |
140 | 1.54M | } |
141 | | |
142 | 1.55M | int k=-1; |
143 | 4.81M | for(int i=0; i < (pcount+sizeof(int)-1)/sizeof(int); i++) |
144 | 3.25M | k ^= kintarr[i]; |
145 | | |
146 | | // CmiPrintf("New key %d, %s\n",k,kstring); |
147 | | |
148 | 1.55M | return k; |
149 | 1.54M | } |
150 | | |
151 | | int LBCommData::hash(const int i, const int m) const |
152 | 22.6M | { |
153 | 22.6M | const double a = 0.6803398875; |
154 | 22.6M | const int k = key(); |
155 | 22.6M | const double ka = k * a; |
156 | | |
157 | 22.6M | int h1 = (int) floor(m*(ka-floor(ka))); |
158 | 22.6M | int h2 = 1; // Should be odd, to guarantee that h2 and size of table |
159 | | // are relatively prime. |
160 | | |
161 | | // CmiPrintf("k=%d h1=%d h2=%d m=%d\n",k,h1,h2,m); |
162 | 22.6M | return (h1 + i * h2) % m; |
163 | 22.6M | } |
164 | | |
165 | | void LBCommTable::GetCommData(LDCommData* data) |
166 | 362 | { |
167 | 362 | LDCommData* out=data; |
168 | 362 | LBCommData* curtable=set; |
169 | 362 | TableState* curstate=state; |
170 | 362 | int i; |
171 | | |
172 | 144k | for(i=0; i < cur_sz; i++, curtable++, curstate++) { |
173 | 144k | if (*curstate == InUse) { |
174 | 2.51k | out->clearHash(); |
175 | 2.51k | if (curtable->from_proc()) { |
176 | 575 | out->src_proc = curtable->src_proc; |
177 | 1.93k | } else { |
178 | 1.93k | out->src_proc = -1; |
179 | 1.93k | out->sender.omID() = curtable->srcObj.omID(); |
180 | 1.93k | out->sender.objID() = curtable->srcObj.objID(); |
181 | 1.93k | } |
182 | 2.51k | out->receiver = curtable->destObj; |
183 | 2.51k | out->messages = curtable->n_messages; |
184 | 2.51k | out->bytes = curtable->n_bytes; |
185 | 2.51k | out++; |
186 | 2.51k | } |
187 | 144k | } |
188 | 362 | } |
189 | | |
190 | | struct LDCommDescComp { |
191 | 0 | bool operator() (const LDCommDesc& lhs, const LDCommDesc &rhs) const { |
192 | 0 | return (lhs.get_destObj() < rhs.get_destObj()); |
193 | 0 | } |
194 | | }; |
195 | | |
196 | | void LBCommTable::GetCommInfo(int& bytes, int& msgs, int& outsidepemsgs, int& |
197 | 0 | outsidepebytes, int& num_nghbor, int& hops, int& hopbytes) { |
198 | |
|
199 | 0 | LBCommData* curtable=set; |
200 | 0 | TableState* curstate=state; |
201 | 0 | int i; |
202 | 0 | bytes = 0; |
203 | 0 | msgs = 0; |
204 | 0 | outsidepemsgs = 0; |
205 | 0 | outsidepebytes = 0; |
206 | 0 | hops = 0; |
207 | 0 | hopbytes = 0; |
208 | 0 | std::set<LDCommDesc, LDCommDescComp> num_neighbors; |
209 | |
|
210 | 0 | int h; |
211 | |
|
212 | 0 | for(i=0; i < cur_sz; i++, curtable++, curstate++) { |
213 | 0 | if (*curstate == InUse) { |
214 | 0 | msgs += curtable->n_messages; |
215 | 0 | bytes += curtable->n_bytes; |
216 | 0 | if (curtable->destObj.get_type() == LD_OBJ_MSG) { |
217 | 0 | num_neighbors.insert(curtable->destObj); |
218 | 0 | } |
219 | |
|
220 | 0 | if (curtable->destObj.lastKnown() != CkMyPe()) { |
221 | 0 | outsidepebytes += curtable->n_bytes; |
222 | 0 | outsidepemsgs += curtable->n_messages; |
223 | 0 | if(curtable->destObj.lastKnown()>=0 && curtable->destObj.lastKnown()<CkNumPes()){ |
224 | 0 | TopoManager_getHopsBetweenPeRanks(CkMyPe(), curtable->destObj.lastKnown(), &h); |
225 | 0 | hops += curtable->n_messages * h; |
226 | 0 | hopbytes += curtable->n_bytes * h; |
227 | 0 | } |
228 | 0 | } |
229 | 0 | } |
230 | 0 | } |
231 | 0 | num_nghbor = num_neighbors.size(); |
232 | 0 | } |
233 | | |
234 | | #endif // CMK_LBDB_ON |
235 | | |
236 | | /*@}*/ |