single_machine.chpl 1.19 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
config const n = 8;//Size of the domain squired
config const epsilon = 1.0e-10;//Stop condition in amount of change
config var iterations = 1000;//Stop condition in number of iterations

//A n+2 by n+2 domain.
const Grid = {0..n+1, 0..n+1};

//A n by n domain that represents the interior of 'Grid'
const Interior = {1..n, 1..n};

var A, T : [Grid] real;//Zero initialized as default

A[..,0] = -273.15;   //Left column
A[..,n+1] = -273.15; //Right column
A[n+1,..] = -273.15; //Bottom row
A[0,..] = 40.0;      //Top row

do{

  //Since all iterations are independent, we can use 'forall', which allows
  //the Chapel runtime system to calculate the iterations in parallel
  forall (i,j) in Interior do//Iterate over all non-border cells
  {
    //Assign each cell in 'T' the mean of its neighboring cells in 'A'
    T[i,j] = (A[i,j] + A[i-1,j] + A[i+1,j] + A[i,j-1] + A[i,j+1]) / 5;
  }

  //Delta is the total amount of change done in this iteration
  const delta = + reduce abs(A[Interior] - T[Interior]);

  //Copy back the non-border cells
  A[Interior] = T[Interior];

  //When 'delta' is smaller than 'epsilon' the calculation has converged
  iterations -= 1;
} while (delta > epsilon && iterations > 0);