The HPJava syntax for multiarrays uses double brackets to distinguish
from standard Java arrays (which of course use single
brackets). Here is a fragment that declares and creates a multiarray:
The syntax is a little bit verbose compared with standard Java, but we don't see this as a wholly bad thing: one can argue that there is some kind of virtue in having non-standard features stand out slightly from the rest of the code.
Here is a simple, sequential matrix multiplication written in HPJava,
with the input and output matrices implemented as multiarrays:
The first is that we access elements of a multiarray using a syntax very similar to the syntax for accessing an array element in ordinary Java. If the multiarray has rank greater than one1.2, then the subscripts are separated by commas. Double brackets aren't needed here. They would soon get tiresome, and they are unnecessary because ordinary type checking can always determine (e.g. from the declaration of the array) whether or not the thing being subscripted is in fact a multiarray.
Another thing to notice is that of course multiarrays can be passed as arguments to methods. They are effectively passed by reference, exactly as for ordinary Java arrays. The formal parameter list must declare the rank of the multiarray expected. Methods can also return multiarrays as their result values.
Finally, notice the form of the inquiry methods that allow one to determine the shape of a given multiarray. Instead of providing a length field, as for Java arrays, a multiarray supports an inquiry operation rng() that looks syntactically like a method. The argument must to be a constant expression in the range 0 to , where is the rank of the multiarray. This rng() operation doesn't return the integer extent of the array directly (as one might have expected). For reasons connected with the parallel origins of HPJava, rng() returns an object of type hpjava.lang.Range. One must apply the size() method to this object to get the extent itself1.3.
Figure 1.2 gives a complete HPJava program including
the matmul method. The source is available in the hpjdk
release as the file hpjdk/examples/PPHPJ/EgSequentialMatmul.hpj.
To compile the source one would execute the command: