star-sp

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

commit 52e8cb881ef6d21f4bd974e4fe7d5905db7db1f7
parent 25ac4b841f2607526f2623bc325d0d6b0831067e
Author: Vincent Forest <vincent.forest@meso-star.com>
Date:   Wed, 15 Apr 2015 17:23:23 +0200

Fix issues in the serialization/deserialization of the C++11 RNGs

Diffstat:
Msrc/ssp_rng.c | 7+++++--
Msrc/ssp_rng_proxy.c | 6+++---
2 files changed, 8 insertions(+), 5 deletions(-)

diff --git a/src/ssp_rng.c b/src/ssp_rng.c @@ -33,6 +33,7 @@ #include <rsys/mem_allocator.h> +#include <cstring> #include <random> #include <sstream> @@ -203,7 +204,7 @@ rng_cxx_write(const void* data, FILE* file) std::stringstream stream; RNG* rng = (RNG*)data; ASSERT(rng); - stream << *rng; + stream << *rng << std::endl; i = fwrite(stream.str().c_str(), 1, stream.str().size(), file); return i == stream.str().size() ? RES_OK : RES_IO_ERR; } @@ -217,8 +218,10 @@ rng_cxx_read(void* data, FILE* file) char* s; RNG* rng = (RNG*)data; ASSERT(rng); - while((s = fgets(buf, sizeof(buf), file))) + while((s = fgets(buf, (int)sizeof(buf)-1 /*'\0'*/, file))) { stream << std::string(s); + if(s[strlen(s)-1] == '\n') break; + } stream >> *rng; return stream.fail() ? RES_IO_ERR : RES_OK; } diff --git a/src/ssp_rng_proxy.c b/src/ssp_rng_proxy.c @@ -290,16 +290,16 @@ rng_proxy_next_ran_pool FOR_EACH(ibucket, 0, sa_size(proxy->states)) { size_t irand; res = rng_state_cache_write(proxy->states + ibucket, proxy->rng); - if(res != RES_OK) FATAL("RNG proxy: Unrecoverable IO error\n"); + if(res != RES_OK) FATAL("RNG proxy: cannot write to state cache\n"); FOR_EACH(irand, 0, BUCKET_SIZE) ssp_rng_get(proxy->rng); /* Discard */ } } - mutex_unlock(proxy->mutex); /* Read the RNG pool state of `bucket_name' */ res = rng_state_cache_read (proxy->states + bucket_name, proxy->pools[bucket_name]); - if(res != RES_OK) FATAL("RNG proxy: Unrecoverable IO error\n"); + if(res != RES_OK) FATAL("RNG proxy: cannot read from state cache\n"); + mutex_unlock(proxy->mutex); return proxy->pools[bucket_name]; }