root / Pi / C / Simple / Pi_Simple.c
Historique | Voir | Annoter | Télécharger (4,38 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 <limits.h> |
| 11 |
#include <sys/time.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 |
#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 |
struct timeval tv1,tv2;
|
| 129 |
struct timezone tz;
|
| 130 |
|
| 131 |
if (argc > 1) { |
| 132 |
iterations=(LENGTH)atoll(argv[1]);
|
| 133 |
} |
| 134 |
else {
|
| 135 |
printf("\n\tPi : Estimate Pi with Monte Carlo exploration\n\n");
|
| 136 |
printf("\t\t#1 : number of iterations (default 1 billion)\n");
|
| 137 |
} |
| 138 |
|
| 139 |
printf ("\n\tInformation about architecture:\n\n");
|
| 140 |
|
| 141 |
printf ("\tSizeof int = %lld bytes.\n", (long long)sizeof(int)); |
| 142 |
printf ("\tSizeof long = %lld bytes.\n", (long long)sizeof(long)); |
| 143 |
printf ("\tSizeof long long = %lld bytes.\n\n", (long long)sizeof(long long)); |
| 144 |
|
| 145 |
printf ("\tMax int = %u\n", INT_MAX);
|
| 146 |
printf ("\tMax long = %ld\n", LONG_MAX);
|
| 147 |
printf ("\tMax long long = %lld\n\n", LLONG_MAX);
|
| 148 |
|
| 149 |
// Set start timer
|
| 150 |
gettimeofday(&tv1, &tz); |
| 151 |
|
| 152 |
inside=MainLoopGlobal(iterations,seed_w,seed_z); |
| 153 |
|
| 154 |
// Set stop timer
|
| 155 |
gettimeofday(&tv2, &tz); |
| 156 |
|
| 157 |
double elapsed=(double)((tv2.tv_sec-tv1.tv_sec) * 1000000L + |
| 158 |
(tv2.tv_usec-tv1.tv_usec))/1000000;
|
| 159 |
|
| 160 |
double itops=(double)(iterations)/elapsed; |
| 161 |
|
| 162 |
printf("Inside/Total %lld %lld\nElapsed Time %.2f\nItops %.0f\nLogItops %.2f\nPi estimation %f\n\n",inside,iterations,elapsed,itops,log10(itops),(4.*(float)inside/(float)(iterations))); |
| 163 |
|
| 164 |
return 0; |
| 165 |
|
| 166 |
} |