End-user products => VAST/DPC => DPC example

DPC Example
Here's a little DPC program that calculates the value of pi. Note in particular the "shape" declaration (in this case no distribution is specified by the programmer, so the compiler chooses one), the array assignment of x, and the summation operation.

#include 
#include 
#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 */

    x = (pcoord (span,0)+0.5) * 1.0 / SIZE;
    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);
}

If you're wondering what the code is doing... for 1 processor:

  ...
 dpce_descriptor_t span;
 int vdpc_span = 0;
 double fabs(double );
 main( )
 {
    int j1, *j2, j3, j4;
    float r1, r2;
    double d1;
    double sum;
    double width;
    double x[10000];
    if ( !vdpc_span )
    {
          vdpc_init_desc( &span, 3,1,10000,10000,1 );
          vdpc_span = 1;
    }
    r1 = 1.F / 10000;
    r2 = 0.;
    for ( j1=0; j1 < 10000; j1++ )
    {
          x[j1] = ((r2 + 0.5) * 1.0) * r1;
          r2 = r2 + 1.;
    }
    d1 = 0;
       for ( j1=0; j1 <= 9999; j1 += 1 )
       d1 = d1 + 4.0 / (1.0 + x[j1] * x[j1]);
    sum = d1;
    sum *= 1.0 / 10000;
    printf( "Estimation of pi is %14.12f\n", sum );
    fabs( sum - 3.141592 ) < 0.00001 ? (void )0 : _assert( 
    "fabs (sum - 3.141592) < 0.00001", "pi.dpc",25 );
 }

Home Contact Legal

Copyright 2003, 2005 Crescent Bay Software Corp.