From 3ab0de831f430ff83163a2ed4132672e32123a36 Mon Sep 17 00:00:00 2001 From: Thomas Van Doren Date: Fri, 10 Apr 2015 17:11:32 -0700 Subject: [PATCH] Update Chapel samples. --- samples/Chapel/distributions.chpl | 90 ++++++++++++++++++++++++++++++- samples/Chapel/lulesh.chpl | 59 ++++++++++---------- 2 files changed, 120 insertions(+), 29 deletions(-) diff --git a/samples/Chapel/distributions.chpl b/samples/Chapel/distributions.chpl index a3e5f81d..31040c92 100644 --- a/samples/Chapel/distributions.chpl +++ b/samples/Chapel/distributions.chpl @@ -58,6 +58,34 @@ var BA: [BlockSpace] int; forall ba in BA do ba = here.id; +// +// The 'hasSingleLocalSubdomain' method on arrays will return true if the +// index set for a locale can be represented by a single domain. +// +if !BA.hasSingleLocalSubdomain() then + halt("For a Block distribution, the index set per locale should be \ + represented by a single domain"); + +// +// If the distribution's subdomains can be represented as single subdomain, +// we can use the 'localSubdomain' method to get the index set for the +// current locale. +// +// Below, we'll use the index set to confirm that the array elements have the +// correct locale id. +// + +for L in Locales { + on L { + const indices = BA.localSubdomain(); + for i in indices { + if BA[i] != L.id then + halt("Error: incorrect locale id"); + } + } +} + + // // Output the Block-distributed array to visually see how the elements // are partitioned across the locales. @@ -104,6 +132,14 @@ writeln("Block Array Index Map"); writeln(BA2); writeln(); +// +// We can use the 'targetLocales' method available on an array to get the +// locales array used as targets. +// +for (L, ML) in zip(BA2.targetLocales(), MyLocales) do + if L != ML then + halt("Error: BA2.targetLocales() should equal MyLocales"); + // @@ -126,6 +162,18 @@ writeln("Cyclic Array Index Map"); writeln(CA); writeln(); +// +// The domain returned by 'localSubdomain' need not be a dense block, as is +// the case for the Cyclic Distribution. +// +on Locales[0] { + const indices = CA.localSubdomain(); + for i in indices { + if CA[i] != 0 then + halt("Error: Cyclic array values on Locale 0 should be zero"); + } +} + // // Next, we'll declare a Block-Cyclic distribution. These @@ -134,7 +182,7 @@ writeln(); // of indices. Thus, the BlockCyclic distribution is parameterized // by a starting index (as with Cyclic) and a block size (per // dimension) specifying how large the chunks to be dealt out are. -// +// const BlkCycSpace = Space dmapped BlockCyclic(startIdx=Space.low, blocksize=(2, 3)); var BCA: [BlkCycSpace] int; @@ -146,6 +194,46 @@ writeln("Block-Cyclic Array Index Map"); writeln(BCA); writeln(); +// +// A locale's index set for a Block-Cyclic distribution cannot be represented +// by a single subdomain. +// +if BCA.hasSingleLocalSubdomain() then + halt("A Block-Cyclic index set cannot be represented by a single subdomain"); + +// +// If the local index set cannot be represented by a single subdomain, +// we can use the 'localSubdomains' iterator to yield a number of domains +// that represent the whole index set. +// +// Let's write a function that will use 'localSubdomains' to verify the +// correctness of the array values. +// + +proc verifyID(Data: []) { + for L in Locales { + on L { + for indices in Data.localSubdomains() { + for i in indices { + if Data[i] != L.id then + halt("Error: incorrect locale id"); + } + } + } + } +} +verifyID(BCA); + +// +// The 'localSubdomains' iterator is also available on distributions that +// can represent a locale's index set with a single domain. This allows us to +// write more general code that will work for all distributions. +// +// This means that we can call the 'verifyID' function on any array, like the +// 'BA' array from earlier. +// +verifyID(BA); + // // The ReplicatedDist distribution is different: each of the diff --git a/samples/Chapel/lulesh.chpl b/samples/Chapel/lulesh.chpl index d6480d26..78dab00d 100644 --- a/samples/Chapel/lulesh.chpl +++ b/samples/Chapel/lulesh.chpl @@ -292,14 +292,14 @@ proc main() { LagrangeLeapFrog(); if debug { - // deprint("[[ Forces ]]", fx, fy, fz); + deprintatomic("[[ Forces ]]", fx, fy, fz); deprint("[[ Positions ]]", x, y, z); deprint("[[ p, e, q ]]", p, e, q); } if showProgress then - writeln("time = ", format("%e", time), ", dt=", format("%e", deltatime), - if doTiming then ", elapsed = " + (getCurrentTime()-iterTime) - else ""); + writef("time = %er, dt=%er, %s", time, deltatime, + if doTiming then ", elapsed = " + (getCurrentTime()-iterTime) +"\n" + else "\n"); } if (cycle == maxcycles) { writeln("Stopped early due to reaching maxnumsteps"); @@ -314,12 +314,10 @@ proc main() { if printCoords { var outfile = open("coords.out", iomode.cw); var writer = outfile.writer(); - var fmtstr = if debug then "%1.9e" else "%1.4e"; - for i in Nodes { - writer.writeln(format(fmtstr, x[i]), " ", - format(fmtstr, y[i]), " ", - format(fmtstr, z[i])); - } + var fmtstr = if debug then "%1.9re %1.9er %1.9er\n" + else "%1.4er %1.4er %1.4er\n"; + for i in Nodes do + writer.writef(fmtstr, x[i], y[i], z[i]); writer.close(); outfile.close(); } @@ -479,7 +477,7 @@ inline proc localizeNeighborNodes(eli: index(Elems), y: [] real, ref y_local: 8*real, z: [] real, ref z_local: 8*real) { - for param i in 1..nodesPerElem { + for i in 1..nodesPerElem { const noi = elemToNode[eli][i]; x_local[i] = x[noi]; @@ -670,7 +668,7 @@ proc SumElemStressesToNodeForces(b_x: 8*real, b_y: 8*real, b_z: 8*real, ref fx: 8*real, ref fy: 8*real, ref fz: 8*real) { - for param i in 1..8 { + for i in 1..8 { fx[i] = -(stress_xx * b_x[i]); fy[i] = -(stress_yy * b_y[i]); fz[i] = -(stress_zz * b_z[i]); @@ -725,17 +723,17 @@ inline proc CalcElemFBHourglassForce(xd: 8*real, yd: 8*real, zd: 8*real, var hx, hy, hz: 4*real; // reduction - for param i in 1..4 { - for param j in 1..8 { + for i in 1..4 { + for j in 1..8 { hx[i] += hourgam[j][i] * xd[j]; hy[i] += hourgam[j][i] * yd[j]; hz[i] += hourgam[j][i] * zd[j]; } } - for param i in 1..8 { + for i in 1..8 { var shx, shy, shz: real; - for param j in 1..4 { + for j in 1..4 { shx += hourgam[i][j] * hx[j]; shy += hourgam[i][j] * hy[j]; shz += hourgam[i][j] * hz[j]; @@ -1088,16 +1086,16 @@ proc CalcFBHourglassForceForElems(determ, x8n, y8n, z8n, dvdx, dvdy, dvdz) { /* TODO: Can we enable this local block? */ // local { - for param i in 1..4 { + for i in 1..4 { var hourmodx, hourmody, hourmodz: real; // reduction - for param j in 1..8 { + for j in 1..8 { hourmodx += x8n[eli][j] * gammaCoef[i][j]; hourmody += y8n[eli][j] * gammaCoef[i][j]; hourmodz += z8n[eli][j] * gammaCoef[i][j]; } - for param j in 1..8 { + for j in 1..8 { hourgam[j][i] = gammaCoef[i][j] - volinv * (dvdx[eli][j] * hourmodx + dvdy[eli][j] * hourmody + @@ -1221,7 +1219,7 @@ proc CalcKinematicsForElems(dxx, dyy, dzz, const dt: real) { arealg[k] = CalcElemCharacteristicLength(x_local, y_local, z_local, volume); - for param i in 1..8 { + for i in 1..8 { x_local[i] -= dt2 * xd_local[i]; y_local[i] -= dt2 * yd_local[i]; z_local[i] -= dt2 * zd_local[i]; @@ -1669,7 +1667,7 @@ proc CalcSoundSpeedForElems(vnewc, rho0:real, enewc, pnewc, pbvc, bvc) { iter elemToNodes(elem) { - for param i in 1..nodesPerElem do + for i in 1..nodesPerElem do yield elemToNode[elem][i]; } @@ -1679,14 +1677,19 @@ iter elemToNodesTuple(e) { } -proc deprint(title:string, x:[?D] real, y:[D]real, z:[D]real) { +proc deprint(title:string, x:[?D] real, y:[D] real, z:[D] real) { writeln(title); - for i in D { - writeln(format("%3d", i), ": ", - format("%3.4e", x[i]), " ", - format("%3.4e", y[i]), " ", - format("%3.4e", z[i])); - } + for i in D do + writef("%3i: %3.4er %3.4er %3.4er\n", + if use3DRepresentation then idx3DTo1D(i, D.dim(1).size) else i, + x[i], y[i], z[i]); } +proc deprintatomic(title:string, x:[?D] atomic real, y:[] atomic real, z:[] atomic real) { + writeln(title); + for i in D do + writef("%3i: %3.4er %3.4er %3.4er\n", + if use3DRepresentation then idx3DTo1D(i, D.dim(1).size) else i, + x[i].peek(), y[i].peek(), z[i].peek()); +}