Changeset: 9f73de104bd8 for MonetDB
URL: https://dev.monetdb.org/hg/MonetDB/rev/9f73de104bd8
Added Files:
gdk/murmurhash3.h
gdk/xxhash.h
monetdb5/modules/kernel/murmurhash3.h
monetdb5/modules/kernel/xxhash.h
Modified Files:
gdk/CMakeLists.txt
gdk/gdk.h
gdk/gdk_bat.c
gdk/gdk_batop.c
monetdb5/modules/kernel/aggr.c
sql/scripts/80_statistics.sql
Branch: cnting_sketches
Log Message:
Implementation of counting sketches for uniques estimation.
diffs (truncated from 16174 to 300 lines):
diff --git a/gdk/CMakeLists.txt b/gdk/CMakeLists.txt
--- a/gdk/CMakeLists.txt
+++ b/gdk/CMakeLists.txt
@@ -91,6 +91,8 @@ target_sources(bat
gdk_tracer.c gdk_tracer.h
gdk_rtree.c
gdk_strimps.c
+ murmurhash3.h
+ xxhash.h
PUBLIC
${gdk_public_headers})
diff --git a/gdk/gdk.h b/gdk/gdk.h
--- a/gdk/gdk.h
+++ b/gdk/gdk.h
@@ -353,6 +353,13 @@ typedef struct Sink {
#define ORDERIDXOFF 3
+#define HLL_BITS 5
+#define BITS_MASK ((1ULL << HLL_BITS) - 1)
+#define MAX_CLZ (64 - HLL_BITS)
+#define BUCKETS (1U << HLL_BITS)
+#define CLZ_BUCKETS (MAX_CLZ + 1)
+#define HLLSEED 0xadc83b19ULL
+
/* assert that atom width is power of 2, i.e., width == 1<<shift */
#define assert_shift_width(shift,width) assert(((shift) == 0 && (width) == 0)
|| ((unsigned)1<<(shift)) == (unsigned)(width))
@@ -439,6 +446,10 @@ typedef struct BAT {
Sink *tsink;
PROPrec *tprops; /* list of dynamic properties stored in the bat
descriptor */
+ uint8_t cnting_sketch[BUCKETS][CLZ_BUCKETS];
+ uint64_t estimate;
+ MT_Lock sketch_lock;
+
MT_Lock theaplock; /* lock protecting heap reference changes */
MT_RWLock thashlock; /* lock specifically for hash management */
MT_Lock batIdxLock; /* lock to manipulate other indexes/properties
*/
diff --git a/gdk/gdk_bat.c b/gdk/gdk_bat.c
--- a/gdk/gdk_bat.c
+++ b/gdk/gdk_bat.c
@@ -177,6 +177,7 @@ BATcreatedesc(oid hseq, int tt, bool hea
.tvheap = vh,
.creator_tid = MT_getpid(),
.qc = role == TRANSIENT ? MT_thread_get_qry_ctx() : NULL,
+ .cnting_sketch = {0},
};
if (bn->theap) {
@@ -198,6 +199,8 @@ 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;
}
@@ -613,6 +616,7 @@ 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,6 +744,149 @@ 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 llroundl(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. */
@@ -1085,6 +1228,9 @@ BATappend2(BAT *b, BAT *n, BAT *s, bool
hlocked = false;
}
+ create_bat_sketch(n, &ni, &ci, qry_ctx);
+ merge_sketches(b, n);
+
doreturn:
bat_iterator_end(&ni);
if (minbound)
diff --git a/gdk/murmurhash3.h b/gdk/murmurhash3.h
new file mode 100644
--- /dev/null
+++ b/gdk/murmurhash3.h
@@ -0,0 +1,374 @@
+//-----------------------------------------------------------------------------
+// MurmurHash3 was written by Austin Appleby, and is placed in the public
+// domain. The author hereby disclaims copyright to this source code.
+
+// Note - The x86 and x64 versions do _not_ produce the same results, as the
+// algorithms are optimized for their respective platforms. You can still
+// compile and run any of them on any platform, but your performance with the
+// non-native version will be less than optimal.
+
+#ifndef _MURMURHASH3_H_
+#define _MURMURHASH3_H_
+
+//-----------------------------------------------------------------------------
+// Platform-specific functions and macros
+
+// Microsoft Visual Studio
+
+#if defined(_MSC_VER) && (_MSC_VER < 1600)
+
+typedef unsigned char uint8_t;
+typedef unsigned int uint32_t;
+typedef unsigned __int64 uint64_t;
+
+// Other compilers
+
+#else // defined(_MSC_VER)
+
+#include <stdint.h>
+
+#endif // !defined(_MSC_VER)
+
+//-----------------------------------------------------------------------------
+
+void MurmurHash3_x86_32 ( const void * key, int len, uint32_t seed, void *
out );
+
+void MurmurHash3_x86_128 ( const void * key, int len, uint32_t seed, void *
out );
+
+void MurmurHash3_x64_128 ( const void * key, int len, uint32_t seed, void *
out );
+
+//-----------------------------------------------------------------------------
+// Platform-specific functions and macros
+
+// Microsoft Visual Studio
+
+#if defined(_MSC_VER)
+
+#define FORCE_INLINE __forceinline
+
+#include <stdlib.h>
+
+#define ROTL32(x,y) _rotl(x,y)
+#define ROTL64(x,y) _rotl64(x,y)
+
+#define BIG_CONSTANT(x) (x)
+
+// Other compilers
+
+#else // defined(_MSC_VER)
+
+#define FORCE_INLINE inline __attribute__((always_inline))
+
+static inline uint32_t
+rotl32 ( uint32_t x, int8_t r )
+{
_______________________________________________
checkin-list mailing list -- [email protected]
To unsubscribe send an email to [email protected]