Returns vector mapping previously allocated node IDs to a smarter allocation based on the points-to sets in pta accessed through keys. The second part of the keys pairs are the number of (potential) occurrences of that points-to set or a subset, depending on the client's wish. TODO: interfaces are getting unwieldy, an initialised object may be better. TODO: kind of sucks pta can't be const here because getPts isn't.
198{
199 assert(pta != nullptr && "Clusterer::cluster: given null BVDataPTAImpl");
201
203 double fastClusterTime = 0.0;
204 double distanceMatrixTime = 0.0;
205 double dendrogramTraversalTime = 0.0;
206 double regioningTime = 0.0;
207 double evalTime = 0.0;
208
209
211
213
214
216
217
219 for (const std::pair<NodeID, unsigned> &keyOcc : keys)
220 {
221 const PointsTo &pts = pta->getPts(keyOcc.first);
222 const size_t oldSize = pointsToSets.size();
223 pointsToSets[pts] += keyOcc.second;;
224
225
226
227 if (oldSize != pointsToSets.size())
228 {
229 NodeID firstO = !pts.empty() ? *(pts.begin()) : 0;
230 Set<NodeID> &firstOsNeighbours = coPointeeGraph[firstO];
231 for (
const NodeID o : pts)
232 {
233 if (o != firstO)
234 {
235 firstOsNeighbours.insert(o);
236 coPointeeGraph[o].insert(firstO);
237 }
238 }
239 }
240 }
241
244
245 size_t numRegions = 0;
246 std::vector<unsigned> objectsRegion;
248 {
250 }
251 else
252 {
253
254 objectsRegion.insert(objectsRegion.end(),
numObjects, 0);
255 numRegions = 1;
256 }
257
258
259
260
261
262
263 std::vector<OrderedSet<NodeID>> regionsObjects(numRegions);
264 for (
NodeID o = 0; o <
numObjects; ++o) regionsObjects[objectsRegion[o]].insert(o);
265
266
267
268
269
270
271
272 std::vector<std::vector<NodeID>> regionMappings(numRegions);
273
274 std::vector<Map<NodeID, unsigned>> regionReverseMappings(numRegions);
275
276 for (unsigned region = 0; region < numRegions; ++region)
277 {
278 size_t curr = 0;
279
280 for (
NodeID o : regionsObjects[region])
281 {
282
283 regionMappings[region].push_back(o);
284 regionReverseMappings[region][o] = curr++;
285 }
286
287
288 assert(curr != 0);
289
290
291
292
293
294
295 }
296
297
298
299 std::vector<std::vector<std::pair<const PointsTo *, unsigned>>> regionsPointsTos(numRegions);
300 for (const Map<PointsTo, unsigned>::value_type &ptocc : pointsToSets)
301 {
302 const PointsTo &pt = ptocc.first;
303 const unsigned occ = ptocc.second;
304 if (pt.empty()) continue;
305
306
307 unsigned region = objectsRegion[*(pt.begin())];
308
309
310 regionsPointsTos[region].push_back(std::make_pair(&pt, occ));
311 }
312
316 overallStats[
NumRegions] = std::to_string(numRegions);
317
318 std::vector<hclust_fast_methods> methods;
320 {
324 }
325 else
326 {
328 }
329
331 {
332 std::vector<NodeID> nodeMap(
numObjects, UINT_MAX);
333
334 unsigned numGtIntRegions = 0;
335 unsigned largestRegion = 0;
336 unsigned nonTrivialRegionObjects = 0;
337 unsigned allocCounter = 0;
338 for (unsigned region = 0; region < numRegions; ++region)
339 {
340 const size_t regionNumObjects = regionsObjects[region].size();
341
342
344 {
345 allocCounter =
347 }
348
349 if (regionNumObjects > largestRegion) largestRegion = regionNumObjects;
350
351
352
354 {
355 for (
NodeID o : regionsObjects[region]) nodeMap[o] = allocCounter++;
356 continue;
357 }
358
359 ++numGtIntRegions;
360 nonTrivialRegionObjects += regionNumObjects;
361
362 double *distMatrix =
getDistanceMatrix(regionsPointsTos[region], regionNumObjects,
363 regionReverseMappings[region], distanceMatrixTime);
364
366 int *dendrogram = new int[2 * (regionNumObjects - 1)];
367 double *height = new double[regionNumObjects - 1];
368 hclust_fast(regionNumObjects, distMatrix, method, dendrogram, height);
369 delete[] distMatrix;
370 delete[] height;
373
377 visited, regionNumObjects - 1, regionMappings[region]);
378 delete[] dendrogram;
380 dendrogramTraversalTime += (clkEnd - clkStart) /
TIMEINTERVAL;
381 }
382
383 candidates.push_back(std::make_pair(method, nodeMap));
384
385
389 }
390
391
392 std::pair<hclust_fast_methods, std::vector<NodeID>> bestMapping =
394
398 overallStats[
EvalTime] = std::to_string(evalTime);
399 overallStats[
TotalTime] = std::to_string(distanceMatrixTime + dendrogramTraversalTime + fastClusterTime + regioningTime + evalTime);
400
402 if (printStat)
403 {
404 printStats(evalSubtitle +
": overall", overallStats);
405 }
406
407 return bestMapping.second;
408}
#define NATIVE_INT_SIZE
Size of native integer that we'll use for bit vectors, in bits.
static const std::string DistanceMatrixTime
static const std::string LargestRegion
static const std::string NumNonTrivialRegionObjects
static const std::string EvalTime
static std::vector< unsigned > regionObjects(const Map< NodeID, Set< NodeID > > &graph, size_t numObjects, size_t &numLabels)
static const std::string BestCandidate
static std::pair< hclust_fast_methods, std::vector< NodeID > > determineBestMapping(const std::vector< std::pair< hclust_fast_methods, std::vector< NodeID > > > &candidates, Map< PointsTo, unsigned > pointsToSets, const std::string &evalSubtitle, double &evalTime, bool printStat)
static const std::string DendrogramTraversalTime
static double * getDistanceMatrix(const std::vector< std::pair< const PointsTo *, unsigned > > pointsToSets, const size_t numObjects, const Map< NodeID, unsigned > &nodeMap, double &distanceMatrixTime)
static void traverseDendrogram(std::vector< NodeID > &nodeMap, const int *dendrogram, const size_t numObjects, unsigned &allocCounter, Set< int > &visited, const int index, const std::vector< NodeID > ®ionNodeMap)
static void printStats(std::string title, Map< std::string, std::string > &stats)
static const std::string NumRegions
static const std::string RegioningTime
static const std::string NumGtIntRegions
static const std::string FastClusterTime
static const std::string NumObjects
static const std::string TotalTime
static NodeIDAllocator * get(void)
Return (singleton) allocator.
static const OptionMap< SVF::NodeIDAllocator::Strategy > NodeAllocStrat
static const Option< bool > RegionAlign
Align identifiers in each region to a word.
static const Option< bool > RegionedClustering
Cluster partitions separately.
static const OptionMap< u32_t > ClusterMethod
static double getClk(bool mark=false)
int hclust_fast(int n, double *distmat, int method, int *merge, double *height)
std::string hclustMethodToString(hclust_fast_methods method)
Returns a string representation of a hclust method.
std::unordered_set< Key, Hash, KeyEqual, Allocator > Set
std::unordered_map< Key, Value, Hash, KeyEqual, Allocator > Map