Statistiques
| Révision :

root / Pi / C / Simple / Pi_Simple.c @ 191

Historique | Voir | Annoter | Télécharger (4,11 ko)

1
//
2
// Estimation of Pi using Monte Carlo exploration process
3
// Cecill v2 Emmanuel QUEMENER <emmanuel.quemener@gmail.com>
4
// gcc -O3 -o Pi_Simple Pi_Simple.c -lm 
5
//
6

    
7
#include <math.h>
8
#include <stdio.h>
9
#include <stdlib.h>
10
#include <omp.h>
11
#include <limits.h>
12

    
13
// Marsaglia RNG very simple implementation
14
#define znew  ((z=36969*(z&65535)+(z>>16))<<16)
15
#define wnew  ((w=18000*(w&65535)+(w>>16))&65535)
16
#define MWC   (znew+wnew)
17
#define SHR3  (jsr=(jsr=(jsr=jsr^(jsr<<17))^(jsr>>13))^(jsr<<5))
18
#define CONG  (jcong=69069*jcong+1234567)
19
#define KISS  ((MWC^CONG)+SHR3)
20

    
21
#define MWCfp (float)MWC*2.3283064365386963e-10f
22
#define KISSfp (float)KISS*2.328306435454494e-10f
23
#define SHR3fp (float)SHR3*2.328306435454494e-10f
24
#define CONGfp (float)CONG*2.328306435454494e-10f
25

    
26
#define ITERATIONS 1000000000
27

    
28
#ifdef LONG
29
#define LENGTH long long
30
#else
31
#define LENGTH int
32
#endif
33

    
34
LENGTH MainLoopGlobal(LENGTH iterations,unsigned int seed_w,unsigned int seed_z)
35
{
36
#if defined TCONG
37
   unsigned int jcong=seed_z;
38
#elif defined TSHR3
39
   unsigned int jsr=seed_w;
40
#elif defined TMWC
41
   unsigned int z=seed_z;
42
   unsigned int w=seed_w;
43
#elif defined TKISS
44
   unsigned int jcong=seed_z;
45
   unsigned int jsr=seed_w;
46
   unsigned int z=seed_z;
47
   unsigned int w=seed_w;
48
#endif
49

    
50
   LENGTH total=0,i;
51
   unsigned long inside;
52
     
53
   for (i=0;i<iterations;i++) {
54

    
55
#if defined TINT32
56
    #define THEONE 1073741824
57
    #if defined TCONG
58
        unsigned int x=CONG>>17 ;
59
        unsigned int y=CONG>>17 ;
60
    #elif defined TSHR3
61
        unsigned int x=SHR3>>17 ;
62
        unsigned int y=SHR3>>17 ;
63
    #elif defined TMWC
64
        unsigned int x=MWC>>17 ;
65
        unsigned int y=MWC>>17 ;
66
    #elif defined TKISS
67
        unsigned int x=KISS>>17 ;
68
        unsigned int y=KISS>>17 ;
69
    #endif
70
#elif defined TINT64
71
    #define THEONE 4611686018427387904
72
    #if defined TCONG
73
        unsigned long x=(unsigned long)(CONG>>1) ;
74
        unsigned long y=(unsigned long)(CONG>>1) ;
75
    #elif defined TSHR3
76
        unsigned long x=(unsigned long)(SHR3>>1) ;
77
        unsigned long y=(unsigned long)(SHR3>>1) ;
78
    #elif defined TMWC
79
        unsigned long x=(unsigned long)(MWC>>1) ;
80
        unsigned long y=(unsigned long)(MWC>>1) ;
81
    #elif defined TKISS
82
        unsigned long x=(unsigned long)(KISS>>1) ;
83
        unsigned long y=(unsigned long)(KISS>>1) ;
84
    #endif
85
#elif defined TFP32
86
#define THEONE (float)1.0f
87
    #if defined TCONG
88
        float x=CONGfp ;
89
        float y=CONGfp ;
90
    #elif defined TSHR3
91
        float x=SHR3fp ;
92
        float y=SHR3fp ;
93
    #elif defined TMWC
94
        float x=MWCfp ;
95
        float y=MWCfp ;
96
    #elif defined TKISS
97
      float x=KISSfp ;
98
      float y=KISSfp ;
99
    #endif
100
#elif defined TFP64
101
#define THEONE (double)1.0f
102
    #if defined TCONG
103
        double x=(double)CONGfp ;
104
        double y=(double)CONGfp ;
105
    #elif defined TSHR3
106
        double x=(double)SHR3fp ;
107
        double y=(double)SHR3fp ;
108
    #elif defined TMWC
109
        double x=(double)MWCfp ;
110
        double y=(double)MWCfp ;
111
    #elif defined TKISS
112
        double x=(double)KISSfp ;
113
        double y=(double)KISSfp ;
114
    #endif
115
#endif
116

    
117
      inside=((x*x+y*y) < THEONE) ? 1:0;
118
      total+=inside;
119
   }
120

    
121
   return(total);
122
}
123

    
124
int main(int argc, char *argv[]) {
125

    
126
  unsigned int seed_w=110271,seed_z=101008;
127
  LENGTH iterations=ITERATIONS,inside=0;
128
  
129
  if (argc > 1) {
130
    iterations=(LENGTH)atoll(argv[1]);
131
  }
132
  else {
133
    printf("\n\tPi : Estimate Pi with Monte Carlo exploration\n\n");
134
    printf("\t\t#1 : number of iterations (default 1 billion)\n");
135
  }
136

    
137
  printf ("\n\tInformation about architecture:\n\n");
138

    
139
  printf ("\tSizeof int = %lld bytes.\n", (long long)sizeof(int));
140
  printf ("\tSizeof long = %lld bytes.\n", (long long)sizeof(long));
141
  printf ("\tSizeof long long = %lld bytes.\n\n", (long long)sizeof(long long));
142

    
143
  printf ("\tMax int = %u\n", INT_MAX);
144
  printf ("\tMax long = %ld\n", LONG_MAX);
145
  printf ("\tMax long long = %lld\n\n", LLONG_MAX);
146

    
147
  inside=MainLoopGlobal(iterations,seed_w,seed_z);
148
  
149
  float pi=4.*(float)inside/(float)iterations;
150

    
151
  printf("\t%lld inside for %lld iterations\n",
152
         (long long)inside,(long long)iterations);
153
  printf("\tPi=%f with error %f\n\n",
154
         pi,fabs(pi-4*atan(1))/pi);
155
  
156
}