The example of Figure 2.8 only uses language features introduced in the preceding sections. It introduces two new library functions.
The problem is well-known to scientific programmers: solution of the two-dimensional Laplace equation with Dirichlet boundary conditions by the iterative Jacobi relaxation method. The boundary conditions of the equation are fixed by setting edge elements of an array. These elements don't change during the computation. The solution for the interior region is obtained by iteration from some arbitrary starting values (zero, here). A single iteration involves replacing each interior element by the average of its neighbouring values. A similar update was already discussed in section 2.4. Here we put it in the context of a working program.
The initialization is done with a pair of nested overall constructs.
Inside, a conditional tests if we are on an edge of the array. If
so, the element values are set to some chosen expression--the
boundary function. Otherwise we zero an interior element.
As explained at the end of the last section we, apply the operator
` to distributed indices to get the global loop index.
Notice that we can freely use ordinary Java constructs like the if construct inside an overall construct. HPJava distributed controls construct are true, compositional control constructs. This is one of the ways in which overall differs from the HPF forall construct, which has restrictive rules about what kind of statement can appear in its body.
The body of the main loop contains shift() operations and nested overall loops. The body of the inner loop is slightly more complicated than the version in figure 2.6 because it saves changes to the main array in a separate array r.
Note the declaration of the float temporary newA inside the body of the parallel loop. This is perfectly good practice. The temporary is just an ordinary scalar Java variable--the HPJava translator doesn't treat it specially. Also note the call to a Java library function Math.abs() inside the loop. As we have emphasized, any normal Java operation is allowed inside an HPJava distributed control construct.
The main loop terminates when the largest change in any element is smaller than some predefined value EPS. The collective library function Adlib.maxval() finds the largest element of distributed array, and broadcasts its value to all processes that call the function. (We didn't initialize the edges of the array r. But, as for ordinary Java arrays, elements of multiarrays and distributed arrays get a default value: 0 for numbers.)
Finally a collective library function Adlib.aprintf() prints a formatted version of the array on the standard output stream. You can find details of how the output is formatted in section 6.4.
There is a working version of this example in the hpjdk release
under the file hpjdk/examples/PPHPJ/EgJacobiLaplace.hpj.
Compile it by: