next up previous
Next: The Computational Kernel Up: The Sparse Matrix Benchmark Previous: Test problems

I/O considerations

Reading in a matrix in F90 and C is simple. Here is the F90 code fragment


  ! read in the row, column sizes and number of nonzeros
  read(input_unit,*) n,m,nz 
  allocate(irn(nz),jcn(nz))

  ! read in the row and column indices (sparse pattern)
  do i = 1, nz
     read(input_unit,*) irn(i),jcn(i)
  end do
and here is the C code fragment

    /* read in the row and columb sizes 
       and the number of nonzeros */
    fscanf(fp,"%d %d %d",&n, &m, &nz) ;

    /* read in the matrix */
    irn = (int*) malloc((nz)*sizeof(int));
    jcn = (int*) malloc((nz)*sizeof(int));
    for (i = 0; i < nz; i++) {
      fscanf(fp,"%d %d",&irn[i],&jcn[i]);
    }
However the JAVA code for input needs more attention because there is more than one way of writing a JAVA code for reading from the ASCII matrix files, with quite different performances. Initially we tried the readLine method of the BufferReader class, which processes the matrix file one line at a time. The line is then turned into integers by the String2int method. When tested on a dual processors Pentium II running Linux and the Sun JAVA compiler version 1.2.2, this approach was found to be up to 30 times slower than C. After some trial and error, the following approach was finally adopted instead:


//open the matrix file
FileInputStream is = new FileInputStream(MatrixFileName);
BufferedReader bis = new BufferedReader(new InputStreamReader(is));
StreamTokenizer st = new StreamTokenizer(bis);

//read in the row and column sizes
st.nextToken(); int n = (int) st.nval;
st.nextToken(); int m = (int) st.nval;
st.nextToken(); int nz = (int) st.nval;

int[] irn = new int[nz];
int[] jcn = new int[nz];

for (int j = 0; j < nz; j++) {
    st.nextToken();
    irn[j] = (int) st.nval;
    st.nextToken();
    jcn[j] = (int) st.nval;
}

In this approach the whole file is read in as an $InputStream$ and buffered, it is then tokenized to extract the sparse patterns. Using a $BufferedReader$ is very important since unbuffered I/O can be very slow.




2000-08-16