FREE Subscription to Dr. Dobb’s Digest: Same Great Content, New Digital Edition
Site Archive (Complete)
C++
Email
Print
Reprint

add to:
Del.icio.us
Digg
Google
Furl
Slashdot
Y! MyWeb
Blink
April 01, 2004

CxC Parallel Programming

(Page 15 of 16)
April 04:

Listing 6: Complete CxC program.

// pi.cxc : calculates the number PI using numerical 
// integration of the function SUM( 4/ (1 + i*i) ) * width
//
//
////////////// controller and unit //////////////////////
define NUM_PPUS 1000
define CONST_A   0.5
controller ZeroToOne    // 1000 intervals between 0 and 1
 {
   double Interval; // result for each interval
   unit IntervalUnit[NUM_PPUS]; 
// 1000 PPU - one for each interval
 }
controller ComputePI    // Controller for result unit
 {
   unit PIUnit[1];  // Unit where PI value is computed
 }
//////////// topology /////////////////////////////////
topology IntervalLink       // link to each IntervalUnit to
 {                  // to PIUnit
  PIUnit[0] -> IntervalUnit[*];
 }
/////////// programs /////////////////////////////////
main PI
 {
   ////////////////////////////////////////////////////////////
   // ZeroToOne: compute value for sub-interval
   ////////////////////////////////////////////////////////////
   program ZeroToOne
    {
     const long NumOfIntervals = NUM_PPUS;
     double x;      // temporary variable
     // compute value for my part of the interval
     x = (id() + CONST_A) / NumOfIntervals;
       Interval = (4.0 / (1.0 + x * x));
     // wait until all ZeroToOne ppu have finished
     barrier; 
    }
   ////////////////////////////////////////////////////////////
   // ComputePI: distibute sub-intervals to ZeroToOne ppus
   //            gather sub-results and compute PI
   ////////////////////////////////////////////////////////////
   program ComputePI
    {
     const long NumOfIntervals = NUM_PPUS;
     // compute the sub-values in 1000 parallel processors barrier; 
     // retrieve the results from the ZeroToOne PPUs
     Val.fanin( Interval );
     println("PI Result: " , sum(Val)/NumOfIntervals );
    }
 }




    
    
      
    
    
Previous Page | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 Next Page
TOP 5 ARTICLES
No Top Articles.



MICROSITES
FEATURED TOPIC

ADDITIONAL TOPICS

INFO-LINK