File: perm.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 (141 lines) | stat: -rw-r--r-- 4,494 bytes parent folder | download | duplicates (15)
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
/*********************************************************************
       Permutation Test
*********************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include "tests.h"

/* # of parameters for the test engin */
#define NUM_TEST_ENGIN_PARAM 2
/* Size of each group */
static int     grpSize;
/* # of groups in each test */
static long    numGrp;
/* # of bins */
static long    numBin;
/* # of tests repeated */
static long    numRepeat;
/* One group of random numbers */
static double  *oneGrp;
/* Array of bins */
static long    *bins;
/* Array of corresponding probabilities */
static double  *probs;
/* Array of chi-squares */
static double  *chiSqrs;

/********************************************************************/
#define FATAL_ABORT printf("Program terminated.\n"); exit(0)

/*------------------------------------------------------------------*/
#ifdef __STDC__
void initTest(int argc, char *argv[]) {
#else
void initTest(argc, argv) int argc; char *argv[]; {
#endif
   int  numParam=NUM_TEST_ENGIN_PARAM+N_STREAM_PARAM;
   long  index;
   double  temp;

   if (argc<(numParam+1)) {
      printf("Error: %i number of parameters needed\n", numParam);
      FATAL_ABORT;
   }

   grpSize = atoi(argv[N_STREAM_PARAM+1]);
   numGrp = atol(argv[N_STREAM_PARAM+2]);
   if ((grpSize<=0) || (numGrp<=0)) {
      printf("Error: incorrect parameter value(s)\n");
      FATAL_ABORT;
   }

   for (numBin=index=1;index<=grpSize;index++) numBin *= index;
   numRepeat = init_tests(argc, argv);

   oneGrp = mymalloc(sizeof(double)*grpSize);
   bins = mymalloc(sizeof(long)*numBin);
   probs = mymalloc(sizeof(double)*numBin);
   chiSqrs = mymalloc(sizeof(double)*NTESTS);

   temp = 1.0 / numBin;
   for (index=0;index<numBin;index++) probs[index] = temp;
}

/*------------------------------------------------------------------*/
#ifdef __STDC__
void deinitTest(void) {
#else
void deinitTest() {
#endif
   free(oneGrp);
   free(bins);
   free(probs);
   free(chiSqrs);
}

/*------------------------------------------------------------------*/
#define PROC_ONE_GROUP {                                       \
   long    theBin, index, r, maxPos;                           \
   double  tempHold;                                           \
                                                               \
   for (index=0;index<grpSize;index++) {                       \
      oneGrp[index] = get_rn();                                \
   }                                                           \
                                                               \
   for (theBin=0,r=grpSize;r>1;r--) {                          \
      for (index=1,maxPos=0;index<r;index++) {                 \
         if (oneGrp[maxPos]<oneGrp[index]) maxPos = index;     \
      }                                                        \
      if (maxPos!=r-1) {                                       \
        tempHold = oneGrp[r-1];                                \
        oneGrp[r-1] = oneGrp[maxPos];                          \
        oneGrp[maxPos] = tempHold;                             \
      }                                                        \
      theBin = theBin * r + maxPos;                            \
   }                                                           \
   bins[theBin]++;                                             \
}

/********************************************************************/
#ifdef __STDC__
void main(int argc, char *argv[]) {
#else
void main(argc, argv) int argc; char *argv[]; {
#endif
   long    curRound, index;
   double  KSvalue, KSprob;
   int Bins_used;
   
   initTest(argc, argv);
   for (curRound=0;curRound<numRepeat;curRound++) {
      for (index=0;index<numBin;index++) bins[index] = 0;
      for (index=0;index<numGrp;index++) PROC_ONE_GROUP;

      chiSqrs[curRound] = chisquare(bins, probs, numGrp, numBin, &Bins_used);
      /*printf("\tChisquare for stream = %f, %% = %f\n",chiSqrs[curRound] , 
	     chipercent(chiSqrs[curRound],numBin-1));*/
      next_stream();
   }

  
#if defined(SPRNG_MPI)
  getKSdata(chiSqrs,NTESTS);
#endif
  
  if(proc_rank == 0)
  {
    set_d_of_f(Bins_used-1);
    KSvalue = KS(chiSqrs, NTESTS, chiF);
    KSprob = KSpercent(KSvalue, NTESTS);

    printf("Result: KS value = %f\n", (float) KSvalue);
    printf("        KS value prob = %f %%\n\n", (float) KSprob*100);
    deinitTest();
 }
   

#if defined(SPRNG_MPI)
     MPI_Finalize();
#endif

}