Skip to content
Snippets Groups Projects
Commit 7240cfb8 authored by Thomas Jahns's avatar Thomas Jahns :cartwheel: Committed by Sergey Kosukhin
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 e37e14e9
No related branches found
No related tags found
2 merge requests!34Version 2.2.0,!13Consolidation with CDI-PIO (develop)
......@@ -150,7 +150,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: varLevs = setup.max_nlev / 3; break;
......@@ -240,7 +240,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"
......@@ -422,7 +423,7 @@ 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
......@@ -446,6 +447,7 @@ main(int argc, char *argv[])
#ifdef USE_MPI
}
cdi_repeatable_finalize();
pioFinalize();
namespaceSetActive(initNamespace);
cdiPioConfDestroy(pioConfHandle);
......
......@@ -115,7 +115,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: varLevs = setup.max_nlev / 3; break;
......@@ -171,7 +171,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);
......
......@@ -253,6 +253,27 @@ 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
/*
......
......@@ -77,4 +77,36 @@ int 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