Changeset: 7163d447038e for MonetDB
URL: https://dev.monetdb.org/hg/MonetDB?cmd=changeset;node=7163d447038e
Modified Files:
clients/Tests/MAL-signatures.stable.out
clients/Tests/MAL-signatures.stable.out.int128
clients/Tests/exports.stable.out
monetdb5/modules/kernel/mmath.c
monetdb5/modules/kernel/mmath.h
monetdb5/modules/kernel/mmath.mal
Branch: default
Log Message:
Use xoshiro256starstar.h for generating pseudo random numbers.
We have one global pseudo random number state variable that is
protected by a lock.
diffs (131 lines):
diff --git a/clients/Tests/MAL-signatures.stable.out
b/clients/Tests/MAL-signatures.stable.out
--- a/clients/Tests/MAL-signatures.stable.out
+++ b/clients/Tests/MAL-signatures.stable.out
@@ -10671,6 +10671,7 @@ Ready.
[ "mmath", "pi", "command mmath.pi():dbl ", "MATHpi;", "return
an important mathematical value" ]
[ "mmath", "pow", "command mmath.pow(x:dbl, y:dbl):dbl ",
"MATHbinary_POWdbl;", "The pow(x,y) function returns the value of x raised
to the power of y." ]
[ "mmath", "pow", "command mmath.pow(x:flt, y:flt):flt ",
"MATHbinary_POWflt;", "" ]
+[ "mmath", "prelude", "command mmath.prelude():void ",
"MATHprelude;", "initilize mmath module" ]
[ "mmath", "radians", "command mmath.radians(x:dbl):dbl ",
"MATHunary_RADIANSdbl;", "The radians() function converts degrees into
radians" ]
[ "mmath", "radians", "command mmath.radians(x:flt):flt ",
"MATHunary_RADIANSflt;", "" ]
[ "mmath", "rand", "unsafe command mmath.rand():int ", "MATHrandint;",
"return a random number" ]
diff --git a/clients/Tests/MAL-signatures.stable.out.int128
b/clients/Tests/MAL-signatures.stable.out.int128
--- a/clients/Tests/MAL-signatures.stable.out.int128
+++ b/clients/Tests/MAL-signatures.stable.out.int128
@@ -14901,6 +14901,7 @@ Ready.
[ "mmath", "pi", "command mmath.pi():dbl ", "MATHpi;", "return
an important mathematical value" ]
[ "mmath", "pow", "command mmath.pow(x:dbl, y:dbl):dbl ",
"MATHbinary_POWdbl;", "The pow(x,y) function returns the value of x raised
to the power of y." ]
[ "mmath", "pow", "command mmath.pow(x:flt, y:flt):flt ",
"MATHbinary_POWflt;", "" ]
+[ "mmath", "prelude", "command mmath.prelude():void ",
"MATHprelude;", "initilize mmath module" ]
[ "mmath", "radians", "command mmath.radians(x:dbl):dbl ",
"MATHunary_RADIANSdbl;", "The radians() function converts degrees into
radians" ]
[ "mmath", "radians", "command mmath.radians(x:flt):flt ",
"MATHunary_RADIANSflt;", "" ]
[ "mmath", "rand", "unsafe command mmath.rand():int ", "MATHrandint;",
"return a random number" ]
diff --git a/clients/Tests/exports.stable.out b/clients/Tests/exports.stable.out
--- a/clients/Tests/exports.stable.out
+++ b/clients/Tests/exports.stable.out
@@ -1362,6 +1362,7 @@ str MATHbinary_POWflt(flt *res, const fl
str MATHbinary_ROUNDdbl(dbl *res, const dbl *a, const int *b);
str MATHbinary_ROUNDflt(flt *res, const flt *a, const int *b);
str MATHpi(dbl *pi);
+str MATHprelude(void *ret);
str MATHrandint(int *res);
str MATHrandintarg(int *res, const int *dummy);
str MATHsqlrandint(int *res, const int *seed);
diff --git a/monetdb5/modules/kernel/mmath.c b/monetdb5/modules/kernel/mmath.c
--- a/monetdb5/modules/kernel/mmath.c
+++ b/monetdb5/modules/kernel/mmath.c
@@ -219,6 +219,23 @@ MATHunary_FINITE(bit *res, const dbl *a)
return MAL_SUCCEED;
}
+#include "xoshiro256starstar.h"
+
+/* global pseudo random generator state */
+static random_state_engine mmath_rse;
+/* serialize access to state */
+static MT_Lock mmath_rse_lock MT_LOCK_INITIALIZER("mmath_rse_lock");
+
+str
+MATHprelude(void *ret)
+{
+ (void) ret;
+#ifdef NEED_MT_LOCK_INIT
+ MT_lock_init(&mmath_rse_lock, "mmath_rse_lock");
+#endif
+ init_random_state_engine(mmath_rse, (uint64_t) GDKusec());
+ return MAL_SUCCEED;
+}
str
MATHrandint(int *res)
@@ -226,7 +243,9 @@ MATHrandint(int *res)
#ifdef STATIC_CODE_ANALYSIS
*res = 0;
#else
- *res = rand();
+ MT_lock_set(&mmath_rse_lock);
+ *res = (int) (next(mmath_rse) >> 33);
+ MT_lock_unset(&mmath_rse_lock);
#endif
return MAL_SUCCEED;
}
@@ -238,7 +257,9 @@ MATHrandintarg(int *res, const int *dumm
#ifdef STATIC_CODE_ANALYSIS
*res = 0;
#else
- *res = rand();
+ MT_lock_set(&mmath_rse_lock);
+ *res = (int) (next(mmath_rse) >> 33);
+ MT_lock_unset(&mmath_rse_lock);
#endif
return MAL_SUCCEED;
}
@@ -247,18 +268,22 @@ str
MATHsrandint(void *ret, const int *seed)
{
(void) ret;
- srand(*seed);
+ MT_lock_set(&mmath_rse_lock);
+ init_random_state_engine(mmath_rse, (uint64_t) *seed);
+ MT_lock_unset(&mmath_rse_lock);
return MAL_SUCCEED;
}
str
MATHsqlrandint(int *res, const int *seed)
{
- srand(*seed);
#ifdef STATIC_CODE_ANALYSIS
*res = 0;
#else
- *res = rand();
+ MT_lock_set(&mmath_rse_lock);
+ init_random_state_engine(mmath_rse, (uint64_t) *seed);
+ *res = (int) (next(mmath_rse) >> 33);
+ MT_lock_unset(&mmath_rse_lock);
#endif
return MAL_SUCCEED;
}
diff --git a/monetdb5/modules/kernel/mmath.h b/monetdb5/modules/kernel/mmath.h
--- a/monetdb5/modules/kernel/mmath.h
+++ b/monetdb5/modules/kernel/mmath.h
@@ -62,4 +62,6 @@ mal_export str MATHrandintarg(int *res,
mal_export str MATHsrandint(void *ret, const int *seed);
mal_export str MATHsqlrandint(int *res, const int *seed);
mal_export str MATHpi(dbl *pi);
+mal_export str MATHprelude(void *ret);
+
#endif /* __MMATH_H__ */
diff --git a/monetdb5/modules/kernel/mmath.mal
b/monetdb5/modules/kernel/mmath.mal
--- a/monetdb5/modules/kernel/mmath.mal
+++ b/monetdb5/modules/kernel/mmath.mal
@@ -209,3 +209,9 @@ comment "initialize the rand() function
command pi():dbl
address MATHpi
comment "return an important mathematical value";
+
+command prelude() :void
+address MATHprelude
+comment "initilize mmath module";
+
+mmath.prelude();
_______________________________________________
checkin-list mailing list
[email protected]
https://www.monetdb.org/mailman/listinfo/checkin-list