File: check_gen_simple.c

package info (click to toggle)
sprng 2.0a-19
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,308 kB
  • sloc: ansic: 30,353; fortran: 1,618; makefile: 576; cpp: 58; sh: 5
file content (293 lines) | stat: -rw-r--r-- 6,598 bytes parent folder | download | duplicates (11)
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
/*--- Chris S.: June 1999  */
/*--- reads in, first an integer as a generator type and next elements from */
/*--- a data file */
/*--- checking Default C interface                                        ---*/
/*--- added 'int gtype' 'scanf("%d\n",  @gtype)' and gtype to all init_sprng */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

/*#define USE_MPI			 Uncomment to test with MPI.  */

#ifdef USE_MPI
#include <mpi.h>
#endif
#define SIMPLE_SPRNG
#include "sprng.h"

#ifdef VERBOSE
#define report printf
#else
#define report ignore
#endif

#define PARAM SPRNG_DEFAULT

#define YES 1
#define NO 0

int gtype; /*--- adding generator type ---*/ 

#ifdef __STDC__
void ignore(char *s, ...)
#else
void ignore(s)
char *s;
#endif
{
}


int check_gen()			/* Check generator with correct parameters   */
{
  int *gen, i, size, seed;
  char *s;
  int tempi, correct, result = YES;
  const unsigned int fltmult = 1<<20, dblmult = 1<<30;
  double tempd;
  
  seed = 985456376;
  gen = init_sprng(gtype,seed,PARAM); /* initialize generator                */
  if(gen == NULL)
  {
    result = NO;
    printf("FAILED: SPRNG was unable to initialize the generator\n");
  }
  
  
  /* ____________________ Check default generator ________________________   */

  correct = YES;
  for(i=0; i<200; i++)		/* check integer generator                   */
  {
    scanf("%d\n", &tempi);
    if(tempi != isprng())
      result = correct = NO;
  }
  
  if(correct == NO)
    printf("FAILED: Integer generator does not reproduce correct stream.\n\tArithmetic on this machine may not be compatible with this generator.\n");
  else
    report("PASSED: Integer generator passed the reproducibility test\n");

  correct = YES;
  for(i=0; i<50; i++)		/* check float generator                   */
  {
    scanf("%d\n", &tempi);
#ifdef USE_MPI
    tempd = (double) get_rn_flt_simple_mpi();
#else
    tempd = (double) get_rn_flt_simple();
#endif
    /* printf("%d. %d %f\n", i, tempi, tempd);*/
   
    if(abs((tempi>>11) - (int) (tempd*fltmult)) > 1 )
    {
      printf("%d. %d %d\n", i, tempi>>11, (int)(tempd*fltmult));
      result = correct = NO;
    }
    
  }
  if(correct == NO)
    printf("FAILED: Float generator does not reproduce correct stream.\n\tArithmetic on this machine may not be compatible with this generator.\n");
  else
    report("PASSED: Float generator passed the reproducibility test\n");


  correct = YES;
  for(i=0; i<50; i++)		/* check double precision generator          */
  {
    scanf("%d", &tempi);
    tempd = sprng();
    if(abs((tempi>>1) - (int) (tempd*dblmult)) > 1 )
    {
      result = correct = NO;
    }
    
  }
  if(correct == NO)
    printf("FAILED: Double generator does not reproduce correct stream.\n\tArithmetic on this machine may not be compatible with this generator.\n");
  else
    report("PASSED: Double generator passed the reproducibility test\n");


  size = pack_sprng(&s);	/* check packing                             */
  if(size == 0)
  {
    result = NO;
    printf("FAILED: SPRNG was unable to pack the generator\n");
  }
  
  for(i=0; i<100; i++)	        /* default and packed generators now differ  */
    isprng();
  
  gen = unpack_sprng(s);	/* check unpacking                           */
  if(gen == NULL)
  {
    result = NO;
    printf("FAILED: SPRNG was unable to unpack the generator\n");
  }

  correct = YES;
  for(i=0; i<100; i++)		/* check unpacked generator                  */
  {
    scanf("%d\n", &tempi);
    if(tempi != isprng())
      result = correct = NO;
  }
  if(correct == NO)
    printf("FAILED: Generator does not reproduce correct stream after packing.\n\tProbably error in packing/unpacking\n");
  else
    report("PASSED: Generator packs and unpacks correctly.\n");

  return result;
}













     /* Check if generator meets specifications in handling errors           */
int check_errors()
{
  int *gen1, i, size;
  int tempi, correct, result = YES;
  char s[MAX_PACKED_LENGTH];
  
  /* ____________________ Unpack invalid string _____________________        */

#ifndef CREATE_DATA
  memset(s,0,MAX_PACKED_LENGTH); /* set string to 0's                        */

  fprintf(stderr,"Expect SPRNG ERROR: packed string invalid\n");
  
  gen1 = unpack_sprng(s);
  if(gen1 != NULL)	    /* NULL should be returned for invalid string    */
    printf("FAILED: Generator unpacks invalid string\n");
  else
    report("PASSED: Generator detected invalid string while unpacking\n");
#endif

  correct = YES;
  for(i=0; i<100; i++)		/* check packing/unpacking                   */
  {
    scanf("%d\n", &tempi);
    
    if(tempi != isprng())
      result = correct = NO;
  }

  if(correct == NO)
    printf("FAILED: Generator does not maintain original stream when unpacked stream is invalid.\n");
  else
    report("PASSED: Generator maintains original stream when unpacked stream is invalid.\n");

  return result;
}




#ifdef USE_MPI
#ifdef __STDC__
int check_mpi_seed(unsigned int seed)
#else
int check_mpi_seed(seed)
unsigned int seed;
#endif
{
  int nprocs, myid, result = YES, i, tag=0;
  MPI_Status status;
  unsigned int temp;
  
  MPI_Comm_rank(MPI_COMM_WORLD, &myid);
  MPI_Comm_size(MPI_COMM_WORLD, &nprocs);

  if(myid != 0)
    MPI_Send(&seed, 1, MPI_UNSIGNED, 0, tag, MPI_COMM_WORLD);
  else
    for(i=1; i<nprocs; i++)
    {
      MPI_Recv(&temp,1, MPI_UNSIGNED, i, tag, MPI_COMM_WORLD, &status);
      if(temp != seed)
	result = NO;
    }
  
  if(result == NO)
    printf("FAILED: Seeds returned by make_seed differ on different processors\n");
  else
    report("PASSED: Seeds returned my make_seed on all processes are equal.\n");
  
  return result;
}
#endif







#ifdef __STDC__
main(int argc, char *argv[])
#else
main(argc, argv)
int argc;
char *argv[];
#endif
{
  int result=YES;
  int temp, myid;
  unsigned int seed1, seed2;

  report("Checking make_sprng_seed ...  ");

#ifdef USE_MPI
  MPI_Init(&argc, &argv);
#endif
  seed1 = make_sprng_seed();
#ifdef USE_MPI
  result = check_mpi_seed(seed1);
#endif

  seed2 = make_sprng_seed();
#ifdef USE_MPI
  MPI_Comm_rank(MPI_COMM_WORLD, &myid);
  if(myid==0)
  {
#endif
  if(seed1 != seed2)
    report("  ... Checked make_sprng_seed\n");
  else
  {
    result = NO;
    printf("\nERROR: make_sprng_seed does not return unique seeds\n");
  }
  scanf("%d\n", &gtype);  /* reading in a generator type */
  
  if(check_gen() != YES)
    result = NO;

  if(check_errors() != YES)
    result = NO;

  if(result == YES)
    printf("\nResult:\t PASSED\n\n");
  else
    printf("\nResult:\t FAILED\n\n");

#ifdef USE_MPI
}
  MPI_Finalize();
#endif
}