Statistiques
| Révision :

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

Historique | Voir | Annoter | Télécharger (5,09 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
#include <sys/time.h>
13

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

    
22
#define MWCfp MWC * 2.328306435454494e-10f
23
#define KISSfp KISS * 2.328306435454494e-10f
24
#define SHR3fp SHR3 * 2.328306435454494e-10f
25
#define CONGfp CONG * 2.328306435454494e-10f
26

    
27
#define ITERATIONS 1000000000
28

    
29
#define PARALLELRATE 1024
30

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

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

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

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

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

    
123
   return(total);
124
}
125

    
126
LENGTH splitter(LENGTH iterations,unsigned int seed_w,unsigned int seed_z,unsigned int ParallelRate)
127
{
128
  LENGTH *inside,insides=0;
129
  struct timeval tv1,tv2;
130
  LENGTH IterationsEach=((iterations%ParallelRate)==0)?iterations/ParallelRate:iterations/ParallelRate+1;
131

    
132
#pragma omp parallel for  
133
  for (int i=0 ; i<process; i++) {
134
    inside[i]=MainLoopGlobal(iterations/process,seed_w+i,seed_z+i);
135
  }
136

    
137
  for (int i=0 ; i<process; i++) {
138
    printf("\tFound %lld for process %i\n",(long long)inside[i],i);
139
    insides+=inside[i];
140
  }
141
  printf("\n");
142

    
143
  double elapsed=(double)((tv2.tv_sec-tv1.tv_sec) * 1000000L +
144
                            (tv2.tv_usec-tv1.tv_usec))/1000000;
145
  
146
  double itops=(double)(ParallelRate*IterationsEach)/elapsed;
147
  
148
  printf("ParallelRate %i\nElapsed Time %.2f\nItops %.0f\n",ParallelRate,elapsed,itops);
149

    
150
  free(inside);
151
  
152
  return(insides);
153
} 
154

    
155

    
156
int main(int argc, char *argv[]) {
157

    
158
  unsigned int seed_w=110271,seed_z=101008,ParallelRate=PARALLELRATE;
159
  LENGTH iterations=ITERATIONS,insides=0;
160
  
161
  if (argc > 1) {
162
    iterations=(LENGTH)atoll(argv[1]);
163
    ParallelRate=atoi(argv[2]);
164
  }
165
  else {
166
    printf("\n\tPi : Estimate Pi with Monte Carlo exploration\n\n");
167
    printf("\t\t#1 : number of iterations (default 1 billion)\n");
168
    printf("\t\t#2 : number of ParallelRate (default 1024)\n\n");
169
  }
170

    
171
  printf ("\n\tInformation about architecture:\n\n");
172

    
173
  printf ("\tSizeof int = %lld bytes.\n", (long long)sizeof(int));
174
  printf ("\tSizeof long = %lld bytes.\n", (long long)sizeof(long));
175
  printf ("\tSizeof long long = %lld bytes.\n\n", (long long)sizeof(long long));
176

    
177
  printf ("\tMax int = %u\n", INT_MAX);
178
  printf ("\tMax long = %ld\n", LONG_MAX);
179
  printf ("\tMax long long = %lld\n\n", LLONG_MAX);
180

    
181
  insides=splitter(iterations,seed_w,seed_z,ParallelRate);
182

    
183
  LENGTH total=((iterations%ParallelRate)==0)?iterations:(iterations/ParallelRate+1)*ParallelRate;
184

    
185
  printf("Inside/Total %ld %ld\nPi estimation %f\n\n",(long int)insides,(long int)total,(4.*(float)insides/total));
186
  
187
}