1 /*  Note: This test is not in the regular SPRNG test format */
2 
3 
4 #include <cstdio>
5 #include <cstdlib>
6 #include <cstring>
7 #if defined(SPRNG_MPI)
8 #include "mpi.h"
9 #endif
10 #include "tests.h"
11 #include <cmath>
12 
13 using namespace std;
14 
15 #define min(a,b) ((a)<(b))?(a):(b)
16 #define max(a,b) ((a)>(b))?(a):(b)
17 
18 double test_normal(double *array, int n);
19 void set_normal_params(double mu, double sd);
20 double normalF(double x);
21 void mean_sd(double *x, int n, double *mean, double *sd);
22 
23 int group_size;
24 
main(int argc,char * argv[])25 int main(int argc, char *argv[])
26 {
27   int ntests, n, i, j, k;
28   double result, *temparray, *sumarray;
29 
30 
31   /****************** Initialization values *******************************/
32 
33   if(argc != N_STREAM_PARAM+3 || atoi(argv[2]) != 1)
34   {
35     fprintf(stderr,"USAGE: %s nstreams 1 seed param 1 0 n group_size\n",
36 	    argv[0]);
37     exit(-1);
38   }
39 
40   ntests = init_tests(argc,argv);
41 
42   n = atoi(argv[N_STREAM_PARAM+1]);
43   group_size = atoi(argv[N_STREAM_PARAM+2]);
44 
45   temparray = new double[n];
46   sumarray = new double[n];
47 
48   for(j=0; j<n; j++)
49     temparray[j] = 0.0;
50 
51   for(i=0; i<ntests; i++)	/* sum elements from each sequence */
52   {
53     for(j=0; j<n; j++)
54       for(k=0; k<group_size; k++)
55       {
56 	temparray[j] += get_rn();
57       }
58     next_stream();
59   }
60 
61 #if defined(SPRNG_MPI)
62   MPI_Reduce(temparray,sumarray, n, MPI_DOUBLE, MPI_SUM, 0, MPI_COMM_WORLD);
63   delete [] temparray;
64   temparray = sumarray;
65 
66 #endif
67 
68   if(proc_rank == 0)
69   {
70     result = test_normal(temparray,n);
71     printf("KS Percent =  %f %%\n", result*100);
72   }
73 
74   delete [] temparray;
75 
76 #if defined(SPRNG_MPI)
77      MPI_Finalize();
78 #endif
79 
80   return 0;
81 }
82 
test_normal(double * array,int n)83 double test_normal(double *array, int n)
84 {
85   double mean1, sd1, mean2, sd2, result, percent, error;
86   int i;
87 
88   mean2 = NTESTS*(group_size/2.0);
89   sd2 = sqrt( (double) NTESTS*(group_size/12.0) );
90   for(i=0; i<n; i++)
91     array[i] = (array[i] - mean2)/sd2;
92 
93   mean_sd(array,n,&mean1,&sd1);
94   printf("\tMean = %f\tstandard deviation = %f\n", mean1, sd1);
95 
96   set_normal_params(0.0, 1.0);
97   result = KS(array,n,normalF);
98 
99   /* State error in using the normal distribution instead of the exact      */
100   /* distribution. Note that the formula used has been empirically derived, */
101   /* and verified only for NTESTS*groupsize <= 10^5                         */
102   error = 0.0275/NTESTS/group_size*sqrt((double) n);
103   printf("\tKS range = [%f, %f]%%\n", 100*KSpercent(max(0.0,result-error),n),
104 	 100*KSpercent(min(sqrt((double) n),result+error),n) );
105 
106   percent = KSpercent(result,n);
107 
108   return percent;
109 }
110 
111