Statistiques
| Révision :

root / Pi / C / Hybrid / Pi_Hybrid.c @ 76

Historique | Voir | Annoter | Télécharger (6,78 ko)

1
//
2
// Estimation of Pi using Monte Carlo exploration process
3
// gcc -std=c99 -O3 -o Pi Pi.c -lm 
4
// Emmanuel Quemener <emmanuel.quemener@ens-lyon.fr>
5
// Cecill v2
6

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

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

    
18
#ifdef TIME
19
#include <sys/time.h>
20
#endif
21

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

    
30
#define MWCfp MWC * 2.328306435454494e-10f
31
#define KISSfp KISS * 2.328306435454494e-10f
32

    
33
#define ITERATIONS 1000000000
34

    
35
#define PROCESS 1
36

    
37
#ifdef LONG
38
#define LENGTH long long
39
#else
40
#define LENGTH int
41
#endif
42

    
43
typedef struct compute_node {
44
        LENGTH iterations;
45
        int process;
46
} node;
47

    
48
unsigned int rotl(unsigned int value, int shift) {
49
    return (value << shift) | (value >> (sizeof(value) * CHAR_BIT - shift));
50
}
51
 
52
unsigned int rotr(unsigned int value, int shift) {
53
    return (value >> shift) | (value << (sizeof(value) * CHAR_BIT - shift));
54
}
55

    
56
LENGTH MainLoopGlobal(LENGTH iterations,unsigned int seed_w,unsigned int seed_z)
57
{
58
   unsigned int z=seed_z;
59
   unsigned int w=seed_w;
60

    
61
   LENGTH total=0;
62

    
63
   for (LENGTH i=0;i<iterations;i++) {
64

    
65
      float x=MWCfp ;
66
      float y=MWCfp ;
67

    
68
      // Matching test
69
      int inside=((x*x+y*y) < 1.0f) ? 1:0;
70
      total+=inside;
71
   }
72

    
73
   return(total);
74

    
75
}
76

    
77
int main(int argc, char *argv[]) {
78

    
79
  unsigned int seed_z=362436069,seed_w=52128862,process=PROCESS;
80
  // Number of NP or OpenMP processes <1024
81
  LENGTH iterations=ITERATIONS,insideMPI[1024],insideOpenMP[1024],
82
    part_inside,part_iterations,insides;
83
  int numtasks,rank,rc,tag=1,i;
84
  float pi;
85
  
86
  // Hostname supposed to be <128 characters
87
  char hostname[128];
88

    
89
  gethostname(hostname, sizeof hostname);
90

    
91
#ifdef TIME
92
  struct timeval start,end;
93
  long int useconds;
94
#endif
95

    
96
  MPI_Status Stat;
97
  MPI_Request RequestSend,RequestRecv,RequestSend2,RequestRecv2;
98
  
99
  rc = MPI_Init(&argc,&argv);
100
  if (rc != MPI_SUCCESS) {
101
    printf ("Error starting MPI program. Terminating.\n");
102
    MPI_Abort(MPI_COMM_WORLD, rc);
103
  }
104

    
105
  MPI_Comm_size(MPI_COMM_WORLD,&numtasks);
106

    
107
    const int nitems=2;
108
    int blocklengths[2] = {1,1};
109

    
110
#ifdef LONG
111
    MPI_Datatype types[2] = {MPI_LONG, MPI_INT};
112
#else
113
    MPI_Datatype types[2] = {MPI_INT, MPI_INT};
114
#endif
115

    
116
    MPI_Datatype mpi_node_type;
117
    MPI_Aint     offsets[2];
118

    
119
    offsets[0] = offsetof(node, iterations);
120
    offsets[1] = offsetof(node, process);
121

    
122
    MPI_Type_create_struct(nitems, blocklengths, offsets, types, &mpi_node_type);
123
    MPI_Type_commit(&mpi_node_type);
124

    
125
    MPI_Comm_rank(MPI_COMM_WORLD,&rank);
126

    
127
  if (rank==0) {
128
    
129
    if (argc > 1) {
130
      iterations=(LENGTH)atoll(argv[1]);
131
      process=atoi(argv[2]);
132
    }
133
    else {
134
      printf("\n\tPi : Estimate Pi with Monte Carlo exploration\n\n");
135
      printf("\t\t#1 : number of iterations (default 1 billion)\n\n");
136
      printf("\t\t#1 : number of OpenMP processes (default 1)\n\n");
137
    }
138
    
139
    printf ("\n\tInformation about architecture:\n\n");
140

    
141
    printf ("Sizeof int = %lld bytes.\n", (long long)sizeof(int));
142
    printf ("Sizeof long = %lld bytes.\n", (long long)sizeof(long));
143
    printf ("Sizeof long long = %lld bytes.\n", (long long)sizeof(long long));
144
    
145
    printf ("Max int = %u\n", INT_MAX);
146
    printf ("Max long = %ld\n", LONG_MAX);
147
    printf ("Max long long = %lld\n\n", LLONG_MAX);
148
    
149
    part_iterations=iterations/(numtasks*process)+1;
150
    
151
    node send;
152
    send.iterations=part_iterations;
153
    send.process=process;
154

    
155
    // Split part of code
156
    for (i=1;i<numtasks;i++) {      
157
      rc = MPI_Isend(&send, 1, mpi_node_type, i, tag, MPI_COMM_WORLD, &RequestSend);
158
    }
159
    MPI_Wait(&RequestSend,&Stat);
160
    
161
#ifdef TIME
162
    gettimeofday(&start,(struct timezone *)0);
163
#endif
164
    
165
#pragma omp parallel for
166
  for (int i=0 ; i<process; i++) {
167
    insideOpenMP[i]=MainLoopGlobal(part_iterations,
168
                                   rotr(seed_w,process),
169
                                   rotl(seed_z,process));
170
    printf("\t(%s,%i) found %lld for process %i\n",hostname,0,
171
           (long long)insideOpenMP[i],i);
172
  }
173
  printf("\n");
174

    
175
  insides=0;
176
  for (int i=0 ; i<process; i++) {
177
    insides+=insideOpenMP[i];
178
  }
179

    
180
#ifdef TIME
181
    gettimeofday(&end,(struct timezone *)0);
182
    useconds=(end.tv_sec-start.tv_sec)*1000000+end.tv_usec-start.tv_usec;
183
    
184
      printf("\tOn %s with %i find %lld inside in %lu useconds.\n",
185
             hostname,rank,(long long)insides,useconds);
186
#else
187
      printf("\tOn %s with %i find %lld inside\n",hostname,rank,
188
             (long long)insides);
189
      
190
#endif
191
      
192
    // Join part of code
193
      for (i=1;i<numtasks;i++) {
194
#ifdef LONG
195
        rc = MPI_Irecv(&insideMPI[i], 1, MPI_LONG_LONG, i, tag, 
196
                       MPI_COMM_WORLD, &RequestRecv2);
197
#else
198
        rc = MPI_Irecv(&insideMPI[i], 1, MPI_INT, i, tag, 
199
                       MPI_COMM_WORLD, &RequestRecv2);
200
#endif
201
        MPI_Wait(&RequestRecv2,&Stat);
202
        printf("\tReceive %lu inside from rank %i\n",(unsigned long)insideMPI[i],i);
203
        insides+=insideMPI[i];
204
      }
205
      
206
      pi=4.*(float)insides/(float)((iterations/numtasks)*numtasks);
207
      
208
      printf("\n\tPi=%.40f\n\twith error %.40f\n\twith %lld iterations\n\n",pi,
209
             fabs(pi-4*atan(1.))/pi,(long long)iterations);
210

    
211
  }
212
  else
213
    {
214
      // Receive information from master
215
     
216
      node recv;
217

    
218
      rc = MPI_Irecv(&recv, 1, mpi_node_type, 0, tag, MPI_COMM_WORLD, &RequestRecv);
219
      MPI_Wait(&RequestRecv,&Stat);
220
      
221
      printf("\t(%s,%i) receive from master %lld with %i process\n",
222
             hostname,rank,(long long)recv.iterations,recv.process);
223
      
224
#ifdef TIME
225
      gettimeofday(&start,(struct timezone *)0);
226
#endif
227

    
228
#pragma omp parallel for
229
  for (int i=0 ; i<recv.process; i++) {
230
      insideOpenMP[i]=MainLoopGlobal(recv.iterations,rotr(seed_w,rank+process),rotl(seed_z,rank+process));
231
      printf("\t(%s,%i) found %lld for process %i\n",hostname,rank,
232
             (long long)insideOpenMP[i],i);
233
  }
234
  printf("\n");
235

    
236
  part_inside=0;
237
  for (int i=0 ; i<recv.process; i++) {
238
    part_inside+=insideOpenMP[i];
239
  }
240
      
241
#ifdef TIME
242
      gettimeofday(&end,(struct timezone *)0);
243
      useconds=(end.tv_sec-start.tv_sec)*1000000+end.tv_usec-start.tv_usec;
244
      
245
      printf("\tOn %s rank %i find %lld inside in %lu useconds.\n",
246
             hostname,rank,(long long)part_inside,useconds);
247
#else
248
      printf("\tOn %s rank %i find %lld inside\n",hostname,rank,
249
             (long long)part_inside);
250
      
251
#endif
252

    
253
#ifdef LONG
254
      rc = MPI_Isend(&part_inside, 1, MPI_LONG_LONG, 0, tag, MPI_COMM_WORLD, &RequestSend2);
255
#else
256
      rc = MPI_Isend(&part_inside, 1, MPI_INT, 0, tag, MPI_COMM_WORLD, &RequestSend2);
257
#endif
258

    
259
      MPI_Wait(&RequestSend2, &Stat);
260
    }
261
  
262
  MPI_Type_free(&mpi_node_type);
263

    
264
  MPI_Finalize();
265
  
266
}