Draft Proposal for Java BLAS Interface

Roldan Pozo
National Institute of Standards and Technology

The Basic Linear Algebra Subprograms (BLAS) library provides computational kernels for several of the fundamental linear algebra operations, such as matrix-multiply, the solution of triangular systems, and simple vector operations.

Java BLAS conventions

The Java BLAS (JBLAS) interface attempts to be similar to the original Fortran interface, except where differences in the Java memory model or charateristics of the language make it impossible or impractical to duplicate.

Rather than reproduce the complete BLAS interface here, we describe the translation rules to generate the Java version, based on the Fortran template. See here for the complete Fortran BLAS source code and documentation.

Java Memory model

Most of the differences between the interfaces are due to how Java handles native arrays. Like C and C++, Java matrices are stored by rows, without any guarantee that the consecutive rows are actually contiguous in memory. In fact, there is no mechanism to test for this in Java. Furthermore, because there are no capabilties to manipulate pointers directly, one cannot 'alias' subvectors, or reshape vectors into matrices. These considerations are discussed below.

The alternative possibility is to treat a 1-D Java array (vector) as a matrix by performing indexing computations. This allows flexibility to use row-major or column-major conventions, but introduces indexing overhead when accessing matrix elements. Instead, we will focus on using native Java arrays wherever possible.

Subvectors and 0/1 offset issues

The JBLAS are designed to work with submatrices. This same mechanism allows one to use 0 or 1-based offset conventions. (To use 1-basd conventions, declare a Java native array to be of size [M+1][N+1] and use the submatrix [1:M][1:N].)

For example, in Fortran, if the function SUM(N, X) adds N elements from a given vector X, then calling it as

    SUM(N, X(I))
    
sums the elements x_i, x_i+1, ..., x_i+N. Because no analogue exists in Java, we must mention subvectors explicitly by describing the original vector (X) and its offset (i) separately, i.e.
    sum(N, x, i)
    

Column vectors

Because elements in a matrix column are non-contiguous, one cannot treat these directly as a Java vector. (The analogue of this in Fortran is the use of an increment parameter to access matrix rows, but cannot be used here.) Instead, explicit mention of the 2D array must be provided. For example, to perform a daxpy on two columns of matrices A and B:

A(:,J) = A(:,J) + alpha * B(:,J).

In Fortran we would have

To perform a daxpy on two rows of A and B,

Note that Java row vectors are really 1D arrays and can be used with no argument modifications.

Fortran -> Java BLAS translation rules

Examples

References

For the complete Fortran BLAS interface, see the BLAS directory at netlib.