Changeset: 1599d7d72d6c for MonetDB
URL: https://dev.monetdb.org/hg/MonetDB?cmd=changeset;node=1599d7d72d6c
Added Files:
        gdk/xoshiro128starstar.h
Modified Files:
        gdk/gdk.h
        gdk/gdk_sample.c
Branch: sample-with-seed
Log Message:

Introducing BATsample_with_seed using xoshiro128starstar as prg.


diffs (114 lines):

diff --git a/gdk/gdk.h b/gdk/gdk.h
--- a/gdk/gdk.h
+++ b/gdk/gdk.h
@@ -2763,6 +2763,7 @@ gdk_export gdk_return BATfirstn(BAT **to
  *
  */
 gdk_export BAT *BATsample(BAT *b, BUN n);
+gdk_export BAT *BATsample_with_seed(BAT *b, BUN n, uint32_t seed);
 
 /*
  *
diff --git a/gdk/gdk_sample.c b/gdk/gdk_sample.c
--- a/gdk/gdk_sample.c
+++ b/gdk/gdk_sample.c
@@ -26,6 +26,7 @@
 #include "monetdb_config.h"
 #include "gdk.h"
 #include "gdk_private.h"
+#include "xoshiro128starstar.h"
 
 #undef BATsample
 
@@ -108,7 +109,7 @@ OIDTreeToBATAntiset(struct oidtreenode *
 
 /* BATsample implements sampling for void headed BATs */
 BAT *
-BATsample(BAT *b, BUN n)
+BATsample_with_seed(BAT *b, BUN n, uint32_t seed)
 {
        BAT *bn;
        BUN cnt, slen;
@@ -128,6 +129,9 @@ BATsample(BAT *b, BUN n)
        } else {
                oid minoid = b->hseqbase;
                oid maxoid = b->hseqbase + cnt;
+               random_state_engine rse;
+               
+               
                /* if someone samples more than half of our tree, we
                 * do the antiset */
                bool antiset = n > cnt / 2;
@@ -144,12 +148,17 @@ BATsample(BAT *b, BUN n)
                        GDKfree(tree);
                        return NULL;
                }
+
+               init_random_state_engine(&rse, seed);
+
                /* while we do not have enough sample OIDs yet */
                for (rescnt = 0; rescnt < n; rescnt++) {
                        oid candoid;
                        do {
+                               double random_double = next_double(rse);
+
                                /* generate a new random OID */
-                               candoid = (oid) (minoid + DRAND * (maxoid - 
minoid));
+                               candoid = (oid) (minoid + random_double * 
(maxoid - minoid));
                                /* if that candidate OID was already
                                 * generated, try again */
                        } while (!OIDTreeMaybeInsert(tree, candoid, rescnt));
@@ -172,3 +181,11 @@ BATsample(BAT *b, BUN n)
                          ALGOBATPAR(b), n, ALGOOPTBATPAR(bn));
        return bn;
 }
+
+BAT *
+BATsample(BAT *b, BUN n)
+{
+       uint32_t some_random_seed = (uint32_t) rand();
+
+       return BATsample_with_seed(b, n,some_random_seed);
+}
diff --git a/gdk/xoshiro128starstar.h b/gdk/xoshiro128starstar.h
new file mode 100644
--- /dev/null
+++ b/gdk/xoshiro128starstar.h
@@ -0,0 +1,37 @@
+#include <stdint.h>
+#include <string.h>
+
+static inline uint32_t rotl(const uint32_t x, int k) {
+       return (x << k) | (x >> (32 - k));
+}
+
+typedef uint32_t random_state_engine[4];
+
+void init_random_state_engine(random_state_engine* engine, uint32_t seed);
+void init_random_state_engine(random_state_engine* engine, uint32_t seed) {
+
+    random_state_engine s = { seed };
+
+    memcpy(engine, &s, sizeof(random_state_engine));
+}
+
+static inline uint32_t next_uint32(random_state_engine rse) {
+       uint32_t output = rotl(rse[0] * 5, 7) * 9;
+
+       const uint32_t t = rse[1] << 9;
+
+       rse[2] ^= rse[0];
+       rse[3] ^= rse[1];
+       rse[1] ^= rse[2];
+       rse[0] ^= rse[3];
+
+       rse[2] ^= t;
+
+       rse[3] = rotl(rse[3], 11);
+
+       return output;
+}
+
+static inline double next_double(random_state_engine rse) {
+       return (double) next_uint32(rse) / UINT32_MAX;
+}
_______________________________________________
checkin-list mailing list
[email protected]
https://www.monetdb.org/mailman/listinfo/checkin-list

Reply via email to