star-sp

Random number generators and distributions
git clone git://git.meso-star.fr/star-sp.git
Log | Files | Refs | README | LICENSE

commit d9296c00b885fe90a96d147b0220fa54e8c369b5
parent b2f49264b18bc3b45f1522bad211ad3b1148e509
Author: Vincent Forest <vincent.forest@meso-star.com>
Date:   Wed,  8 Dec 2021 12:24:33 +0100

Upd the initial sequence_id of the proxy

We sequence id is now always the index of the last active sequence. On
proxy creation, no sequence is active : the index is thus no more 0 but
SSP_SEQUENCE_ID_NONE, i.e. SIZE_MAX.

Diffstat:
Msrc/ssp.h | 4++++
Msrc/ssp_rng_proxy.c | 17+++++++++--------
Msrc/test_ssp_rng_proxy.c | 23+++++++++++++----------
3 files changed, 26 insertions(+), 18 deletions(-)

diff --git a/src/ssp.h b/src/ssp.h @@ -37,6 +37,8 @@ #include <rsys/math.h> #include <rsys/rsys.h> +#include <stdint.h> /* SIZE_MAX */ + /* Library symbol management */ #if defined(SSP_SHARED_BUILD) /* Build shared library */ #define SSP_API extern EXPORT_SYM @@ -55,6 +57,8 @@ #define SSP(Func) ssp_ ## Func #endif +#define SSP_SEQUENCE_ID_NONE SIZE_MAX + /* Forward declaration of opaque types */ struct ssp_rng; struct ssp_rng_proxy; diff --git a/src/ssp_rng_proxy.c b/src/ssp_rng_proxy.c @@ -89,11 +89,13 @@ struct ssp_rng_proxy { struct ssp_rng** pools; /* `type' RNGs wrapped by bucket RNGs */ struct rng_state_cache* states; /* Cache of `type' RNG states */ - /* Current index of the sequence. This index is independent of the original - * seed used by the proxy. When creating the proxy, the sequence index is 0. - * It is then incremented by one each time a new sequence is required. This - * index is used to identify the state of the proxy relative to the original - * seed. */ + /* Index of the last sequence queried by proxy-managed RNGs. This index is + * independent of the original seed used by the proxy and is designed to to + * identify the status of the proxy relative to its original seed. When the + * proxy is created, the sequence index is SSP_SEQUENCE_ID_NONE, that is, + * no sequence has been queried. At the first query of a random number, the + * first sequence is consumed and this sequence index is then 0. It is then + * incremented by one each time a new sequence is required. */ size_t sequence_id; signal_T signals[RNG_PROXY_SIGS_COUNT__]; @@ -648,7 +650,7 @@ ssp_rng_proxy_create2 /* Set the sequence index to SIZE_MAX because no sequence is active until a * random number query is made. On the first query, the index will be * incremented to 0 */ - proxy->sequence_id = SIZE_MAX; + proxy->sequence_id = SSP_SEQUENCE_ID_NONE/* <=> SIZE_MAX*/; /* Create the proxy RNG in its default state */ if(!args->rng) { @@ -829,7 +831,6 @@ res_T ssp_rng_proxy_get_sequence_id(const struct ssp_rng_proxy* proxy, size_t* id) { if(!proxy || !id) return RES_BAD_ARG; - *id = proxy->sequence_id == SIZE_MAX /* No active sequence <=> id 0 */ - ? 0 : proxy->sequence_id; + *id = proxy->sequence_id; return RES_OK; } diff --git a/src/test_ssp_rng_proxy.c b/src/test_ssp_rng_proxy.c @@ -451,21 +451,24 @@ test_read_with_cached_states(void) * the proposed builtin type and is thus the one that will fill quickly the * cache stream. */ args.type = SSP_RNG_MT19937_64; - args.sequence_size = 4; - args.sequence_pitch = 4; + args.sequence_size = 10; + args.sequence_pitch = 10; args.nbuckets = 2; CHK(ssp_rng_proxy_create2(NULL, &args, &proxy) == RES_OK); CHK(ssp_rng_proxy_create_rng(proxy, 0, &rng0) == RES_OK); CHK(ssp_rng_proxy_create_rng(proxy, 1, &rng1) == RES_OK); /* Check RNG states */ - CHK(ssp_rng_get(rng) == ssp_rng_get(rng0)); - CHK(ssp_rng_get(rng) == ssp_rng_get(rng0)); - CHK(ssp_rng_get(rng) == ssp_rng_get(rng1)); - CHK(ssp_rng_get(rng) == ssp_rng_get(rng1)); + FOR_EACH(i, 0, args.sequence_size) { + if(i < args.sequence_size/2) { + CHK(ssp_rng_get(rng) == ssp_rng_get(rng0)); + } else { + CHK(ssp_rng_get(rng) == ssp_rng_get(rng1)); + } + } /* Discard several RNs for the 1st RNG to cache several states for the rng1 */ - CHK(ssp_rng_discard(rng0, 100) == RES_OK); + CHK(ssp_rng_discard(rng0, 103) == RES_OK); /* Generate a RNG state */ stream = tmpfile(); @@ -675,9 +678,9 @@ test_sequence(void) CHK(ssp_rng_proxy_get_sequence_id(proxy, NULL) == RES_BAD_ARG); /* Check that directly after its creation, the proxy has a the sequence index - * of zero */ + * of SSP_SEQUENCE_ID_NONE */ CHK(ssp_rng_proxy_get_sequence_id(proxy, &id) == RES_OK); - CHK(id == 0); + CHK(id == SSP_SEQUENCE_ID_NONE); /* Generate 4 sequences of random numbers and check the progression of the * sequence index */ @@ -767,7 +770,7 @@ main(int argc, char** argv) test_read_with_cached_states(); test_write(); test_cache(); - /*test_sequence();*/ + test_sequence(); CHK(mem_allocated_size() == 0); return 0; }