Example 1: Compute pi


DPCE Version of PI Example

#include <dpce.h>
#include <stdio.h>
#include "assert.h"

#define SIZE 10000

shape [SIZE] span;

double fabs(double);

main ()
{
   double sum;                           /* Sum of areas */
   double width;
   double:span x;                     /* Midpoint of rectangle on x axis */

/* the pcoord function generates the sequence of 0...SIZE-1 */
/* in the statement below, the assignments occur in parallel are:
    [0]x = ( 0 + 0.5 ) * 1.0 / SIZE;
    [1]x = ( 1 + 0.5 ) * 1.0 / SIZE;
       ...
    [SIZE-1]x = ( SIZE-1 + 0.5 ) * 1.0 / SIZE;   */

    x = (pcoord (span,0)+0.5) * 1.0 / SIZE;

/* The sum of all the elements of (4.0/(1.0 + [0..SIZE-1]x * [0..SIZE-1]x) ) */
    sum = (+= (4.0/(1.0+x*x)));

   sum *= 1.0/SIZE;

   printf ("Estimation of pi is %14.12f\n", sum);

   assert (fabs (sum - 3.141592) < 0.00001);
}

Scalar Version of PI Example

To get an understand of what each operation actually does, it is helpful to look at code that one is familiar with. The below code is roughly the equivalent of the DPCE program above. The reason we say "roughly" is because this is merely a scalar equivalent while the program above performs its operations in parallel:

    ...
    int j;
    double sum;
    double width;
    double x[SIZE];
    for ( j=0; j < SIZE; j++ )
       x[j] = ((j + 0.5) * 1.0) * 1.0/SIZE;

    sum = 0;
    for ( j=0; j < SIZE; j++ )
       sum = sum + 4.0 / (1.0 + x[j] * x[j]);

    sum *= 1.0 / SIZE;
    printf( "Estimation of pi is %14.12f\n", sum );

    assert (fabs (sum - 3.141592) < 0.00001);


Options:

©1995 Pacific-Sierra Research Corporation. All rights reserved.

Send comments or suggestions to dpce2 at crescentbaysoftware.com.