Support for I/O in the current HPJava version of Adlib is rudimentary.
In general any communications needed to input and output parallel data
structures are left as the reponsibility of the HPJava programmer--such
operations may, for example, be built up from standard Java I/O features,
together with the broadcast and remap functions of Adlib. But to help
you get started we provide a couple of ``convenience methods''.
The most interesting is aprintf(). This is modelled
on the C printf() function. Its arguments are a control string
and a list of distributed arrays. The effective prototypes are
The control string (like all arguments of Adlib methods) should be a
``coherent'' expression: it takes the same value for all processes in
the active group. It has basically the same role and the same format
as the control string of the printf() function in the C standard
library. But the aprintf() operation performs its output
``elementally''. If there is a single array argument it is as if
a printf() function was called repeatedly, once for each element
of the array. For example
%R0interpolates the index value into the string without the need to initialize an extra array of integers.
All this works for multidimensional arrays as well.
In a pseudocode notation, the general behaviour of aprintf() is like
%R0in the control string; the value is interpolated wherever there is a
%R1; and so on (a field width modifier is also allowed with
%Rformat, just as with
%dformat). The imaginary elemental printf operation outputs to System.out on the root process of the active process group.
This code, for example:
For large, possibly multidimensional, arrays one would probably prefer a
more tabular layout. The aprintf() method provides one special format
to support more creative line breaking. The aprintf() format
%N by itself behaves exactly like the
\n escape sequence--it
inserts a newline into the output. But
%N allows an integer
modifier. This looks like a printf() field width modifier, but
it is interpretted differently--it defines the frequency with which
the newline is printed: if the value of the modifier is , the newline
is only printed in every th elemental print operation. So if we replace
the aprintf() call in the previous example with:
%fformat one could line up columns more beautifully, if so desired.
As a final trick, we can build the control string at run-time, allowing
the number of columns to reflect the actual shape of the array:
"%f %8N", and a newline is printed after every 8 elemental outputs, i.e. we get a newline at the end of every row of the array, whatever the actual shape of the array. With a second modified
%Nformat in the control string, one might use similar tricks to insert a blank line to delimit the end of each ``plane'' of a three dimensional array. And so on.
The aprintf() method is useful for testing and demonstrating programs. It is less obvious that it is useful for production codes. We expect distributed arrays to be large, and in general it probably it doesn't make much sense to print thousands or millions of elements to standard output. With this in mind the current implementation has not been carefully optimized--it is quite slow.
For convenience there are also two much simpler methods in Adlib for
printing a ``global'' String value. These are called gprint()
and gprintln(). They have interfaces: