Statistiques
| Révision :

root / Pi / C / OpenMP / Pi_OpenMP.c @ 126

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

1
//
2
// Estimation of Pi using Monte Carlo exploration process
3
// Cecill v2 Emmanuel QUEMENER <emmanuel.quemener@gmail.com>
4
// gcc -std=c99 -O3 -o Pi Pi.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 MWC * 2.328306435454494e-10f
22
#define KISSfp KISS * 2.328306435454494e-10f
23
#define SHR3fp SHR3 * 2.328306435454494e-10f
24
#define CONGfp CONG * 2.328306435454494e-10f
25

    
26
#define ITERATIONS 1000000000
27

    
28
#define PROCESS 4
29

    
30
#ifdef LONG
31
#define LENGTH long long
32
#else
33
#define LENGTH int
34
#endif
35

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

    
52
   LENGTH total=0;
53
   
54
   for (LENGTH i=0;i<iterations;i++) {
55

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

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

    
122
   return(total);
123
}
124

    
125
LENGTH splitter(LENGTH iterations,unsigned int process,
126
                unsigned int seed_w,unsigned int seed_z)
127
{
128
  // Trashy static allocation...
129
  LENGTH inside[4096],insides=0;
130

    
131
#pragma omp parallel for
132
  for (int i=0 ; i<process; i++) {
133
    inside[i]=MainLoopGlobal(iterations/process,seed_w+process,seed_z+process);
134
    printf("\tFound %lld for process %i\n",(long long)inside[i],i);
135
  }
136
  printf("\n");
137

    
138
  for (int i=0 ; i<process; i++) {
139
    insides+=inside[i];
140
  }
141

    
142
  return(insides);
143
} 
144

    
145

    
146
int main(int argc, char *argv[]) {
147

    
148
  unsigned int seed_w=10,seed_z=10,process=PROCESS;
149
  LENGTH iterations=ITERATIONS,insides=0;
150
  
151
  if (argc > 1) {
152
    iterations=(LENGTH)atoll(argv[1]);
153
    process=atoi(argv[2]);
154
  }
155
  else {
156
    printf("\n\tPi : Estimate Pi with Monte Carlo exploration\n\n");
157
    printf("\t\t#1 : number of iterations (default 1 billion)\n");
158
    printf("\t\t#2 : number of process (default 4)\n\n");
159
  }
160

    
161
  printf ("\n\tInformation about architecture:\n\n");
162

    
163
  printf ("\tSizeof int = %lld bytes.\n", (long long)sizeof(int));
164
  printf ("\tSizeof long = %lld bytes.\n", (long long)sizeof(long));
165
  printf ("\tSizeof long long = %lld bytes.\n\n", (long long)sizeof(long long));
166

    
167
  printf ("\tMax int = %u\n", INT_MAX);
168
  printf ("\tMax long = %ld\n", LONG_MAX);
169
  printf ("\tMax long long = %lld\n\n", LLONG_MAX);
170

    
171
  insides=splitter(iterations,process,seed_w,seed_z);
172
  
173
  float pi=4.*(float)insides/(float)iterations;
174

    
175
  printf("\tPi=%f with error %f and %lld iterations\n\n",pi,
176
         fabs(pi-4*atan(1))/pi,(long long)iterations);
177
  
178
}