next up previous contents index
Next: Some Semantic Issues Up: Multiarrays Previous: Multidimensional Arrays and Multiarrays   Contents   Index

Syntax

The HPJava syntax for multiarrays uses double brackets to distinguish type signatures from standard Java arrays (which of course use single brackets). Here is a fragment that declares and creates a multiarray:

\begin{displaymath}
\begin{minipage}[t]{\linewidth}\small\begin{verbatim}floa...
...tangular here (and will always be).\end{verbatim}\end{minipage}\end{displaymath}

The rank, or dimensionality, is determined by the number of asterisks that appear in the type signature, or by the number of comma-separated integer expressions that apppear in the multiarray creation expression. These expressions define the extents, or number of elements, of the multiarray in each dimension.

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:

\begin{displaymath}
\begin{minipage}[t]{\linewidth}\small\begin{verbatim}publ...
..., j] += a [i, k] * b [k, j] ;
}
}\end{verbatim}\end{minipage}\end{displaymath}

There are a few things to notice here:

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 $R-1$, where $R$ 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: A complete, sequential HPJava program.

\begin{displaymath}
\begin{minipage}[t]{\linewidth}\small\begin{verbatim}impo...
...] += a [i, k] * b [k, j] ;
}
}
}\end{verbatim}\end{minipage}\end{displaymath}

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:

\begin{displaymath}
\begin{minipage}[t]{\linewidth}\small\begin{verbatim}hpjavac EgSequentialMatmul.hpj\end{verbatim}\end{minipage}\end{displaymath}

To execute the program one would then simply run:

\begin{displaymath}
\begin{minipage}[t]{\linewidth}\small\begin{verbatim}java EgSequentialMatmul\end{verbatim}\end{minipage}\end{displaymath}

Because this is a sequential program, there is no need to define the hpjava.numprocs property. You should see the output:

\begin{displaymath}
\begin{minipage}[t]{\linewidth}\small\begin{verbatim}0.0 ...
... 1008.0 1344.0 1680.0 2016.0 2352.0\end{verbatim}\end{minipage}\end{displaymath}


next up previous contents index
Next: Some Semantic Issues Up: Multiarrays Previous: Multidimensional Arrays and Multiarrays   Contents   Index
Bryan Carpenter 2003-04-15