Chestnut

A GPU Programming language for non-experts

Examples Chestnut programs

We show examples of complete Chestnut programs including all data declarations, parallel function definitions, array initializations and parallel foreach contexts interspersed with sequential code. Heat Flow and Game of Life, show initialization inside a parallel context and makes use of parallel functions. Mandelbrot, is an embarassily parallel application with a while loop inside the foreach context.

We show examples of Chestnut programs that do not use Chestnut's support for simultaneous GPU visualiztation and animation of the application, and versions with Chestnut's animation/visualization support.

COMING SOON: more Chestnut program examples.

Versions without Chestnut's support for GPU visualiztion

Chestnut Heat Flow

 // heat flow constants:
 Real c0 = 0.6 // % of point
 Real c1 = 0.1 // % of neighbors
 
 // parallel array (surface): 
 RealArray2d heat_data[500, 250] 
              = read("heat.data";
 
 // stencil array specifies heat 
 // source (1) and sink (2) pts
 // ex. 0 0 1 1 0
 //     0 0 0 0 0
 //     0 0 2 2 0
 IntArray2d h_stencil[500, 250] 
             = read("heat_stencil.data");
 
 ////////////////////////////////////
 // parallel function: update element 
 parallel Real heat_flow(Real2d p) {
   Real new_p = c0*p.center + c1*(p.west 
                + p.east + p.north + p.south);
   new_p = min(1.0, new_p);
    return max(0.0, new_p);
 }
 
 //////////////////////////////////////
 // main program control flow
 
 // run 10000 iternations of heat dispersion: 
 for (Int i=0; i < 10000; i++) {
 
   foreach pt in heat_data, s_val in h_stencil
     // update each pt of surface
     point = heat_flow(pt);
     // update source and sink values
     if(s_val == 1) { point = 1; }
     if(s_val == 2) { point = 2; }
   end
 }
 write(heat_data, "outfile.txt")

Chestnut Game of Life

// 1000x500 parallel grid (world)
IntArray2d life_data[1000, 500];  

/////////////////////////////////
// parallel function to update a grid element  
parallel Int game_of_life(Int2d e) {

  Int neighbor_count = e.northWest + e.north 
        + e.northEast + e.west  + e.east 
        + e.southWest + e.south + e.southEast;

  Int state = 1;  // start with live result
  if (e == 1) {   // if cell is alive
    if (neighbor_count <= 1) {
      state = 0; // dies from loneliness
    } else if (neighbor_count >= 4) {
      state = 0; // dies from overpopulation
    }
  } else { // if cell is dead
    if (neighbor_count != 3) {
      state = 0; // stays dead
    }
  }
  return state;
}
/////////////////////////////////
// main program control flow:

// parallel initialization:
// set 25% of elements to 1, rest to 0
foreach cell in life_data    
  cell = (random() < 0.25); 
end

// run 10000 rounds of GOL
for(Int i=0; i < 10000; i++) {  
  // update each cells value 
  foreach cell in life_data    
    cell = game_of_life(cell);
  end
}
write(life_data, "outfile.txt")

Chestnut Mandelbrot Fractal Generation

Real left = -2.5;
Real right = 1;
Real top = 1;
Real bottom = -1;
Int  N = 1000;

IntArray2d display[875, 500];

foreach pixel in display
  // scale coords to be in given window
  Real x0 = (pixel.x/display.width) 
              * (right - left) + left;
  Real y0 = (pixel.y/display.height) 
              * (top - bottom) + bottom;
  Real x = 0;
  Real y = 0;
  Int i = 0;

  while (((x*x + y*y) <= 4) && (i < N)) {
    Real xtemp = (x*x - y*y) + x0;
    y = 2*x*y + y0;
    x = xtemp;
    i = i + 1;
  }

  if (i == N)
    pixel = 0; // didn't diverge: in the set
  else
    pixel = i; // diverged in i iterations
end