next up previous contents
Next: Low Level SPMD Programming Up: The HPJava Language Previous: More Distribution Formats   Contents


Array Sections

HPJava supports subarrays modeled on the array sections of Fortran 90. The syntax of an array section is similar to a multiarray element reference, but uses double brackets. Whereas an element reference is a variable, an array section is an expression that represents a new multiarray object. The new multiarray object doesn't contain new elements. It contains a subset of the elements of the parent array. The options for subscripts in array sections are more flexible than those in multiarrays. Most importantly, subscripts of an array section may be triplets[*] as in Fortran 90. An array section expression has an array dimension for each triplet subscript. So the rank of an array section expression is equal to the number of triplet subscripts. If an array section has some scalar subscripts similar to those in element references, the rank of the array section is lower than the parent array. The following HPJava code contains the example of array section expressions.

$\displaystyle \begin{minipage}[t]{\linewidth}\small\begin{verbatim}
Procs2 p = ...
...] ;
foo(a [[0 : N / 2 - 1, 0 : N - 1 : 2]]) ;
}\end{verbatim}\end{minipage}
$

The first array section expression appears on the right hand side of the definition of b. It specifies b as an alias for the first row of a (Figure 3.16). In an array section expression, unlike in an array element reference, a scalar subscript is always allowed to be an integer expression[*]. The second array section expression appears as an argument to the method foo. It represents a two-dimensional, $ N/2 \times N/2$, subset of the elements of a, visualized in Figure 3.17.
Figure 3.16: A one-dimensional section of a two-dimensional array (shaded area).
\includegraphics[width=3.5in]{Figures/1d-section}
Figure 3.17: A two-dimensional section of a two-dimensional array (shaded area).
\includegraphics[width=3.5in]{Figures/2d-section}
What are the ranges of b from the above example? That is, what does the rng() inquiry of b return? The ranges of an array section are called subranges, as a matter of fact. These are different from ranges considered so far. For the completeness, the language has a special syntax for writing subranges directly. Ranges identical to those of the argument of foo from the above could be generated by

$\displaystyle \begin{minipage}[t]{\linewidth}\small\begin{verbatim}
Range u = x...
...: N / 2 - 1]] ;
Range v = y [[0 : N - 1 : 2]] ;\end{verbatim}\end{minipage}
$

which look quite natural and similar to the subscripts for location expressions. But, the subscript is a triplet. The global indices related with the subrange v are in the range $ 0, \dots,
{\tt v.size()}-1$. A subrange inherits locations from its parent range, but it specifically does not inherit global indices from the parent range. As triplet section subscripts motivated us to define subranges as a new kind range, scalar section subscripts will drive us to define a new kind of group. A restricted group is the subset of processes in some parent group to which a specific location is mapped. From the above example, the distribution group of b is the subset of processes in p to which the location x[0] is mapped. In order to represent the restricted group, the division operator is overloaded. Thus, the distribution group of b is equal to q as follws;

$\displaystyle \begin{minipage}[t]{\linewidth}\small\begin{verbatim}
Group q = p / x[0] ;\end{verbatim}\end{minipage}
$

In a sense the definition of a restricted group is tacit in the definition of an abstract location. In Figure 3.6 section the set of processes with coordinates $ (0,0)$, $ (0,1)$ and $ (0,2)$, to which location x[1] is mapped, can now be written as

$\displaystyle \begin{minipage}[t]{\linewidth}\small\begin{verbatim}
p / x[1] ;\end{verbatim}\end{minipage}
$

and the set with coordinates $ (0,1)$ and $ (1,1)$, to which y[4] is mapped, can be written as

$\displaystyle \begin{minipage}[t]{\linewidth}\small\begin{verbatim}
p / y[4] ;\end{verbatim}\end{minipage}
$

The intersection of these two--the group containing the single process with coordinates $ (0,1)$--can be written as

$\displaystyle \begin{minipage}[t]{\linewidth}\small\begin{verbatim}
p / x[1] / y[4] ;\end{verbatim}\end{minipage}
$

or as

$\displaystyle \begin{minipage}[t]{\linewidth}\small\begin{verbatim}
p / y[4] / x[1] ;\end{verbatim}\end{minipage}
$

We didn't restrict that the subscripts in an array section expression must include some triplets (or ranges). It is legitimate for all the subscripts to be ``scalar''. In this case the resulting ``array'' has rank 0. There is nothing pathological about rank-0 arrays. They logically maintain a single element, bundled with the distribution group over which this element is replicated. Because they are logically multiarrays they can be passed as arguments to Adlib functions such as remap. If a and b are multiarrays, we cannot usually write a statement like

$\displaystyle \begin{minipage}[t]{\linewidth}\small\begin{verbatim}
a [4, 5] = b [19] ;\end{verbatim}\end{minipage}
$

since the elements involved are generally held on different processors. As we have seen, HPJava imposes constraints that forbid this kind of direct assignment between array element references. However, if it is really needed, we can usually achieve the same effect by writing

$\displaystyle \begin{minipage}[t]{\linewidth}\small\begin{verbatim}
Adlib.remap (a [[4, 5]], b [[19]]) ;\end{verbatim}\end{minipage}
$

The arguments are rank-0 sections holding just the destination and source elements. The type signature for a rank-0 array with element of type $ T$ is written $ T$[[]][*]. Rank-0 multiarrays, which we will also call simply ``scalars'', can be created as follows:

$\displaystyle \begin{minipage}[t]{\linewidth}\small\begin{verbatim}
double [[-,...
...;
Adlib.remap (c, a [[4, 5]]) ;
double d = c[];\end{verbatim}\end{minipage}
$

This example illustrates one way to broadcast an element of a multiarray: remap it to a scalar replicated over the active process group. The element of the scalar is extracted through a multiarray element reference with an empty subscript list. (Like any other multiarray, scalar constructors can have on clauses, specifying a non-default distribution group.)
next up previous contents
Next: Low Level SPMD Programming Up: The HPJava Language Previous: More Distribution Formats   Contents
Bryan Carpenter 2004-06-09