star-sp

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

commit 3cb7c4260413a32c9337212e1268e34461e684ca
parent 1851af0ae984e2748191538ba4ea227275f848dc
Author: Vincent Forest <vincent.forest@meso-star.com>
Date:   Sat, 22 Mar 2025 16:23:43 +0100

Correct the way read status cache is disabled

Do not free its memory space or close its stream, as it could be
reactivated when a proxy sequence is flushed.

Diffstat:
Msrc/ssp_rng_proxy.c | 8++------
Msrc/test_ssp_rng_proxy.c | 21++++++++++++++++++++-
2 files changed, 22 insertions(+), 7 deletions(-)

diff --git a/src/ssp_rng_proxy.c b/src/ssp_rng_proxy.c @@ -325,12 +325,8 @@ rng_state_cache_read && cache->no_wstream && cache->read == cache->write && cache->nstates == 1/* A state is saved in 'cache->state' */) { - /* There is no more data cached into the stream. Close the stream and do - * not rely anymore on the proxy RNG to generate the RNG states */ - fclose(cache->stream); - MEM_RM(cache->allocator, cache->buffer); - cache->stream = NULL; - cache->buffer = NULL; + /* There is no more data cached into the stream. Do not rely anymore on the + * proxy RNG to generate the RNG states */ cache->no_rstream = 1; } diff --git a/src/test_ssp_rng_proxy.c b/src/test_ssp_rng_proxy.c @@ -628,6 +628,7 @@ test_cache(void) CHK(ssp_rng_get(rng1) == ssp_rng_get(rng)); if(i % 2) CHK(ssp_rng_discard(rng, 2) == RES_OK); } + CHK(ssp_rng_ref_put(rng) == RES_OK); CHK(ssp_rng_proxy_ref_put(proxy) == RES_OK); @@ -806,7 +807,7 @@ test_flush_sequence() /* Fill the cache of the rng1 */ CHK(ssp_rng_discard(rng0, 1013) == RES_OK); CHK(ssp_rng_proxy_get_sequence_id(proxy, &id) == RES_OK); - CHK(id == 63 + 203/*ceil(1023/(args.sequence_size*0.5))*/); + CHK(id == 63 + 203/*ceil(1013/(args.sequence_size*0.5))*/); /* Discard several sequences */ CHK(ssp_rng_proxy_flush_sequences(proxy, 314) == RES_OK); @@ -825,6 +826,24 @@ test_flush_sequence() CHK(id == 63 + 203 + 314); } + /* Generate several RNs for the first RNG only to make under pressure the + * cache stream of 'rng1'. The cache stream limit is set to 32 MB and the + * size of a Mersenne Twister RNG state is greater than 6 KB. Consquently, + * ~5500 RNG states will exceed the cache stream, i.e. 5500*5 = 27500 + * random generations (since there is 5 RNs per bucket). Above this limit, + * 'rng1' will not rely anymore on the proxy RNG to manage its state. */ + CHK(ssp_rng_discard(rng0, 30000) == RES_OK); + + /* Discard enough random numbers from rng1 to consume all cached RNG states, + * and thus disable read cache usage */ + CHK(ssp_rng_discard(rng1, 30000) == RES_OK); + + CHK(ssp_rng_proxy_get_sequence_id(proxy, &id) == RES_OK); + CHK(id == 63+203+314+(30000*2)/args.sequence_size); + + /* Try to flush a sequence on a proxy whose a generator don't use cache */ + CHK(ssp_rng_proxy_flush_sequences(proxy, 1) == RES_OK); + CHK(ssp_rng_proxy_ref_put(proxy) == RES_OK); CHK(ssp_rng_ref_put(rng1) == RES_OK); CHK(ssp_rng_ref_put(rng0) == RES_OK);