Advertisement
Guest User

Untitled

a guest
Jan 30th, 2018
306
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Chapel 3.17 KB | None | 0 0
  1. class Data{
  2.   //the slice of the domain we are iterating over
  3.   const D : domain(2);
  4.   // original D expanded by 1 to hold neighbouring rows / halo regions
  5.   const bigD : domain(2) = D.expand(1, 0);
  6.   var temps: [bigD] real;
  7.   var cond: [D] real;
  8.   var k : int;
  9. }
  10.  
  11. proc do_compute() {
  12.   /* your main function */
  13.  
  14.   var r          : results;
  15.   var t          : Timer;
  16.   var b       = new Barrier(numLocales, reusable=true);
  17.   var diff       : [tempsD] real;
  18.   var indexes = calculate_indexes();
  19.   var niter      : int;
  20.   var localeData : [LocaleSpace] Data;
  21.  
  22.  
  23.   //setup data across locales
  24.   for l in LocaleSpace{
  25.     on Locales[l]{
  26.       const D : domain(2) = {indexes[l].. (indexes[l+1])-1, 1..M};
  27.       localeData[l] = new Data(D);
  28.       const bigD = localeData[l].bigD;
  29.       if(l == 0){
  30.         localeData[l].temps[{indexes[l]..indexes[l+1], 1..M}] = tinit[{indexes[l]..indexes[l+1], 1..M}];
  31.         localeData[l].temps[0,..]  = tinit[1,..];
  32.       }
  33.       else if (l == (numLocales-1)){
  34.         var x : int = bigD.last(1);
  35.         localeData[l].temps[{indexes[l]-1 ..(indexes[l+1])-1, 1..M}] = tinit[{indexes[l]-1.. (indexes[l+1])-1, 1..M}];
  36.         localeData[l].temps[x,..]            = tinit[x-1,..];
  37.       }
  38.       else{
  39.         localeData[l].temps[bigD] = tinit[bigD];
  40.       }
  41.       localeData[l].cond = tcond[D];
  42.     }
  43.   }
  44.   t.start();
  45.   coforall l in LocaleSpace{
  46.     on Locales[l]{
  47.       const D : domain(2)    = localeData[l].D;
  48.       const bigD : domain(2) = localeData[l].bigD;
  49.       const firstRow         = {(indexes[l+1]-1)..(indexes[l+1]-1), 1..M};
  50.       const lastRow          = {(indexes[l])..(indexes[l]) , 1..M};
  51.       const cond             = localeData[l].cond;
  52.       var temps_new          : [D] real;
  53.       var k                  : int = 0;
  54.       var maxdiff            : real;
  55.       do{
  56.         const temps = localeData[l].temps;
  57.         forall (i,j) in D{
  58.           //offset for accessing neighbour in cylindrical surface
  59.           const pos : int = if (j == M) then -(M-1) else 1;
  60.           const neg : int = if (j == 1) then  (M-1) else -1;
  61.           temps_new[i,j] = (temps[i,j] * cond[i,j])
  62.             //direct neighbours
  63.             + (((1 - cond[i,j]) * direct) *
  64.                ((temps[i+1,j] +
  65.                  temps[i-1,j] +
  66.                  temps[i,j+pos] +
  67.                  temps[i,j+neg]) / 4) )
  68.             //diagonal neighbours
  69.             + (((1 - cond[i,j]) * diag) *
  70.                ((temps[i+1, j+pos] +
  71.                  temps[i+1, j+neg] +
  72.                  temps[i-1, j+pos] +
  73.                  temps[i-1, j+neg] ) / 4) );
  74.  
  75.           // update current max/min values
  76.           diff[i,j] =  abs(temps_new[i,j] - temps[i,j]);
  77.         }
  78.         localeData[l].temps[D] = temps_new;
  79.         // exchange neighbour rows
  80.         if (l != 0) then
  81.           localeData[l-1].temps[lastRow] = temps_new[lastRow];
  82.         if(l != numLocales-1) then
  83.           localeData[l+1].temps[firstRow] = temps_new[firstRow];
  84.  
  85.         on diff do maxdiff = max reduce diff;
  86.  
  87.         k += 1;
  88.         b.barrier(); //wait for all locales to exchange rows
  89.       }while(k<I && maxdiff>E);
  90.       localeData[l].k = k;
  91.     }
  92.   }
  93.   t.stop();
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement