1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340
|
/*============================================================================
* Unit test for random number generator.
*============================================================================*/
/*
This file is part of Code_Saturne, a general-purpose CFD tool.
Copyright (C) 1998-2019 EDF S.A.
This program is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free Software
Foundation; either version 2 of the License, or (at your option) any later
version.
This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
details.
You should have received a copy of the GNU General Public License along with
this program; if not, write to the Free Software Foundation, Inc., 51 Franklin
Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
/*----------------------------------------------------------------------------*/
#include "cs_defs.h"
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <bft_error.h>
#include <bft_printf.h>
#include "cs_timer.h"
#include "cs_random.h"
/*---------------------------------------------------------------------------*/
#define NPTS 5000
/*---------------------------------------------------------------------------*/
static void
_uniform_test(cs_lnum_t n,
cs_real_t *a)
{
double diff;
double b[607];
double svblk[1634];
double t1, t2, t3;
int i, k, ii;
int ia[20];
/* number of iterations of test: nits */
int nits = 128;
for (k = 0; k < 20; ++k) {
ia[k] = 0;
}
t1 = 100.;
for (k = 0; k < nits; ++k) {
t2 = cs_timer_wtime();
cs_random_uniform(n, a);
t3 = cs_timer_wtime();
t2 = t3 - t2;
t1 = CS_MIN(t2, t1);
for (i = 0; i < n; ++i) {
ii = (int) (a[i] * (double)20.);
++ia[ii];
}
/* last time, save klotz0 for save/resore test */
if (k == nits - 2) {
cs_random_save(svblk);
}
}
/* test save/restore sequence */
cs_random_restore(svblk);
cs_random_uniform(607, b);
diff = 0.;
for (i = 0; i < (CS_MIN(n, 607)); ++i) {
diff += (b[i] - a[i])*(b[i] - a[i]);
}
if (fabs(diff) > 1e-18)
printf("ERROR in start/restart: diff = %e\n", diff);
else
printf(" cs_random save/restore test OK \n");
t1 = t1 / ((float)n);
printf("\n time/uniform = %e \n",t1);
printf("\n Histogram of uniform distribution:\n");
printf(" --------- -- ------- ------------ \n");
for (k = 0; k < 20; ++k) {
if(k<9) printf(" bin[%d] = %d\n",k+1,ia[k]);
else printf(" bin[%d] = %d\n",k+1,ia[k]);
}
}
static void
_normal_test(cs_lnum_t n,
cs_real_t *x)
{
double diff;
int i, k;
double y[128], boxsv[1634];
cs_real_t t1, t2, t3;
double x1, x2, x3, x4, x5, x6;
int kk;
double xx2, xx4;
int bin[21];
/* number of iterations of test */
int nits = 128;
/* initialize moments */
x1 = 0.;
x2 = 0.;
x3 = 0.;
x4 = 0.;
x5 = 0.;
x6 = 0.;
cs_random_normal(n, x);
for (i = 0; i < 21; ++i) {
bin[i] = 0;
}
t1 = 100.;
for (k = 0; k < nits; ++k) {
/* save seeds and pointers for save/restore test */
if (k == nits-1) {
cs_random_save(boxsv);
}
t2 = cs_timer_wtime();
cs_random_normal(n, x);
t3 = cs_timer_wtime();
/* t2 = t3 - t2; */
t1 = CS_MIN(t1,t3-t2);
for (i = 0; i < n; ++i) {
kk = (int) ((x[i] + 5.25) * 2.);
++bin[kk];
}
for (i = 0; i < n; ++i) {
x1 += x[i];
xx2 = x[i] * x[i];
x2 += xx2;
x3 += xx2 * x[i];
xx4 = xx2 * xx2;
x4 += xx4;
x5 += xx4 * x[i];
x6 += xx4 * xx2;
}
/* restore previous seeds and pointers for save/restore test */
if (k == nits-1) {
cs_random_restore(boxsv);
cs_random_normal(128, y);
}
}
/* save/restore check: */
diff = 0.;
for (i = 0; i < (CS_MIN(n,128)); ++i) {
diff += (y[i] - x[i])*(y[i] - x[i]);
}
if (fabs(diff) > 1e-18)
printf("ERROR in normalsv/normalrs: diff = %e\n",diff);
else
printf(" normal law save/restore test OK\n");
x1 /= (double) (n * nits);
x2 /= (double) (n * nits);
x3 /= (double) (n * nits);
x4 /= (double) (n * nits);
x5 /= (double) (n * nits);
x6 /= (double) (n * nits);
t1 = t1 / ((float) n);
printf("\n Time/normal = %e seconds \n",t1);
printf(" Moments: \n");
printf(" Compare to (0.0) (1.0) \n");
printf(" %e %e \n",x1,x2);
printf(" Compare to (0.0) (3.0) \n");
printf(" %e %e \n",x3,x4);
printf(" Compare to (0.0) (15.0) \n");
printf(" %e %e \n",x5,x6);
printf("\n Histogram of gaussian distribution:\n");
printf(" --------- -- --------- ------------ \n");
for (k = 0; k < 21; ++k) {
if (k<9) printf(" bin[%d] = %d \n",k+1,bin[k]);
else printf(" bin[%d] = %d \n",k+1,bin[k]);
}
}
static void
_poisson_test(cs_lnum_t n,
int *p)
{
int nits, i, k;
double p1,p2,p3,p4,x1,x2,x3,x4,fp;
float t1,t2,t3;
int kk;
double mu;
int bin[20];
mu = 2.;
nits = 128;
for (k = 0; k < 20; ++k) {
bin[k] = 0;
}
/* moment comparison values */
p1 = mu;
p2 = mu + mu * mu;
p3 = mu + mu * (double)3. * mu + mu * mu * mu;
p4 = mu + 7.*mu*mu + 6.*mu*mu*mu + mu*mu*mu*mu;
x1 = 0.;
x2 = 0.;
x3 = 0.;
x4 = 0.;
t1 = 10.;
for (k = 0; k < nits; ++k) {
t2 = cs_timer_wtime();
cs_random_poisson(n, mu, p);
t3 = cs_timer_wtime();
t2 = t3 - t2;
t1 = CS_MIN(t1,t2);
for (i = 0; i < n; ++i) {
kk = p[i];
++bin[kk];
}
for (i = 0; i < n; ++i) {
fp = (double) p[i];
x1 += fp;
x2 += fp * fp;
x3 += fp * fp * fp;
x4 += fp * fp * fp * fp;
}
}
x1 /= (double) (n * nits);
x2 /= (double) (n * nits);
x3 /= (double) (n * nits);
x4 /= (double) (n * nits);
t1 = t1 / (float) n;
printf("\n Time/poisson = %e seconds \n",t1);
printf(" Moments: \n");
printf(" Compare: (%e) (%e)\n",p1,p2);
printf(" %e %e \n",x1,x2);
printf(" Compare: (%e) (%e)\n",p3,p4);
printf(" %e %e \n",x3,x4);
printf("\n Histogram of Poisson distribution: mu = %e\n",mu);
printf(" --------- -- ------- ------------ \n");
for (k = 0; k < 20; ++k) {
if(k<9) printf(" bin[%d] = %d \n",k+1,bin[k]);
else printf(" bin[%d] = %d \n",k+1,bin[k]);
}
}
/*---------------------------------------------------------------------------*/
int
main (int argc, char *argv[])
{
CS_UNUSED(argc);
CS_UNUSED(argv);
int p[NPTS];
double a[NPTS];
double wt0, wt1;
/* Initialize the seeds for the uniform random number generator */
int seed = 0;
wt0 = cs_timer_wtime();
cs_random_seed(seed);
wt1 = cs_timer_wtime();
printf("Random number generator initialized in %f seconds\n",
wt1 - wt0);
wt0 = cs_timer_wtime();
_uniform_test(NPTS, a);
wt1 = cs_timer_wtime();
printf("Uniform distribution for %d values in %f seconds\n",
NPTS, wt1 - wt0);
wt0 = cs_timer_wtime();
_normal_test(NPTS, a);
wt1 = cs_timer_wtime();
printf("Normal (Gaussian) distribution for %d values in %f seconds\n",
NPTS, wt1 - wt0);
wt0 = cs_timer_wtime();
_poisson_test(NPTS, p);
wt1 = cs_timer_wtime();
printf("Fischer distribution for %d values in %f seconds\n",
NPTS, wt1 - wt0);
exit(EXIT_SUCCESS);
}
|