Presentation is loading. Please wait.

Presentation is loading. Please wait.

Data Structures in Java for Matrix Computations Geir Gundersen Department of Informatics University of Bergen Norway Joint work with Trond Steihaug.

Similar presentations


Presentation on theme: "Data Structures in Java for Matrix Computations Geir Gundersen Department of Informatics University of Bergen Norway Joint work with Trond Steihaug."— Presentation transcript:

1 Data Structures in Java for Matrix Computations Geir Gundersen Department of Informatics University of Bergen Norway Joint work with Trond Steihaug

2 Overview We will show how to utilize Java’s native arrays for matrix computations. zHow to use Java arrays as a 2D array for efficient dense matrix computation. zHow to create efficient sparse matrix data structure using Java arrays. zObject-oriented programming have been favored in the last decade(s): yEasy to understand paradigm. yStraightforward to build large scale applications. zJava will be used for (limited) numerical computations. yJava is already introduced as the programming language in introductory courses in scientific computation. yImpact on computing will force new fields to use Java.

3 A “mathematical” 2D array

4 A 2D Java Array zArray elements that refers to another array creates a multidimensional array.

5 A true 2D Java Array

6 Java Arrays zJava arrays are true objects. yThus creating an array is object creation. zThe objects of an array of objects are not necessarily stored continuously. yAn array of objects stores references to the actual objects. zThe primitive elements of an array are most likely stored continuously. yAn array of primitive elements holds the actual values for those elements.

7 Frobenius Norm Example

8 zBasic observation: yAccessing the consecutive elements in a row will be faster then accessing consecutive elements in a column.

9 Matrix Multiplication Algorithms zThe efficiency of the matrix multiplication operation is dependent on the details of the underlying data structure both hardware and software. zWe discuss several different implementations using Java arrays as the data structure: y A straightforward matrix multiplication algorithm yA package implementation that is highly optimized yAn algorithm that takes the row-wise layout into fully consideration and uses the same optimizing techniques as the package implementation.

10 Matrix Multiplication Algorithms A straightforward matrix multiplication operation. for(int i = 0; i<m;i++){ for(int j = 0;j<n;j++){ for(int k = 0;k<p;k++){ C[i][j] += A[i][k]*B[k][j]; } Interchanging the three for loops give six distinct ways (pure row, pure column, and partial row/column).

11 Matrix Multiplication Algorithms The loop orders tells us how the matrices involved gets traversed in the course of the matrix multiplication operation. We see the same time differences with pure row versus pure column as we did with the Frobenius norm example. This is the same effect.

12 Matrix Multiplication Algorithms zThe time differences are due to accessing different object arrays when traversing columns as opposed to accessing the same object array several times (when traversing a row). zFor a rectangular array of primitive elements, the elements of a row will be stored continuously, but the rows may be scattered. zDifferences between row and column traversing is also an issue in FORTRAN, C and C++ but the differences are not so significant.

13 JAMA zA basic linear algebra package implemented in Java. zIt provides user-level classes for constructing and manipulating real dense matrices. zIt is intended to serve as the standard matrix class for Java. zJAMA is comprised of six Java classes: yMatrix: xMatrix Multiplication: A.times(B) yCholeskyDecomposition yLUDecomposition yQRDecomposition ySingularValueDecomposition yEigenvalueDecomposition

14 Matrix Multiplication Operations

15 JAMA versus Pure-Row zA comparison on input AB is shown for square matrices. zThe pure row-oriented algorithm has an average of 30 % better performance than JAMA's algorithm.

16 JAMA versus Pure-Row zJAMA's algorithm is more efficient than the pure row-oriented algorithm on input Ab with an average factor of two.

17 JAMA versus Pure-Row zThere is a significant difference between JAMA's algorithm versus the pure row-oriented algorithm on b T A with an average factor of 7. zIn this case JAMA is less efficient. zThe break even results.

18 Sparse Matrices zA sparse matrix is usually defined as a matrix where "many" of its elements are equal to zero zWe benefit both in time and space by working only on the nonzero data structure. zCurrently there is no packages implemented in Java for matrix computation on sparse matrices, as complete as JAMA (for dense matrices).

19 Sparse Matrix Concept zThe Sparse Matrix Concept (SMC) is a general object- oriented structure.  The Rows objects stores the arrays for the nonzero values and indexes.

20 Java Sparse Array zThe Java Sparse Array (JSA) format is a new concept for storing sparse matrices made possible with Java. zOne array for storing the references to the value arrays and one for storing the references to the index arrays. Java's native arrays can store object references therefore the extra Rows object layer in SMC is unnecessarily in Java.

21 Compressed Row Storage zThe most commonly used storage schemes for large sparse matrices: yCompressed Row/Column Storage zThese storage schemes have enjoyed several decades of research zThe compressed storage schemes have minimal memory requirements.

22 Numerical Results These numerical results shows that CRS, SMC and JSA have approximately the same performance.

23 Sparse Matrix Update zConsider the outer product ab T of the two vectors a,b where many of the elements are 0. zThe outer product will be a sparse matrix with some rows where all elements are 0, and the corresponding sparse data structure will have rows without any elements. zA typical operation is a rank one update of an n x n matrix A: zwhere a i is element i in a and b j is element j in b. Thus only those rows of A where a i is different from 0 need to be updated.

24 Numerical Results These numerical results shows that JSA is more efficient than CRS with an average factor of 78 which is significant.

25 Concluding Remarks zUsing Java arrays as a 2D array for dense matrices we need to consider that the rows are independent objects. zOther suggestion to eliminate the row versus column “problem”: yCluster row objects together in memory. yCreating a Java array class, avoiding array of arrays. zJava Sparse Array: yManipulating only the rows of the structure without updating or traversing the rest of the structure, unlike Compressed Row Storage. yMore efficient, less memory requirements and have a more natural notation than SMC. zPeople will use Java for numerical computations, therefore it may be useful to invest time and resources finding how to use Java for numerical computation. zThis work has given ideas of how some constructions in Java restricts natural development (rows versus columns). z Java has flexibility that is not fully explored (Java Sparse Array).


Download ppt "Data Structures in Java for Matrix Computations Geir Gundersen Department of Informatics University of Bergen Norway Joint work with Trond Steihaug."

Similar presentations


Ads by Google