Statistiques
| Révision :

root / Pi / C / MPI / Pi_MPI.c @ 283

Historique | Voir | Annoter | Télécharger (8,13 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_MPI Pi_MPI.c -lm
5

    
6
// Needed for gethostname
7
#define _DEFAULT_SOURCE
8
#include <sys/unistd.h>
9

    
10
#include <math.h>
11
#include <stdio.h>
12
#include <stdlib.h>
13
#include <limits.h>
14
#include <mpi.h>
15
#include <stddef.h>
16
#include <sys/time.h>
17

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

    
26
#define MWCfp MWC * 2.328306435454494e-10f
27
#define KISSfp KISS * 2.328306435454494e-10f
28
#define SHR3fp SHR3 * 2.328306435454494e-10f
29
#define CONGfp CONG * 2.328306435454494e-10f
30

    
31
#define ITERATIONS 1000000000
32

    
33
#ifdef LONG
34
#define LENGTH long long
35
#else
36
#define LENGTH int
37
#endif
38

    
39
typedef struct compute_node {
40
        LENGTH inside;
41
        long int useconds;
42
} result;
43

    
44
unsigned int rotl(unsigned int value, int shift) {
45
    return (value << shift) | (value >> (sizeof(value) * CHAR_BIT - shift));
46
}
47
 
48
unsigned int rotr(unsigned int value, int shift) {
49
    return (value >> shift) | (value << (sizeof(value) * CHAR_BIT - shift));
50
}
51

    
52
LENGTH MainLoopGlobal(LENGTH iterations,unsigned int seed_w,unsigned int seed_z)
53
{
54
#if defined TCONG
55
   unsigned int jcong=seed_z;
56
#elif defined TSHR3
57
   unsigned int jsr=seed_w;
58
#elif defined TMWC
59
   unsigned int z=seed_z;
60
   unsigned int w=seed_w;
61
#elif defined TKISS
62
   unsigned int jcong=seed_z;
63
   unsigned int jsr=seed_w;
64
   unsigned int z=seed_z;
65
   unsigned int w=seed_w;
66
#endif
67

    
68
  LENGTH total=0;
69

    
70
   for (LENGTH i=0;i<iterations;i++) {
71

    
72
#if defined TINT32
73
    #define THEONE 1073741824
74
    #if defined TCONG
75
        unsigned int x=CONG>>17 ;
76
        unsigned int y=CONG>>17 ;
77
    #elif defined TSHR3
78
        unsigned int x=SHR3>>17 ;
79
        unsigned int y=SHR3>>17 ;
80
    #elif defined TMWC
81
        unsigned int x=MWC>>17 ;
82
        unsigned int y=MWC>>17 ;
83
    #elif defined TKISS
84
        unsigned int x=KISS>>17 ;
85
        unsigned int y=KISS>>17 ;
86
    #endif
87
#elif defined TINT64
88
    #define THEONE 4611686018427387904
89
    #if defined TCONG
90
        unsigned long x=(unsigned long)(CONG>>1) ;
91
        unsigned long y=(unsigned long)(CONG>>1) ;
92
    #elif defined TSHR3
93
        unsigned long x=(unsigned long)(SHR3>>1) ;
94
        unsigned long y=(unsigned long)(SHR3>>1) ;
95
    #elif defined TMWC
96
        unsigned long x=(unsigned long)(MWC>>1) ;
97
        unsigned long y=(unsigned long)(MWC>>1) ;
98
    #elif defined TKISS
99
        unsigned long x=(unsigned long)(KISS>>1) ;
100
        unsigned long y=(unsigned long)(KISS>>1) ;
101
    #endif
102
#elif defined TFP32
103
    #define THEONE 1.0f
104
    #if defined TCONG
105
        float x=CONGfp ;
106
        float y=CONGfp ;
107
    #elif defined TSHR3
108
        float x=SHR3fp ;
109
        float y=SHR3fp ;
110
    #elif defined TMWC
111
        float x=MWCfp ;
112
        float y=MWCfp ;
113
    #elif defined TKISS
114
      float x=KISSfp ;
115
      float y=KISSfp ;
116
    #endif
117
#elif defined TFP64
118
    #define THEONE 1.0f
119
    #if defined TCONG
120
        double x=(double)CONGfp ;
121
        double y=(double)CONGfp ;
122
    #elif defined TSHR3
123
        double x=(double)SHR3fp ;
124
        double y=(double)SHR3fp ;
125
    #elif defined TMWC
126
        double x=(double)MWCfp ;
127
        double y=(double)MWCfp ;
128
    #elif defined TKISS
129
        double x=(double)KISSfp ;
130
        double y=(double)KISSfp ;
131
    #endif
132
#endif
133

    
134
      // Matching test
135
      unsigned long inside=((x*x+y*y) < THEONE) ? 1:0;
136
      total+=inside;
137
   }
138
  
139
   return(total);
140
}
141

    
142
int main(int argc, char *argv[]) {
143

    
144
  unsigned int seed_z=362436069,seed_w=52128862;
145
  LENGTH iterations=ITERATIONS,inside[8192],insides,part_inside,part_iterations;
146
  int NumberProcesses,rank,rc,tag=1,i;
147

    
148
  char hostname[128];
149

    
150
  gethostname(hostname, sizeof hostname);
151

    
152
  struct timeval start,end;
153
  long int useconds;
154

    
155
  MPI_Status Stat;
156

    
157
  rc = MPI_Init(&argc,&argv);
158
  if (rc != MPI_SUCCESS) {
159
    printf ("Error starting MPI program. Terminating.\n");
160
    MPI_Abort(MPI_COMM_WORLD, rc);
161
  }
162

    
163
  MPI_Comm_size(MPI_COMM_WORLD,&NumberProcesses);
164
  MPI_Comm_rank(MPI_COMM_WORLD,&rank);
165

    
166
  const int nitems=2;
167
  int blocklengths[2] = {1,1};
168

    
169
#ifdef LONG
170
  MPI_Datatype types[2] = {MPI_LONG, MPI_LONG};
171
#else
172
  MPI_Datatype types[2] = {MPI_INT, MPI_LONG};
173
#endif
174

    
175
  MPI_Datatype mpi_result_type;
176
  MPI_Aint     offsets[2];
177

    
178
  offsets[0] = offsetof(result, inside);
179
  offsets[1] = offsetof(result, useconds);
180
  
181
  MPI_Type_create_struct(nitems, blocklengths, offsets, types, &mpi_result_type);
182
  MPI_Type_commit(&mpi_result_type);
183
    
184
  if (rank==0) {
185

    
186
    struct timeval tv1,tv2;
187

    
188
    if (argc > 1) {
189
      iterations=(LENGTH)atoll(argv[1]);
190
    }
191
    else {
192
      printf("\n\tPi : Estimate Pi with Monte Carlo exploration\n\n");
193
      printf("\t\t#1 : number of iterations (default 1 billion)\n\n");
194
    }
195
    
196
    printf ("\tSizeof int = %lld bytes.\n", (long long)sizeof(int));
197
    printf ("\tSizeof long = %lld bytes.\n", (long long)sizeof(long));
198
    printf ("\tSizeof long long = %lld bytes.\n", (long long)sizeof(long long));
199
    
200
    printf ("\tMax int = %u\n", INT_MAX);
201
    printf ("\tMax long = %ld\n", LONG_MAX);
202
    printf ("\tMax long long = %lld\n\n", LLONG_MAX);
203
    
204
    part_iterations=((iterations%NumberProcesses) == 0) ? iterations/NumberProcesses:iterations/NumberProcesses+1 ;
205

    
206
    gettimeofday(&tv1, NULL);
207
    
208
    // Split part of code
209
    for (i=1;i<NumberProcesses;i++) {
210
      
211
#ifdef LONG
212
      rc = MPI_Send(&part_iterations, 1, MPI_LONG_LONG, i, tag, 
213
                    MPI_COMM_WORLD);
214
#else
215
      rc = MPI_Send(&part_iterations, 1, MPI_INT, i, tag, 
216
                    MPI_COMM_WORLD);
217
#endif
218
    }
219
    
220
    gettimeofday(&start,(struct timezone *)0);
221
    
222
    insides=MainLoopGlobal(part_iterations,seed_w,seed_z);
223
    
224
    gettimeofday(&end,(struct timezone *)0);
225
    useconds=(end.tv_sec-start.tv_sec)*1000000+end.tv_usec-start.tv_usec;
226
    
227
    printf("\tOn %s with rank %i, find %lld inside in %lu useconds.\n",
228
             hostname,rank,(long long)insides,useconds);
229
      
230
    // Join part of code
231
      for (i=1;i<NumberProcesses;i++) {
232

    
233
        result recv;
234
        
235
        rc = MPI_Recv(&recv, 1, mpi_result_type, i, tag, 
236
                      MPI_COMM_WORLD, &Stat);
237

    
238
        inside[i]=recv.inside;
239
        useconds=recv.useconds;
240
        
241
        printf("\tReceive from %i, find %lld inside in %lu useconds\n",i,(long long)inside[i],useconds);
242
        
243
        insides+=inside[i];
244
      }
245
      
246
      float pi=4.*(float)insides/(float)(part_iterations*NumberProcesses);
247

    
248
      gettimeofday(&tv2, NULL);
249
      
250
      double elapsed=(double)((tv2.tv_sec-tv1.tv_sec) * 1000000L +
251
                              (tv2.tv_usec-tv1.tv_usec))/1000000;
252
      
253
      double itops=(double)(part_iterations*NumberProcesses)/elapsed;
254
  
255
      printf("\nParallelRate %i\nElapsed Time %.2f\nItops %.0f\nLogItops %.2f\n",NumberProcesses,elapsed,itops,log10(itops));
256

    
257
      LENGTH total=((iterations%NumberProcesses)==0)?iterations:(iterations/NumberProcesses+1)*NumberProcesses;
258

    
259
      printf("Inside/Total %ld %ld\nPi estimation %f\n\n",(long int)insides,(long int)total,(4.*(float)insides/total));
260
      
261
  }
262
  else
263
    {
264
      // Receive information from master
265
#ifdef LONG
266
      rc = MPI_Recv(&part_iterations, 1, MPI_LONG_LONG, 0, tag, 
267
                    MPI_COMM_WORLD, &Stat);
268
#else
269
      rc = MPI_Recv(&part_iterations, 1, MPI_INT, 0, tag, 
270
                    MPI_COMM_WORLD, &Stat);
271
#endif
272
      
273
      /*      Not such a good idea to print information... 
274
              printf("\tOn %s with rank %i, receive from master %lld\n",
275
              hostname,rank,(long long)part_iterations); */
276
      
277
      gettimeofday(&start,(struct timezone *)0);
278

    
279
      part_inside=MainLoopGlobal(part_iterations,rotr(seed_w,rank),rotl(seed_z,rank));
280
      
281
      gettimeofday(&end,(struct timezone *)0);
282
      useconds=(end.tv_sec-start.tv_sec)*1000000+end.tv_usec-start.tv_usec;
283

    
284
      /*      
285
      printf("\tOn %s with rank %i, find %lld inside in %lu useconds.\n",
286
             hostname,rank,(long long)part_inside,useconds);
287
      */
288

    
289
      result send;
290
      send.inside=part_inside;
291
      send.useconds=useconds;
292

    
293
      rc = MPI_Send(&send, 1, mpi_result_type, 0, tag, MPI_COMM_WORLD);
294

    
295
    }
296

    
297
  MPI_Type_free(&mpi_result_type);  
298
  MPI_Finalize();
299

    
300
}