File: Heat.java

package info (click to toggle)
concurrent-dfsg 1.3.4-6
  • links: PTS
  • area: main
  • in suites: bookworm
  • size: 976 kB
  • sloc: java: 10,704; xml: 49; makefile: 12
file content (295 lines) | stat: -rw-r--r-- 7,853 bytes parent folder | download | duplicates (4)
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
/*
  Converted from heat.cilk, which is
    Copyright (c) 1996 Massachusetts Institute of Technology
  with the following notice:

   * Permission is hereby granted, free of charge, to any person obtaining
   * a copy of this software and associated documentation files (the
   * "Software"), to use, copy, modify, and distribute the Software without
   * restriction, provided the Software, including any modified copies made
   * under this license, is not distributed for a fee, subject to
   * the following conditions:
   * 
   * The above copyright notice and this permission notice shall be
   * included in all copies or substantial portions of the Software.
   * 
   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
   * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
   * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
   * IN NO EVENT SHALL THE MASSACHUSETTS INSTITUTE OF TECHNOLOGY BE LIABLE
   * FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
   * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
   * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
   * 
   * Except as contained in this notice, the name of the Massachusetts
   * Institute of Technology shall not be used in advertising or otherwise
   * to promote the sale, use or other dealings in this Software without
   * prior written authorization from the Massachusetts Institute of
   * Technology.
   *  
*/

import EDU.oswego.cs.dl.util.concurrent.*;

public class Heat {

  // Parameters
  static int nx;
  static int ny;
  static int nt;
  static int leafmaxcol;

  // the matrix representing the cells
  static double[][] newm;

  // alternating workspace matrix
  static double[][] oldm;


  public static void main(String[] args) {
    int procs = 1;
    int benchmark = 0;

    try {
      procs = Integer.parseInt(args[0]);
      benchmark = Integer.parseInt(args[1]);
    }
    catch (Exception e) {
      System.out.println("Usage: java Heat <threads> <0-4>");
      return;
    }

    switch (benchmark) {
    case 0:      /* cilk demo defaults */
      nx = 4096; ny = 512; nt = 100; leafmaxcol = 10; 
      break;
    case 1:      /* cilk short benchmark options */
      nx = 512; ny = 512; nt = 1; leafmaxcol = 10;
      break;
    case 2:      /* cilk standard benchmark options */
      nx = 4096; ny = 512; nt = 40; leafmaxcol = 10;
      break;
    case 3:      /* cilk long benchmark options */
      nx = 4096; ny = 1024; nt = 100; leafmaxcol = 1;
      break;
    case 4:      /* hood demo faults */
      nx = 1024; ny = 512; nt = 100; leafmaxcol = 16;
      break;
    default:
      System.out.println("Usage: java Heat <threads> <0-4>");
      return;
    }

    System.out.print("Parameters: ");
    System.out.print(" granularity = " + leafmaxcol);
    System.out.print(" rows = " + nx);
    System.out.print(" columns = " + ny);
    System.out.println(" steps = " + nt);

    
    oldm = new double[nx][ny];
    newm = new double[nx][ny];

    try {  
    
      FJTaskRunnerGroup g = new FJTaskRunnerGroup(procs);
      
      FJTask main = new FJTask() {
        public void run() {
          for (int timestep = 0; timestep <= nt; timestep++) {
            FJTask.invoke(new Compute(0, nx, timestep));
          }
        }
      };
      
      g.invoke(main);

      g.stats();

    }
    catch (InterruptedException ex) { return; }


  }


  // constants (at least for this demo)
  static final double xu = 0.0;
  static final double xo = 1.570796326794896558;
  static final double yu = 0.0;
  static final double yo = 1.570796326794896558;
  static final double tu = 0.0;
  static final double to = 0.0000001;

  static final double dx = (xo - xu) / (nx - 1);
  static final double dy = (yo - yu) / (ny - 1);
  static final double dt = (to - tu) / nt;	
  static final double dtdxsq = dt / (dx * dx);
  static final double dtdysq = dt / (dy * dy);


  // the function being applied across the cells
  static final double f(double x, double y) { 
    return Math.sin(x) * Math.sin(y); 
  }

  // random starting values

  static final double randa(double x, double t) { 
    return 0.0; 
  }
  static final double randb(double x, double t) { 
    return Math.exp(-2*t) * Math.sin(x); 
  }
  static final double randc(double y, double t) { 
    return 0.0; 
  }
  static final double randd(double y, double t) { 
    return Math.exp(-2*t) * Math.sin(y); 
  }
  static final double solu(double x, double y, double t) { 
    return Math.exp(-2*t) * Math.sin(x) * Math.sin(y); 
  }




  static final class Compute extends FJTask {

    final int lb;
    final int ub;
    final int time;

    Compute(int lowerBound, int upperBound, int timestep) {
      lb = lowerBound;
      ub = upperBound;
      time = timestep;
    }
     
    public void run() {
      if (ub - lb > leafmaxcol) {
        int mid = (lb + ub) / 2;
        coInvoke(new Compute(lb, mid, time),
                 new Compute(mid, ub, time));
      }
      else if (time == 0)     // if first pass, initialize cells
        init();
      else if (time %2 != 0)  // alternate new/old
        compstripe(newm, oldm);
      else
        compstripe(oldm, newm);
    }


    /** Update all cells **/
    final void compstripe(double[][] newMat, double[][] oldMat) {

      // manually mangled to reduce array indexing

      final int llb = (lb == 0)  ? 1 : lb;
      final int lub = (ub == nx) ? nx - 1 : ub;

      double[] west;
      double[] row = oldMat[llb-1];
      double[] east = oldMat[llb];

      for (int a = llb; a < lub; a++) {

        west = row;
        row =  east;
        east = oldMat[a+1];

        double prev;
        double cell = row[0];
        double next = row[1];

        double[] nv = newMat[a];

        for (int b = 1; b < ny-1; b++) {

          prev = cell;
          cell = next;
          double twoc = 2 * cell;
          next = row[b+1];

          nv[b] = cell
            + dtdysq * (prev    - twoc + next)
            + dtdxsq * (east[b] - twoc + west[b]);

        }
      }

      edges(newMat, llb, lub,  tu + time * dt);
    }


    // the original version from cilk
    final void origcompstripe(double[][] newMat, double[][] oldMat) {
      
      final int llb = (lb == 0)  ? 1 : lb;
      final int lub = (ub == nx) ? nx - 1 : ub;

      for (int a = llb; a < lub; a++) {
        for (int b = 1; b < ny-1; b++) {
          double cell = oldMat[a][b];
          double twoc = 2 * cell;
          newMat[a][b] = cell
            + dtdxsq * (oldMat[a+1][b] - twoc + oldMat[a-1][b])
            + dtdysq * (oldMat[a][b+1] - twoc + oldMat[a][b-1]);

        }
      }

      edges(newMat, llb, lub,  tu + time * dt);
    }


    /** Initialize all cells **/
    final void init() {
      final int llb = (lb == 0) ? 1 : lb;
      final int lub = (ub == nx) ? nx - 1 : ub;

      for (int a = llb; a < lub; a++) {	/* inner nodes */
        double[] ov = oldm[a];
        double x = xu + a * dx;
        double y = yu;
        for (int b = 1; b < ny-1; b++) {
          y += dy;
          ov[b] = f(x, y);
        }
      }

      edges(oldm, llb, lub, 0);

    }

    /** Fill in edges with boundary values **/
    final void edges(double [][] m, int llb, int lub, double t) {

      for (int a = llb; a < lub; a++) {
        double[] v = m[a];
        double x = xu + a * dx;
        v[0] = randa(x, t);
        v[ny-1] = randb(x, t);
      }

      if (lb == 0) {
        double[] v = m[0];
        double y = yu;
        for (int b = 0; b < ny; b++) {
          y += dy;
          v[b] = randc(y, t);
        }
      }

      if (ub == nx) {
        double[] v = m[nx - 1]; 
        double y = yu;
        for (int b = 0; b < ny; b++) {
          y += dy;
          v[b] = randd(y, t);
        }
      }
    }
  }
}