コード例 #1
0
ファイル: MatrixFile.java プロジェクト: kidaa30/S-Space
 /**
  * Loads the matrix from disk and returns a copy of its data. Note that a new {@code Matrix} is
  * created each time this is called.
  */
 public Matrix load() {
   try {
     return MatrixIO.readMatrix(matrixFile, format);
   } catch (IOException ioe) {
     throw new IOError(ioe);
   }
 }
コード例 #2
0
ファイル: MatrixFile.java プロジェクト: kidaa30/S-Space
 /**
  * Returns an iterator over all the entries in the matrix. The order in which entries are returned
  * is format-specific; no guarantee is provided about the ordering.
  */
 public Iterator<MatrixEntry> iterator() {
   try {
     return MatrixIO.getMatrixFileIterator(matrixFile, format);
   } catch (IOException ioe) {
     throw new IOError(ioe);
   }
 }
コード例 #3
0
  /**
   * {@inheritDoc} Once this method has been called, any subsequent calls will have no effect and
   * will not throw an exception.
   */
  public synchronized void finish() {
    if (!isFinished) {
      isFinished = true;
      try {
        matrixDos.close();
        // Re-open as a random access file so we can overwrite the 3 int
        // header that specifies the number of dimensions and values.
        // Note that the location of the matrix data is dependent on
        // whether the matrix is to be transposed.
        File dataFile = (transposeData) ? transposedMatrixFile : matrixFile;
        RandomAccessFile matrixRaf = new RandomAccessFile(dataFile, "rw");

        // Back fill the dimensions of the matrix and the number of
        // non-zero values as the 3 int header in the file
        matrixRaf.writeInt(numRows);
        matrixRaf.writeInt(curCol);
        matrixRaf.writeInt(nonZeroValues);
        matrixRaf.close();

      } catch (IOException ioe) {
        throw new IOError(ioe);
      }

      // If the user specified that the matrix should be tranposed, then
      // transposedMatrixFile will contain the matrix in its un-transposed
      // form.  Issue a call to SVDLIBC to transposed the file contents
      // for us.
      if (transposeData) {
        boolean svdlibcFailed = false;
        try {
          String commandLine =
              "svd  -r sb " + " -w sb -t -c " + transposedMatrixFile + " " + matrixFile;
          Process svdlibc = Runtime.getRuntime().exec(commandLine);
          LOGGER.fine("transposing svdlibc sparse matrix: " + commandLine);
          BufferedReader stdout =
              new BufferedReader(new InputStreamReader(svdlibc.getInputStream()));
          BufferedReader stderr =
              new BufferedReader(new InputStreamReader(svdlibc.getErrorStream()));

          StringBuilder output = new StringBuilder("SVDLIBC output:\n");
          for (String line = null; (line = stderr.readLine()) != null; ) {
            output.append(line).append("\n");
          }
          LOGGER.fine(output.toString());

          int exitStatus = svdlibc.waitFor();
          LOGGER.fine("svdlibc exit status: " + exitStatus);
          if (exitStatus != 0) {
            StringBuilder sb = new StringBuilder();
            for (String line = null; (line = stderr.readLine()) != null; ) {
              sb.append(line).append("\n");
            }
            // warning or error?
            LOGGER.warning("svdlibc exited with error status.  " + "stderr:\n" + sb.toString());
            svdlibcFailed = true;
          }
        } catch (Exception e) {
          svdlibcFailed = true;
          LOGGER.log(
              Level.WARNING,
              "an exception occurred when trying to transpose "
                  + "with svdlibc; retrying with Java code",
              e);
        }
        // If SVDLIBC failed in any way, use MatrixIO to transpose the
        // data for us.
        if (svdlibcFailed) {
          LOGGER.fine("retrying failed svdlibc transpose " + "with MatrixIO transpose");
          try {
            matrixFile =
                MatrixIO.convertFormat(
                    transposedMatrixFile,
                    Format.SVDLIBC_SPARSE_BINARY,
                    Format.SVDLIBC_SPARSE_BINARY,
                    true);
          } catch (IOException ioe) {
            throw new IOError(ioe);
          }
        }
      }
    }
  }