Changeset: 698e647d24cb for MonetDB
URL: https://dev.monetdb.org/hg/MonetDB/rev/698e647d24cb
Added Files:
        gdk/gdk_sketch.c
Modified Files:
        gdk/CMakeLists.txt
        gdk/gdk.h
        gdk/gdk_bat.c
        gdk/gdk_batop.c
        gdk/gdk_join.c
        monetdb5/modules/kernel/aggr.c
Branch: cnting_sketches
Log Message:

Small cleanup of sketch code


diffs (truncated from 579 to 300 lines):

diff --git a/gdk/CMakeLists.txt b/gdk/CMakeLists.txt
--- a/gdk/CMakeLists.txt
+++ b/gdk/CMakeLists.txt
@@ -93,6 +93,7 @@ target_sources(bat
   gdk_strimps.c
   murmurhash3.h
   xxhash.h
+  gdk_sketch.c
   PUBLIC
   ${gdk_public_headers})
 
diff --git a/gdk/gdk.h b/gdk/gdk.h
--- a/gdk/gdk.h
+++ b/gdk/gdk.h
@@ -447,8 +447,6 @@ typedef struct BAT {
        PROPrec *tprops;        /* list of dynamic properties stored in the bat 
descriptor */
 
        uint8_t cnting_sketch[BUCKETS][CLZ_BUCKETS];
-       double estimate;
-       MT_Lock sketch_lock;
 
        MT_Lock theaplock;      /* lock protecting heap reference changes */
        MT_RWLock thashlock;    /* lock specifically for hash management */
@@ -1423,6 +1421,11 @@ gdk_export void STRMPdestroy(BAT *b);
 gdk_export bool BAThasstrimps(BAT *b);
 gdk_export gdk_return BATsetstrimps(BAT *b);
 
+gdk_export void sketch_populate(BAT* n, BATiter *ni, struct canditer *nci);
+gdk_export void sketch_merge(BAT* b, BAT* n);
+gdk_export double sketch_estimator(uint8_t cnt_sketch[BUCKETS][CLZ_BUCKETS]);
+gdk_export double BATsketchestimator(BAT *b, BATiter *bi, struct canditer 
*bci);
+
 /* Rtree structure functions */
 #ifdef HAVE_RTREE
 gdk_export bool RTREEexists(BAT *b);
diff --git a/gdk/gdk_bat.c b/gdk/gdk_bat.c
--- a/gdk/gdk_bat.c
+++ b/gdk/gdk_bat.c
@@ -199,8 +199,6 @@ BATcreatedesc(oid hseq, int tt, bool hea
        MT_lock_init(&bn->batIdxLock, name);
        snprintf(name, sizeof(name), "hashlock%d", bn->batCacheid); /* fits */
        MT_rwlock_init(&bn->thashlock, name);
-       snprintf(name, sizeof(name), "sklock%d", bn->batCacheid); /* fits */
-       MT_lock_init(&bn->sketch_lock, name);
        return bn;
 }
 
@@ -616,7 +614,6 @@ BATdestroy(BAT *b)
        MT_lock_destroy(&b->theaplock);
        MT_lock_destroy(&b->batIdxLock);
        MT_rwlock_destroy(&b->thashlock);
-       MT_lock_destroy(&b->sketch_lock);
        if (b->theap) {
                GDKfree(b->theap);
        }
diff --git a/gdk/gdk_batop.c b/gdk/gdk_batop.c
--- a/gdk/gdk_batop.c
+++ b/gdk/gdk_batop.c
@@ -744,149 +744,6 @@ append_msk_bat(BAT *b, BATiter *ni, stru
        return GDK_SUCCEED;
 }
 
-#include <sys/random.h>
-/// #include "murmurhash3.h"
-#define XXH_STATIC_LINKING_ONLY
-#define XXH_IMPLEMENTATION
-#include "xxhash.h"
-
-/// Helper function sigma as defined in
-/// "New cardinality estimation algorithms for HyperLogLog sketches"
-/// Otmar Ertl, arXiv:1702.01284
-static inline double
-sigma(double x)
-{
-       if (x == 1.) return INFINITY;
-       double y = 1;
-       double z = x;
-       double z_prime;
-       do {
-               x *= x;
-               z_prime = z;
-               z += x * y;
-               y += y;
-       } while(z_prime != z);
-       return z;
-}
-
-/// Helper function tau as defined in
-/// "New cardinality estimation algorithms for HyperLogLog sketches"
-/// Otmar Ertl, arXiv:1702.01284
-static inline double
-tau(double x)
-{
-       if (x == 0. || x == 1.) return 0.;
-       double y = 1.0;
-       double z = 1 - x;
-       double z_prime;
-       do {
-               x = sqrt(x);
-               z_prime = z;
-               y *= 0.5;
-               z -= pow(1 - x, 2) * y;
-       } while(z_prime != z);
-       return z / 3;
-}
-
-/// Estimator as defined in
-/// "New cardinality estimation algorithms for HyperLogLog sketches"
-/// Otmar Ertl, arXiv:1702.01284.
-/// Only difference is how the multiplicity array is computed
-static inline uint64_t
-estimator(uint8_t cnt_sketch[BUCKETS][CLZ_BUCKETS])
-{
-       int8_t C[CLZ_BUCKETS + 1] = {0};
-       for (size_t bucket = 0; bucket < BUCKETS; bucket++)
-       {
-               int K = -1;
-               for (size_t clz = 0; clz < CLZ_BUCKETS; clz++)
-                       if (cnt_sketch[bucket][clz] > 0)
-                               K = clz;
-               K == -1 ? C[0]++ : C[K + 1]++;
-       }
-       double t = tau(1.0 - ((double)C[CLZ_BUCKETS] / BUCKETS));
-       double z = (double)BUCKETS * t;
-       for (int k = CLZ_BUCKETS; k >= 1; k--)
-       {
-               z = 0.5 * (z + (double)C[k]);
-       }
-       double s = sigma((double)C[0] / BUCKETS);
-       z += (double)BUCKETS * s;
-       const double alpha = 0.7213475204444817;
-       return alpha * (double)BUCKETS * BUCKETS / z;
-}
-
-static inline void
-create_bat_sketch(BAT* n, BATiter *ni,
-                 struct canditer *nci, QryCtx *qry_ctx)
-{
-       oid hseq = n->hseqbase;
-       /* uint64_t murmur3_out[2]; */
-       uint64_t hash;
-       uint8_t bucket;
-       /* uint8_t cnting_sketch[BUCKETS][CLZ_BUCKETS] = {0}; */
-       uint8_t clz;
-
-       canditer_reset(nci);
-       TIMEOUT_LOOP(nci->ncand, qry_ctx) {
-               BUN p = canditer_next(nci) - hseq;
-               const void *ptr = BUNtail(ni, p);
-               switch (ni->type) {
-               case TYPE_int:
-                       if (is_int_nil(*(int *)ptr))
-                               continue;
-                       else
-                               /* MurmurHash3_x64_128(ptr, sizeof(int), 
HLLSEED, murmur3_out); */
-                               /* hash = murmur3_out[1]; */
-                               hash = XXH64(ptr, sizeof(int), HLLSEED);
-                       break;
-               case TYPE_lng:
-                       if (is_lng_nil(*(lng *)ptr))
-                               continue;
-                       else
-                               /* MurmurHash3_x64_128(ptr, sizeof(lng), 
HLLSEED, murmur3_out); */
-                               /* hash = murmur3_out[1]; */
-                               hash = XXH64(ptr, sizeof(lng), HLLSEED);
-                       break;
-               case TYPE_str:
-                       if (strNil(ptr))
-                               continue;
-                       else
-                               /* MurmurHash3_x64_128(ptr, strlen(ptr), 
HLLSEED, murmur3_out); */
-                               /* hash = murmur3_out[1]; */
-                               hash = XXH64(ptr, strlen(ptr), HLLSEED);
-                       break;
-               default:
-                       return;
-               }
-               bucket = hash & BITS_MASK;
-               hash |= BITS_MASK;
-               clz = __builtin_clzll(hash);
-               assert(clz <= 58);
-               if (n->cnting_sketch[bucket][clz] <= 128) {
-                       n->cnting_sketch[bucket][clz]++;
-               } else {
-                       uint8_t k = n->cnting_sketch[bucket][clz] - 128;
-                       uint64_t rng;
-                       getrandom(&rng, sizeof(rng), 0);
-                       if ((rng & ((1ULL << k) - 1)) == 0)
-                               n->cnting_sketch[bucket][clz]++;
-               }
-       }
-}
-
-static inline void
-merge_sketches(BAT* b, BAT* n)
-{
-       MT_lock_set(&b->sketch_lock);
-       for (size_t i = 0; i < BUCKETS; i++)
-               for (size_t j = 0; j < CLZ_BUCKETS; j++)
-                       if (n->cnting_sketch[i][j] > b->cnting_sketch[i][j])
-                               b->cnting_sketch[i][j] = n->cnting_sketch[i][j];
-       b->estimate = estimator(b->cnting_sketch);
-       MT_lock_unset(&b->sketch_lock);
-}
-
 /* Append the contents of BAT n (subject to the optional candidate
  * list s) to BAT b.  If b is empty, b will get the seqbase of s if it
  * was passed in, and else the seqbase of n. */
@@ -1228,8 +1085,8 @@ BATappend2(BAT *b, BAT *n, BAT *s, bool 
                hlocked = false;
        }
 
-       create_bat_sketch(n, &ni, &ci, qry_ctx);
-       merge_sketches(b, n);
+       sketch_populate(n, &ni, &ci);
+       sketch_merge(b, n);
 
   doreturn:
        bat_iterator_end(&ni);
diff --git a/gdk/gdk_join.c b/gdk/gdk_join.c
--- a/gdk/gdk_join.c
+++ b/gdk/gdk_join.c
@@ -3542,8 +3542,9 @@ count_unique(BAT *b, BAT *s, BUN *cnt1, 
 static double
 guess_uniques(BAT *b, struct canditer *ci)
 {
-       if (b->estimate)
-               return b->estimate;
+       if (b->tunique_est)
+               return b->tunique_est;
+
        BUN cnt1 = 0, cnt2;
        BUN max = b->twidth < SIZEOF_BUN ? (BUN) 1 << (8*b->twidth) : BUN_MAX;
        BAT *s1;
@@ -3627,8 +3628,8 @@ BATguess_uniques(BAT *b, struct canditer
                return 0;
        if (b->batCount == 1 || (ci && ci->ncand == 1))
                return 1;
-       if (b->estimate)
-               return b->estimate;
+       if (b->tunique_est)
+               return b->tunique_est;
        struct canditer lci;
        if (ci == NULL) {
                canditer_init(&lci, b, NULL);
diff --git a/gdk/gdk_sketch.c b/gdk/gdk_sketch.c
new file mode 100644
--- /dev/null
+++ b/gdk/gdk_sketch.c
@@ -0,0 +1,162 @@
+/*
+ * SPDX-License-Identifier: MPL-2.0
+ *
+ * This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0.  If a copy of the MPL was not distributed with this
+ * file, You can obtain one at https://mozilla.org/MPL/2.0/.
+ *
+ * For copyright information, see the file debian/copyright.
+ */
+
+#include "gdk.h"
+
+#include <sys/random.h>
+/// #include "murmurhash3.h"
+#define XXH_STATIC_LINKING_ONLY
+#define XXH_IMPLEMENTATION
+#include "xxhash.h"
+
+/// Helper function sigma as defined in
+/// "New cardinality estimation algorithms for HyperLogLog sketches"
+/// Otmar Ertl, arXiv:1702.01284
+static inline double
+sigma(double x)
+{
+       if (x == 1.) return INFINITY;
+       double y = 1;
+       double z = x;
+       double z_prime;
+       do {
+               x *= x;
+               z_prime = z;
+               z += x * y;
+               y += y;
+       } while(z_prime != z);
+       return z;
+}
+
+/// Helper function tau as defined in
+/// "New cardinality estimation algorithms for HyperLogLog sketches"
+/// Otmar Ertl, arXiv:1702.01284
+static inline double
+tau(double x)
+{
+       if (x == 0. || x == 1.) return 0.;
+       double y = 1.0;
+       double z = 1 - x;
+       double z_prime;
+       do {
+               x = sqrt(x);
+               z_prime = z;
_______________________________________________
checkin-list mailing list -- [email protected]
To unsubscribe send an email to [email protected]

Reply via email to