Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Update perf
[simgrid.git] / teshsuite / smpi / mpich3-test / rma / ircpi.c
1 /* -*- Mode: C; c-basic-offset:4 ; indent-tabs-mode:nil ; -*- */
2 /*
3  *  (C) 2001 by Argonne National Laboratory.
4  *      See COPYRIGHT in top-level directory.
5  */
6 #include "mpi.h" 
7 #include "stdio.h"
8 #include <math.h> 
9
10 /* From Using MPI-2 */
11
12 int main(int argc, char *argv[]) 
13
14     int n, myid, numprocs, i, ierr; 
15     double PI25DT = 3.141592653589793238462643; 
16     double mypi, pi, h, sum, x; 
17     MPI_Win nwin, piwin; 
18  
19     MPI_Init(&argc,&argv); 
20     MPI_Comm_size(MPI_COMM_WORLD,&numprocs); 
21     MPI_Comm_rank(MPI_COMM_WORLD,&myid); 
22  
23     if (myid == 0) { 
24         MPI_Win_create(&n, sizeof(int), 1, MPI_INFO_NULL, 
25                        MPI_COMM_WORLD, &nwin); 
26         MPI_Win_create(&pi, sizeof(double), 1, MPI_INFO_NULL, 
27                        MPI_COMM_WORLD, &piwin);  
28     } 
29     else { 
30         MPI_Win_create(MPI_BOTTOM, 0, 1, MPI_INFO_NULL, 
31                        MPI_COMM_WORLD, &nwin); 
32         MPI_Win_create(MPI_BOTTOM, 0, 1, MPI_INFO_NULL, 
33                        MPI_COMM_WORLD, &piwin); 
34     } 
35     while (1) { 
36         if (myid == 0) { 
37             fprintf(stdout, "Enter the number of intervals: (0 quits) ");
38             fflush(stdout); 
39             ierr=scanf("%d",&n); 
40             pi = 0.0;                    
41         } 
42         MPI_Win_fence(0, nwin); 
43         if (myid != 0)  
44             MPI_Get(&n, 1, MPI_INT, 0, 0, 1, MPI_INT, nwin); 
45         MPI_Win_fence(0, nwin); 
46         if (n == 0) 
47             break; 
48         else { 
49             h   = 1.0 / (double) n; 
50             sum = 0.0; 
51             for (i = myid + 1; i <= n; i += numprocs) { 
52                 x = h * ((double)i - 0.5); 
53                 sum += (4.0 / (1.0 + x*x)); 
54             } 
55             mypi = h * sum; 
56             MPI_Win_fence( 0, piwin); 
57             MPI_Accumulate(&mypi, 1, MPI_DOUBLE, 0, 0, 1, MPI_DOUBLE, 
58                            MPI_SUM, piwin); 
59             MPI_Win_fence(0, piwin); 
60             if (myid == 0) { 
61                 fprintf(stdout, "pi is approximately %.16f, Error is %.16f\n", 
62                        pi, fabs(pi - PI25DT)); 
63                 fflush(stdout);
64             }
65         } 
66     } 
67     MPI_Win_free(&nwin); 
68     MPI_Win_free(&piwin); 
69     MPI_Finalize(); 
70     return 0; 
71