Skip to content
Snippets Groups Projects
Commit 066a0de9 authored by Thomas Jahns's avatar Thomas Jahns :cartwheel:
Browse files

Use reliable PRNG instead of plain random.

* random is subject to arbitrary disturbances because libraries like to
  assume it can be used indiscriminately.
parent 1dd87128
No related branches found
No related tags found
No related merge requests found
......@@ -158,7 +158,7 @@ modelRun(struct model_config setup, MPI_Comm comm)
varDesc = (struct varDesc_t *)Malloc(nVars * sizeof (varDesc[0]));
for (size_t varIdx = 0; varIdx < nVars; varIdx++ )
{
int varLevs = (int)random()%4;
int varLevs = (int)cdi_repeatable_random()%4;
switch (varLevs)
{
case 1:
......@@ -276,7 +276,7 @@ modelRun(struct model_config setup, MPI_Comm comm)
varDesc[varIdx].code = GRIB_USERDEF + (int)varIdx;
vlistDefVarCode(vlistID, varDesc[varIdx].id, varDesc[varIdx].code);
vlistDefVarDatatype(vlistID, varDesc[varIdx].id, setup.datatype);
varDesc[varIdx].useFloat = (bool)(random()&1);
varDesc[varIdx].useFloat = (bool)(cdi_repeatable_random()&1);
}
taxisID = taxisCreate ( setup.taxistype );
......
......@@ -25,6 +25,7 @@
#include "error.h"
#include "dmemory.h"
#include "pio_write.h"
#include "simple_model_helper.h"
#ifdef USE_MPI
#include "cdipio.h"
#include "pio_util.h"
......@@ -495,7 +496,7 @@ int main(int argc, char *argv[])
#ifdef USE_MPI
MPI_Bcast(&randSeed, 1, MPI_UNSIGNED, 0, MPI_COMM_WORLD);
#endif
srandom(randSeed);
cdi_seed_repeatable_random(randSeed);
#ifdef USE_MPI
if (rankGlob == 0)
#endif
......@@ -520,6 +521,7 @@ int main(int argc, char *argv[])
#ifdef USE_MPI
}
cdi_repeatable_finalize();
pioFinalize ();
namespaceSetActive(initNamespace);
cdiPioConfDestroy(pioConfHandle);
......
......@@ -116,7 +116,7 @@ modelRun(struct model_config setup, MPI_Comm comm)
varDesc = (struct varDesc_t *)Malloc(nVars * sizeof (varDesc[0]));
for (size_t varIdx = 0; varIdx < nVars; varIdx++ )
{
int varLevs = (int)random()%4;
int varLevs = (int)cdi_repeatable_random()%4;
switch (varLevs)
{
case 1:
......@@ -186,7 +186,7 @@ modelRun(struct model_config setup, MPI_Comm comm)
varDesc[varIdx].code = GRIB_USERDEF + (int)varIdx;
vlistDefVarCode(vlistID, varDesc[varIdx].id, varDesc[varIdx].code);
vlistDefVarDatatype(vlistID, varDesc[varIdx].id, setup.datatype);
varDesc[varIdx].useFloat = (bool)(random()&1);
varDesc[varIdx].useFloat = (bool)(cdi_repeatable_random()&1);
}
taxisID = taxisCreate ( setup.taxistype );
......
......@@ -296,6 +296,26 @@ PPM_prime_factorization_32(uint32_t n, uint32_t **factors)
*factors = pfactors;
return numFactors;
}
#endif
#ifndef HAVE_PPM_CORE
static char repeatable_rand_state[31 * sizeof (long)];
long cdi_repeatable_random(void)
{
char *caller_rand_state = setstate(repeatable_rand_state);
long retval = random();
setstate(caller_rand_state);
return retval;
}
void cdi_seed_repeatable_random(unsigned seed)
{
char *caller_rand_state = initstate(seed, repeatable_rand_state,
sizeof (repeatable_rand_state));
setstate(caller_rand_state);
}
#endif
/*
......
......@@ -93,4 +93,33 @@ PPM_prime_factorization_32(uint32_t n, uint32_t **factors);
#endif
#ifdef HAVE_PPM_CORE
# include <ppm/ppm.h>
# include <core/ppm_random.h>
# ifdef _OPENMP
# define cdi_omp_pragma(prg) _Pragma(prg)
# else
# define cdi_omp_pragma(prg)
# endif
# define cdi_seed_repeatable_random(seed) do { \
int seed_ = (int)seed; \
MPI_Comm comm_ = MPI_COMM_WORLD; \
cdi_omp_pragma("omp parallel") \
if ((!PPM_initialized() || PPM_finalized())) \
PPM_initialize(&comm_, &seed_, NULL); \
else \
PPM_ya_rand_init(MPI_COMM_WORLD, seed_); \
} while (0)
# define cdi_repeatable_random() ((long)(PPM_irandp()))
# define cdi_repeatable_finalize() PPM_finalize()
#else
/* add random-wrapper to make call results repeatable, if PPM_random
* can't be used */
void cdi_seed_repeatable_random(unsigned seed);
long cdi_repeatable_random(void);
# define cdi_repeatable_finalize() do { \
} while (0)
#endif
#endif
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment