Révision 247 Pi/C/Hybrid/Pi_Hybrid.c

Pi_Hybrid.c (revision 247)
1 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
2
// Estimation of Pi using Monte Carlo exploration process using OpenMP & MPI
3
// Cecill v2 Emmanuel QUEMENER <emmanuel.quemener@gmail.com>
4
// gcc -std=c99 -O3 -o Pi_Hybrid Pi_Hybrid.c -lm 
6 5

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

  
11 10
#include <math.h>
......
159 158
  LENGTH iterations=ITERATIONS,insideMPI[8192],insideOpenMP[1024],
160 159
    part_inside=0,part_iterations,insides=0;
161 160
  int numtasks,rank,rc,tag=1,i;
162
  float pi;
163 161
  
164 162
  // Hostname supposed to be <128 characters
165 163
  char hostname[128];
......
207 205
  MPI_Type_commit(&mpi_result_type);
208 206
  
209 207
  if (rank==0) {
208

  
209
    struct timeval tv1,tv2;
210 210
    
211 211
    if (argc > 1) {
212 212
      iterations=(LENGTH)atoll(argv[1]);
......
234 234
    send.iterations=part_iterations;
235 235
    send.process=process;
236 236

  
237
    gettimeofday(&tv1, NULL);
238
        
237 239
    // Split part of code
238 240
    for (i=1;i<numtasks;i++) {      
239 241
      rc = MPI_Send(&send, 1, mpi_node_type, i, tag, MPI_COMM_WORLD);
......
280 282
      insides+=insideMPI[i];
281 283
    }
282 284
    
283
    pi=4.*(float)insides/(float)(part_iterations*numtasks*process);
285
    gettimeofday(&tv2, NULL);
286

  
287
    double elapsed=(double)((tv2.tv_sec-tv1.tv_sec) * 1000000L +
288
			      (tv2.tv_usec-tv1.tv_usec))/1000000;
284 289
    
285
    printf("\n\tPi=%.40f\n\twith error %.40f\n\twith %lld iterations\n\n",pi,
286
	   fabs(pi-4*atan(1.))/pi,(long long)(part_iterations*numtasks*process));
290
    double itops=(double)(part_iterations*numtasks)/elapsed;
287 291
    
292
    printf("\nParallelRate %i\nElapsed Time %.2f\nItops %.0f\nLogItops %.2f\n",numtasks*process,elapsed,itops,log10(itops));
293
    
294
    LENGTH total=((iterations%numtasks)==0)?iterations:(iterations/numtasks+1)*numtasks;
295

  
296
    printf("Inside/Total %ld %ld\nPi estimation %f\n\n",(long int)insides,(long int)total,(4.*(float)insides/total));
297
    
288 298
  }
289 299
  else
290 300
    {

Formats disponibles : Unified diff