slide 17 of 40


Compute Pi Using AFAPI Functions

#include 
#include 
#include "afapi.h"

main(int argc,
char **argv)
{
  register double width, sum;
  register int intervals, i;

  /* check-in with AFAPI */
  if (p_init()) exit(1);

  /* get the number of intervals */
  intervals = atoi(argv[1]);
  width = 1.0 / intervals;

  /* do the local computations */
  sum = 0;
  for (i=IPROC; i<intervals; i+=NPROC) {
    register double x = (i + 0.5) * width;
    sum += 4.0 / (1.0 + x * x);
  }

  /* sum across the local results & scale by width */
  sum = p_reduceAdd64f(sum) * width;

  /* have only the console PE print the result */
  if (IPROC == CPROC) {
    printf("Estimation of pi is %14.12lf\n", sum);
  }

  /* check-out */
  p_exit();
  exit(0);
}