Statistiques
| Révision :

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

Historique | Voir | Annoter | Télécharger (2,52 ko)

1
//
2
// Estimation of Pi using Monte Carlo exploration process
3
// gcc -std=c99 -O3 -o Pi Pi.c -lm 
4
//
5

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

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

    
20
#define MWCfp MWC * 2.328306435454494e-10f
21
#define KISSfp KISS * 2.328306435454494e-10f
22

    
23
#define ITERATIONS 1000000000
24

    
25
#define PROCESS 4
26

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

    
33
LENGTH MainLoopGlobal(LENGTH iterations,unsigned int seed_w,unsigned int seed_z)
34
{
35
   unsigned int z=seed_z;
36
   unsigned int w=seed_w;
37

    
38
   LENGTH total=0;
39

    
40
   for (LENGTH i=0;i<iterations;i++) {
41

    
42
      float x=MWCfp ;
43
      float y=MWCfp ;
44

    
45
      // Matching test
46
      int inside=((x*x+y*y) < 1.0f) ? 1:0;
47
      total+=inside;
48
   }
49

    
50
   return(total);
51
}
52

    
53
LENGTH splitter(LENGTH iterations,unsigned int process,
54
                unsigned int seed_w,unsigned int seed_z)
55
{
56
  LENGTH inside[1024],insides=0;
57

    
58
#pragma omp parallel for
59
  for (int i=0 ; i<process; i++) {
60
    inside[i]=MainLoopGlobal(iterations/process,seed_w+process,seed_z+process);
61
    printf("\tFound %lld for process %i\n",(long long)inside[i],i);
62
  }
63
  printf("\n");
64

    
65
  for (int i=0 ; i<process; i++) {
66
    insides+=inside[i];
67
  }
68

    
69
  return(insides);
70
} 
71

    
72

    
73
int main(int argc, char *argv[]) {
74

    
75
  unsigned int seed_w=10,seed_z=10,process=PROCESS;
76
  LENGTH iterations=ITERATIONS,insides=0;
77
  
78
  if (argc > 1) {
79
    iterations=(LENGTH)atoll(argv[1]);
80
    process=atoi(argv[2]);
81
  }
82
  else {
83
    printf("\n\tPi : Estimate Pi with Monte Carlo exploration\n\n");
84
    printf("\t\t#1 : number of iterations (default 1 billion)\n");
85
    printf("\t\t#2 : number of process (default 4)\n\n");
86
  }
87

    
88
  printf ("\n\tInformation about architecture:\n\n");
89

    
90
  printf ("\tSizeof int = %lld bytes.\n", (long long)sizeof(int));
91
  printf ("\tSizeof long = %lld bytes.\n", (long long)sizeof(long));
92
  printf ("\tSizeof long long = %lld bytes.\n\n", (long long)sizeof(long long));
93

    
94
  printf ("\tMax int = %u\n", INT_MAX);
95
  printf ("\tMax long = %ld\n", LONG_MAX);
96
  printf ("\tMax long long = %lld\n\n", LLONG_MAX);
97

    
98
  insides=splitter(iterations,process,seed_w,seed_z);
99
  
100
  float pi=4.*(float)insides/(float)iterations;
101

    
102
  printf("\tPi=%f with error %f and %lld iterations\n\n",pi,
103
         fabs(pi-4*atan(1))/pi,(long long)iterations);
104
  
105
}