1 31 32 import EDU.oswego.cs.dl.util.concurrent.*; 33 34 public class Heat { 35 36 static int nx; 38 static int ny; 39 static int nt; 40 static int leafmaxcol; 41 42 static double[][] newm; 44 45 static double[][] oldm; 47 48 49 public static void main(String [] args) { 50 int procs = 1; 51 int benchmark = 0; 52 53 try { 54 procs = Integer.parseInt(args[0]); 55 benchmark = Integer.parseInt(args[1]); 56 } 57 catch (Exception e) { 58 System.out.println("Usage: java Heat <threads> <0-4>"); 59 return; 60 } 61 62 switch (benchmark) { 63 case 0: 64 nx = 4096; ny = 512; nt = 100; leafmaxcol = 10; 65 break; 66 case 1: 67 nx = 512; ny = 512; nt = 1; leafmaxcol = 10; 68 break; 69 case 2: 70 nx = 4096; ny = 512; nt = 40; leafmaxcol = 10; 71 break; 72 case 3: 73 nx = 4096; ny = 1024; nt = 100; leafmaxcol = 1; 74 break; 75 case 4: 76 nx = 1024; ny = 512; nt = 100; leafmaxcol = 16; 77 break; 78 default: 79 System.out.println("Usage: java Heat <threads> <0-4>"); 80 return; 81 } 82 83 System.out.print("Parameters: "); 84 System.out.print(" granularity = " + leafmaxcol); 85 System.out.print(" rows = " + nx); 86 System.out.print(" columns = " + ny); 87 System.out.println(" steps = " + nt); 88 89 90 oldm = new double[nx][ny]; 91 newm = new double[nx][ny]; 92 93 try { 94 95 FJTaskRunnerGroup g = new FJTaskRunnerGroup(procs); 96 97 FJTask main = new FJTask() { 98 public void run() { 99 for (int timestep = 0; timestep <= nt; timestep++) { 100 FJTask.invoke(new Compute(0, nx, timestep)); 101 } 102 } 103 }; 104 105 g.invoke(main); 106 107 g.stats(); 108 109 } 110 catch (InterruptedException ex) { return; } 111 112 113 } 114 115 116 static final double xu = 0.0; 118 static final double xo = 1.570796326794896558; 119 static final double yu = 0.0; 120 static final double yo = 1.570796326794896558; 121 static final double tu = 0.0; 122 static final double to = 0.0000001; 123 124 static final double dx = (xo - xu) / (nx - 1); 125 static final double dy = (yo - yu) / (ny - 1); 126 static final double dt = (to - tu) / nt; 127 static final double dtdxsq = dt / (dx * dx); 128 static final double dtdysq = dt / (dy * dy); 129 130 131 static final double f(double x, double y) { 133 return Math.sin(x) * Math.sin(y); 134 } 135 136 138 static final double randa(double x, double t) { 139 return 0.0; 140 } 141 static final double randb(double x, double t) { 142 return Math.exp(-2*t) * Math.sin(x); 143 } 144 static final double randc(double y, double t) { 145 return 0.0; 146 } 147 static final double randd(double y, double t) { 148 return Math.exp(-2*t) * Math.sin(y); 149 } 150 static final double solu(double x, double y, double t) { 151 return Math.exp(-2*t) * Math.sin(x) * Math.sin(y); 152 } 153 154 155 156 157 static final class Compute extends FJTask { 158 159 final int lb; 160 final int ub; 161 final int time; 162 163 Compute(int lowerBound, int upperBound, int timestep) { 164 lb = lowerBound; 165 ub = upperBound; 166 time = timestep; 167 } 168 169 public void run() { 170 if (ub - lb > leafmaxcol) { 171 int mid = (lb + ub) / 2; 172 coInvoke(new Compute(lb, mid, time), 173 new Compute(mid, ub, time)); 174 } 175 else if (time == 0) init(); 177 else if (time %2 != 0) compstripe(newm, oldm); 179 else 180 compstripe(oldm, newm); 181 } 182 183 184 185 final void compstripe(double[][] newMat, double[][] oldMat) { 186 187 189 final int llb = (lb == 0) ? 1 : lb; 190 final int lub = (ub == nx) ? nx - 1 : ub; 191 192 double[] west; 193 double[] row = oldMat[llb-1]; 194 double[] east = oldMat[llb]; 195 196 for (int a = llb; a < lub; a++) { 197 198 west = row; 199 row = east; 200 east = oldMat[a+1]; 201 202 double prev; 203 double cell = row[0]; 204 double next = row[1]; 205 206 double[] nv = newMat[a]; 207 208 for (int b = 1; b < ny-1; b++) { 209 210 prev = cell; 211 cell = next; 212 double twoc = 2 * cell; 213 next = row[b+1]; 214 215 nv[b] = cell 216 + dtdysq * (prev - twoc + next) 217 + dtdxsq * (east[b] - twoc + west[b]); 218 219 } 220 } 221 222 edges(newMat, llb, lub, tu + time * dt); 223 } 224 225 226 final void origcompstripe(double[][] newMat, double[][] oldMat) { 228 229 final int llb = (lb == 0) ? 1 : lb; 230 final int lub = (ub == nx) ? nx - 1 : ub; 231 232 for (int a = llb; a < lub; a++) { 233 for (int b = 1; b < ny-1; b++) { 234 double cell = oldMat[a][b]; 235 double twoc = 2 * cell; 236 newMat[a][b] = cell 237 + dtdxsq * (oldMat[a+1][b] - twoc + oldMat[a-1][b]) 238 + dtdysq * (oldMat[a][b+1] - twoc + oldMat[a][b-1]); 239 240 } 241 } 242 243 edges(newMat, llb, lub, tu + time * dt); 244 } 245 246 247 248 final void init() { 249 final int llb = (lb == 0) ? 1 : lb; 250 final int lub = (ub == nx) ? nx - 1 : ub; 251 252 for (int a = llb; a < lub; a++) { 253 double[] ov = oldm[a]; 254 double x = xu + a * dx; 255 double y = yu; 256 for (int b = 1; b < ny-1; b++) { 257 y += dy; 258 ov[b] = f(x, y); 259 } 260 } 261 262 edges(oldm, llb, lub, 0); 263 264 } 265 266 267 final void edges(double [][] m, int llb, int lub, double t) { 268 269 for (int a = llb; a < lub; a++) { 270 double[] v = m[a]; 271 double x = xu + a * dx; 272 v[0] = randa(x, t); 273 v[ny-1] = randb(x, t); 274 } 275 276 if (lb == 0) { 277 double[] v = m[0]; 278 double y = yu; 279 for (int b = 0; b < ny; b++) { 280 y += dy; 281 v[b] = randc(y, t); 282 } 283 } 284 285 if (ub == nx) { 286 double[] v = m[nx - 1]; 287 double y = yu; 288 for (int b = 0; b < ny; b++) { 289 y += dy; 290 v[b] = randd(y, t); 291 } 292 } 293 } 294 } 295 } 296
| Popular Tags
|