artdaq  v2_02_03
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Pages
cpiB.cc
1 /*
2  (C) 2001 by Argonne National Laboratory.
3  See COPYRIGHT in top-level directory.
4 */
5 #include "mpi.h"
6 #include <stdio.h>
7 #include <math.h>
8 #include <iostream>
9 
10 using namespace std;
11 
12 extern char** environ;
13 
14 double f(double);
15 
16 double f(double a)
17 {
18  return (4.0 / (1.0 + a * a));
19 }
20 
21 int main(int argc, char* argv[])
22 {
23  // for(int i=0;environ[i]!=0;++i) cout << environ[i] << "\n";
24 
25  cout << "booB" << endl;
26 
27  int done = 0, n, myid, numprocs, i;
28  double PI25DT = 3.141592653589793238462643;
29  double mypi, pi, h, sum, x;
30  double startwtime = 0.0, endwtime;
31  int namelen;
32  char processor_name[MPI_MAX_PROCESSOR_NAME];
33 
34  for (int i = 0; i < argc; ++i)
35  cout << "argv[" << i << "] = " << argv[i]
36  << endl;
37 
38  MPI_Init(&argc, &argv);
39  MPI_Comm_size(MPI_COMM_WORLD, &numprocs);
40  MPI_Comm_rank(MPI_COMM_WORLD, &myid);
41  MPI_Get_processor_name(processor_name, &namelen);
42 
43  fprintf(stderr, "Process %d on %s\n",
44  myid, processor_name);
45 
46  n = 0;
47  while (!done)
48  {
49  if (myid == 0)
50  {
51  if (n == 0) n = 100;
52  else n = 0;
53 
54  startwtime = MPI_Wtime();
55  }
56  MPI_Bcast(&n, 1, MPI_INT, 0, MPI_COMM_WORLD);
57  if (n == 0)
58  done = 1;
59  else
60  {
61  h = 1.0 / (double) n;
62  sum = 0.0;
63  for (i = myid + 1; i <= n; i += numprocs)
64  {
65  x = h * ((double)i - 0.5);
66  sum += f(x);
67  }
68  mypi = h * sum;
69 
70  MPI_Reduce(&mypi, &pi, 1, MPI_DOUBLE, MPI_SUM, 0, MPI_COMM_WORLD);
71 
72  if (myid == 0)
73  {
74  printf("pi is approximately %.16f, Error is %.16f\n",
75  pi, fabs(pi - PI25DT));
76  endwtime = MPI_Wtime();
77  printf("wall clock time = %f\n",
78  endwtime - startwtime);
79  }
80  }
81  }
82  MPI_Finalize();
83  return 0;
84 }