One of the more difficult aspects of coding stencils or
other neighbourhood-based operations is working out what should happen at the
edge of the grid. Parallel array languages such as ZPL have expended
significant effort to provide mechanism to make coding edge effects easier.
PM handles edge effects by making use of optional data types
- containers that can hold either a value (e.g.: an integer) or null. Optional types have many uses,
including the representation of unbounded data structures in a pointerless language.
A number of operators are defined for optional types, most usefully |. x|y returns the value contained in x if that
value is not null. Otherwise y is
returned.
Binary communicating operators (x@[neighbourhood] or x@{neighbourhood}) always
return optionally-typed values, with off-edge cells set to null. Combined with the | operator, this makes dealing with edge-effects
straightforward.
The following code fragment demonstrates how PM handles edge
effects in practice. It solves the 2D steady-state heat transfer problem for a
rectangular box whose four walls are maintained at temperatures boundary1..boundary4. A simple solution to this problem involves repeatedly
replacing the temperature in each grid cell with the average of its four
immediate neighbours until no further change occurs (Jacobi Iteration).
for cell in [1..N,1..M] do
temp := 0.0
repeat
newtemp := (
temp@[-1, 0]|boundary1
+temp@[ 1, 0]|boundary2
+temp@[ 0,-1]|boundary3
+temp@[ 0, 1]|boundary4
)/4.0
diff := abs(newtemp-temp)
temp = newtemp
until max::diff < tolerance
endfor
Comments
Post a Comment