Coverage Report

Created: 2026-11-15 13:46

/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
590k
{
31
590k
  if (in_use > cur_sz/2)
32
179
    Resize();
33
590k
  int i = 0;
34
590k
  int j;
35
11.5M
  do {
36
11.5M
    j = data.hash(i,cur_sz);
37
    //    CmiPrintf("Hashing to %d, %d %d\n",j,i,cur_sz);
38
11.5M
    if (state[j] == nil) {
39
591k
      state[j] = InUse;
40
591k
      set[j] = data;
41
591k
      in_use++;
42
591k
      return &set[j];
43
10.9M
    } else i++;
44
11.5M
  } 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
590k
}
51
52
LBCommData* LBCommTable::HashSearch(const LBCommData &data)
53
1.86M
{
54
1.86M
  int i=0;
55
1.86M
  int j;
56
11.4M
  do {
57
11.4M
    j = data.hash(i,cur_sz);
58
11.4M
    if (state[j] != nil && set[j].equal(data)) {
59
1.59M
      return &set[j];
60
1.59M
    }
61
9.86M
    i++;
62
9.86M
  } while (state[j] != nil && i != cur_sz);
63
267k
  return 0;
64
1.86M
}
65
66
LBCommData* LBCommTable::HashInsertUnique(const LBCommData &data)
67
1.87M
{
68
1.87M
  LBCommData* item = HashSearch(data);
69
1.87M
  if (!item) {
70
277k
    item = HashInsert(data);
71
277k
  }
72
1.87M
  return item;
73
1.87M
}
74
75
void LBCommTable::Resize()
76
179
{
77
179
  LBCommData* old_set = set;
78
179
  TableState* old_state = state;
79
179
  int old_sz = cur_sz;
80
81
179
  NewTable(old_sz*2);
82
627k
  for(int i=0; i < old_sz; i++) {
83
626k
    if (old_state[i] == InUse)
84
313k
      HashInsert(old_set[i]);
85
626k
  }
86
179
  delete [] old_set;
87
179
  delete [] old_state;
88
179
}  
89
90
bool LBCommData::equal(const LBCommData &d2) const
91
11.1M
{
92
11.1M
  if (from_proc()) {
93
205k
    if (src_proc != d2.src_proc)
94
10.9k
      return false;
95
10.9M
  } else {
96
10.9M
    if (srcObj.omID() != d2.srcObj.omID()
97
10.9M
  || srcObj.objID() != d2.srcObj.objID() )
98
9.56M
      return false;
99
10.9M
  }
100
1.61M
  return (bool)(destObj == d2.destObj);
101
11.1M
}
102
103
int LBCommData::compute_key()
104
1.88M
{
105
1.88M
  char kstring[320];
106
1.88M
  char* kptr = &kstring[0];
107
1.88M
  int* kintarr = (int*)((void*)(&kstring[0]));
108
1.88M
  int pcount;
109
110
1.88M
  if (from_proc()) {
111
214k
    pcount = snprintf(kptr,sizeof(kstring),"%d",src_proc);
112
214k
    kptr += pcount;
113
1.66M
  } else {
114
1.66M
    pcount = snprintf(kptr,sizeof(kstring),"%d%" PRIu64 "",srcObj.omID().id.idx,
115
1.66M
         srcObj.id);
116
1.66M
    kptr += pcount;
117
1.66M
  }
118
119
  //CmiAssert(destObj.get_type() == LD_OBJ_MSG);
120
1.88M
  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.86M
  case LD_OBJ_MSG: {
125
1.86M
       LDObjKey &destKey = destObj.get_destObj();
126
1.86M
       pcount += snprintf(kptr, sizeof(kstring) - pcount, "%d%" PRIu64 "XXXXXXXX",destKey.omID().id.idx,
127
1.86M
        destKey.objID());
128
1.86M
       pcount -= 8;  /* The 'X's insure that the next few bytes are fixed */
129
1.86M
       break;
130
0
       }
131
168
  case LD_OBJLIST_MSG: {
132
168
       int len;
133
168
       const LDObjKey *destKeys = destObj.get_destObjs(len);
134
168
       CmiAssert(len>0);
135
168
       pcount += snprintf(kptr, sizeof(kstring) - pcount, "%d%" PRIu64 "XXXXXXXX",destKeys[0].omID().id.idx,
136
168
        destKeys[0].objID());
137
168
       pcount -= 8;  /* The 'X's insure that the next few bytes are fixed */
138
168
       break;
139
0
       }
140
1.88M
  }
141
142
1.89M
  int k=-1;
143
5.87M
  for(int i=0; i < (pcount+sizeof(int)-1)/sizeof(int); i++)
144
3.97M
    k ^= kintarr[i];
145
146
  // CmiPrintf("New key %d, %s\n",k,kstring);
147
148
1.89M
  return k;
149
1.88M
}
150
151
int LBCommData::hash(const int i, const int m) const
152
22.7M
{
153
22.7M
  const double a = 0.6803398875;
154
22.7M
  const int k = key();
155
22.7M
  const double ka = k * a;
156
157
22.7M
  int h1 = (int) floor(m*(ka-floor(ka)));
158
22.7M
  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.7M
  return (h1 + i * h2) % m;
163
22.7M
}
164
165
void LBCommTable::GetCommData(LDCommData* data)
166
600
{
167
600
  LDCommData* out=data;
168
600
  LBCommData* curtable=set;
169
600
  TableState* curstate=state;
170
600
  int i;
171
172
265k
  for(i=0; i < cur_sz; i++, curtable++, curstate++) {
173
264k
    if (*curstate == InUse) {
174
11.3k
      out->clearHash();
175
11.3k
      if (curtable->from_proc()) {
176
3.23k
  out->src_proc = curtable->src_proc;
177
8.15k
      } else {
178
8.15k
  out->src_proc = -1;
179
8.15k
        out->sender.omID() = curtable->srcObj.omID();
180
8.15k
        out->sender.objID() = curtable->srcObj.objID();
181
8.15k
      }
182
11.3k
      out->receiver = curtable->destObj;
183
11.3k
      out->messages = curtable->n_messages;
184
11.3k
      out->bytes = curtable->n_bytes;
185
11.3k
      out++;
186
11.3k
    }
187
264k
  }
188
600
}
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
/*@}*/