?? oberdorf.txt
字號:
CxC & Parallel Programming
by Matt Oberdorfer and Jim Gutowski
Listing 1:
//// My first CxC program hello.cxc controller and unit declaration
controller ArrayController // create processor controller
{ // create parallel processors
unit ParallelProcessor[30];
}
//// don't need a topology since there is no communication
//// between processors taking place
//// program implementations
main hello(10) // execute 10 times
{
program ArrayController // program for all processors
{ // of declared controller
println("hello parallel world!");
}
}
Listing 2:
define NUM_PPUS 1000
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 computer
}
Listing 3:
topology IntervalLink // link from PIUnit
{ // to each IntervalUnit
PIUnit[0] -> IntervalUnit[*];
}
Listing 4:
program ZeroToOne
{
double x; // temporary variable
// compute value for part of the interval
x = ( id() + 0.5) / NUM_PPUS;
Interval = (4.0 / (1.0 + x * x));
barrier; // synchronize with result unit
}
Listing 5:
program ComputePI
{
double IntervalLink:Val;
// buffer to gather computed values
barrier; // synchronize with compute units
// retrieve the results from the ZeroToOne PPUs
Val.fanin( Interval );
println("PI Result: " , sum(Val)/ NUM_PPUS );
}
Listing 6:
//
// 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 computer
}
//////////// 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 );
}
}
3
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -