Example usage for java.io DataOutput writeInt

List of usage examples for java.io DataOutput writeInt

Introduction

In this page you can find the example usage for java.io DataOutput writeInt.

Prototype

void writeInt(int v) throws IOException;

Source Link

Document

Writes an int value, which is comprised of four bytes, to the output stream.

Usage

From source file:org.apache.sysml.runtime.matrix.data.MatrixBlock.java

private void writeNnzInfo(DataOutput out, boolean ultrasparse) throws IOException {
    //note: if ultrasparse, int always sufficient because nnz<rlen
    // where rlen is limited to integer

    long lrlen = (long) rlen;
    long lclen = (long) clen;

    //write long if required, otherwise int
    if (lrlen * lclen > Integer.MAX_VALUE && !ultrasparse) {
        out.writeLong(nonZeros);// w  w  w.  j  a v  a  2 s.co  m
    } else {
        out.writeInt((int) nonZeros);
    }
}

From source file:org.apache.sysml.runtime.matrix.data.MatrixBlock.java

private void writeDenseToUltraSparse(DataOutput out) throws IOException {
    out.writeByte(BlockType.ULTRA_SPARSE_BLOCK.ordinal());
    writeNnzInfo(out, true);//  w w w  . j a v a  2s.co  m

    long wnnz = 0;

    if (clen > 1) //ULTRA-SPARSE BLOCK
    {
        //block: write ijv-triples
        for (int r = 0, ix = 0; r < rlen; r++)
            for (int c = 0; c < clen; c++, ix++)
                if (denseBlock[ix] != 0) {
                    out.writeInt(r);
                    out.writeInt(c);
                    out.writeDouble(denseBlock[ix]);
                    wnnz++;
                }
    } else //ULTRA-SPARSE COL
    {
        //col: write iv-pairs
        for (int r = 0; r < rlen; r++)
            if (denseBlock[r] != 0) {
                out.writeInt(r);
                out.writeDouble(denseBlock[r]);
                wnnz++;
            }
    }

    //validity check (nnz must exactly match written nnz)
    if (nonZeros != wnnz) {
        throw new IOException(
                "Invalid number of serialized non-zeros: " + wnnz + " (expected: " + nonZeros + ")");
    }
}

From source file:org.apache.sysml.runtime.matrix.data.MatrixBlock.java

private void writeSparseBlock(DataOutput out) throws IOException {
    out.writeByte(BlockType.SPARSE_BLOCK.ordinal());
    writeNnzInfo(out, false);//from  ww w .ja va 2  s .c  o  m

    if (out instanceof MatrixBlockDataOutput) //fast serialize
        ((MatrixBlockDataOutput) out).writeSparseRows(rlen, sparseBlock);
    else //general case (if fast serialize not supported)
    {
        int r = 0;
        for (; r < Math.min(rlen, sparseBlock.numRows()); r++) {
            if (sparseBlock.isEmpty(r))
                out.writeInt(0);
            else {
                int pos = sparseBlock.pos(r);
                int nr = sparseBlock.size(r);
                int[] cols = sparseBlock.indexes(r);
                double[] values = sparseBlock.values(r);

                out.writeInt(nr);
                for (int j = pos; j < pos + nr; j++) {
                    out.writeInt(cols[j]);
                    out.writeDouble(values[j]);
                }
            }
        }
        for (; r < rlen; r++)
            out.writeInt(0);
    }
}

From source file:com.ibm.bi.dml.runtime.matrix.data.MatrixBlock.java

@Override
public void write(DataOutput out) throws IOException {
    //determine format
    boolean sparseSrc = sparse;
    boolean sparseDst = evalSparseFormatOnDisk();

    //write first part of header
    out.writeInt(rlen);
    out.writeInt(clen);/*w  w  w . j a v a  2  s .c  om*/

    if (sparseSrc) {
        //write sparse to *
        if (sparseRows == null || nonZeros == 0)
            writeEmptyBlock(out);
        else if (nonZeros < rlen && sparseDst)
            writeSparseToUltraSparse(out);
        else if (sparseDst)
            writeSparseBlock(out);
        else
            writeSparseToDense(out);
    } else {
        //write dense to *
        if (denseBlock == null || nonZeros == 0)
            writeEmptyBlock(out);
        else if (nonZeros < rlen && sparseDst)
            writeDenseToUltraSparse(out);
        else if (sparseDst)
            writeDenseToSparse(out);
        else
            writeDenseBlock(out);
    }
}

From source file:org.apache.sysml.runtime.matrix.data.MatrixBlock.java

private void writeSparseToUltraSparse(DataOutput out) throws IOException {
    out.writeByte(BlockType.ULTRA_SPARSE_BLOCK.ordinal());
    writeNnzInfo(out, true);/*from w  w  w. j a v a 2 s .  co m*/

    long wnnz = 0;
    if (clen > 1) //ULTRA-SPARSE BLOCK
    {
        //block: write ijv-triples
        for (int r = 0; r < Math.min(rlen, sparseBlock.numRows()); r++)
            if (!sparseBlock.isEmpty(r)) {
                int apos = sparseBlock.pos(r);
                int alen = sparseBlock.size(r);
                int[] aix = sparseBlock.indexes(r);
                double[] avals = sparseBlock.values(r);

                for (int j = apos; j < apos + alen; j++) {
                    //ultra-sparse block: write ijv-triples
                    out.writeInt(r);
                    out.writeInt(aix[j]);
                    out.writeDouble(avals[j]);
                    wnnz++;
                }
            }
    } else //ULTRA-SPARSE COL
    {
        //block: write iv-pairs (should never happen since always dense)
        for (int r = 0; r < Math.min(rlen, sparseBlock.numRows()); r++)
            if (!sparseBlock.isEmpty(r)) {
                int pos = sparseBlock.pos(r);
                out.writeInt(r);
                out.writeDouble(sparseBlock.values(r)[pos]);
                wnnz++;
            }
    }

    //validity check (nnz must exactly match written nnz)
    if (nonZeros != wnnz) {
        throw new IOException(
                "Invalid number of serialized non-zeros: " + wnnz + " (expected: " + nonZeros + ")");
    }
}

From source file:com.ibm.bi.dml.runtime.matrix.data.MatrixBlock.java

/**
 * //from   ww w  .j av a 2  s  . c om
 * @param out
 * @throws IOException
 */
private void writeDenseToSparse(DataOutput out) throws IOException {
    out.writeByte(BlockType.SPARSE_BLOCK.ordinal()); //block type
    writeNnzInfo(out, false);

    int start = 0;
    for (int r = 0; r < rlen; r++) {
        //count nonzeros
        int nr = 0;
        for (int i = start; i < start + clen; i++)
            if (denseBlock[i] != 0.0)
                nr++;
        out.writeInt(nr);
        for (int c = 0; c < clen; c++) {
            if (denseBlock[start] != 0.0) {
                out.writeInt(c);
                out.writeDouble(denseBlock[start]);
            }
            start++;
        }
    }
}

From source file:com.ibm.bi.dml.runtime.matrix.data.MatrixBlock.java

/**
 * /*from   w ww  .  j a  v  a2s  . c  o  m*/
 * @param out
 * @throws IOException
 */
private void writeNnzInfo(DataOutput out, boolean ultrasparse) throws IOException {
    //note: if ultrasparse, int always sufficient because nnz<rlen
    // where rlen is limited to integer

    long lrlen = (long) rlen;
    long lclen = (long) clen;

    //write long if required, otherwise int
    if (lrlen * lclen > Integer.MAX_VALUE && !ultrasparse) {
        out.writeLong(nonZeros);
    } else {
        out.writeInt((int) nonZeros);
    }
}

From source file:com.ibm.bi.dml.runtime.matrix.data.MatrixBlock.java

/**
 * //from   w  w w.  j a v  a 2s .  c  o  m
 * @param out
 * @throws IOException
 */
private void writeDenseToUltraSparse(DataOutput out) throws IOException {
    out.writeByte(BlockType.ULTRA_SPARSE_BLOCK.ordinal());
    writeNnzInfo(out, true);

    long wnnz = 0;

    if (clen > 1) //ULTRA-SPARSE BLOCK
    {
        //block: write ijv-triples
        for (int r = 0, ix = 0; r < rlen; r++)
            for (int c = 0; c < clen; c++, ix++)
                if (denseBlock[ix] != 0) {
                    out.writeInt(r);
                    out.writeInt(c);
                    out.writeDouble(denseBlock[ix]);
                    wnnz++;
                }
    } else //ULTRA-SPARSE COL
    {
        //col: write iv-pairs
        for (int r = 0; r < rlen; r++)
            if (denseBlock[r] != 0) {
                out.writeInt(r);
                out.writeDouble(denseBlock[r]);
                wnnz++;
            }
    }

    //validity check (nnz must exactly match written nnz)
    if (nonZeros != wnnz) {
        throw new IOException(
                "Invalid number of serialized non-zeros: " + wnnz + " (expected: " + nonZeros + ")");
    }
}

From source file:com.ibm.bi.dml.runtime.matrix.data.MatrixBlock.java

/**
 * /*from w w w.ja va  2s .  com*/
 * @param out
 * @throws IOException
 */
private void writeSparseBlock(DataOutput out) throws IOException {
    out.writeByte(BlockType.SPARSE_BLOCK.ordinal());
    writeNnzInfo(out, false);

    if (out instanceof MatrixBlockDataOutput) //fast serialize
        ((MatrixBlockDataOutput) out).writeSparseRows(rlen, sparseRows);
    else //general case (if fast serialize not supported)
    {
        int r = 0;
        for (; r < Math.min(rlen, sparseRows.length); r++) {
            if (sparseRows[r] == null)
                out.writeInt(0);
            else {
                int nr = sparseRows[r].size();
                out.writeInt(nr);
                int[] cols = sparseRows[r].getIndexContainer();
                double[] values = sparseRows[r].getValueContainer();
                for (int j = 0; j < nr; j++) {
                    out.writeInt(cols[j]);
                    out.writeDouble(values[j]);
                }

            }
        }
        for (; r < rlen; r++)
            out.writeInt(0);
    }
}

From source file:com.ibm.bi.dml.runtime.matrix.data.MatrixBlock.java

/**
 * //from   w ww  .  j  a v a 2 s.c om
 * @param out
 * @throws IOException
 */
private void writeSparseToUltraSparse(DataOutput out) throws IOException {
    out.writeByte(BlockType.ULTRA_SPARSE_BLOCK.ordinal());
    writeNnzInfo(out, true);

    long wnnz = 0;
    if (clen > 1) //ULTRA-SPARSE BLOCK
    {
        //block: write ijv-triples
        for (int r = 0; r < Math.min(rlen, sparseRows.length); r++)
            if (sparseRows[r] != null && !sparseRows[r].isEmpty()) {
                int alen = sparseRows[r].size();
                int[] aix = sparseRows[r].getIndexContainer();
                double[] avals = sparseRows[r].getValueContainer();
                for (int j = 0; j < alen; j++) {
                    //ultra-sparse block: write ijv-triples
                    out.writeInt(r);
                    out.writeInt(aix[j]);
                    out.writeDouble(avals[j]);
                    wnnz++;
                }
            }
    } else //ULTRA-SPARSE COL
    {
        //block: write iv-pairs (should never happen since always dense)
        for (int r = 0; r < Math.min(rlen, sparseRows.length); r++)
            if (sparseRows[r] != null && !sparseRows[r].isEmpty()) {
                out.writeInt(r);
                out.writeDouble(sparseRows[r].getValueContainer()[0]);
                wnnz++;
            }
    }

    //validity check (nnz must exactly match written nnz)
    if (nonZeros != wnnz) {
        throw new IOException(
                "Invalid number of serialized non-zeros: " + wnnz + " (expected: " + nonZeros + ")");
    }
}