Example usage for java.nio DoubleBuffer clear

List of usage examples for java.nio DoubleBuffer clear

Introduction

In this page you can find the example usage for java.nio DoubleBuffer clear.

Prototype

public final Buffer clear() 

Source Link

Document

Clears this buffer.

Usage

From source file:org.mitre.math.linear.BufferRealMatrix.java

/**
 * Gets a submatrix. Rows and columns are indicated
 * counting from 0 to n-1./*from w ww . j av a  2s.co m*/
 *
 * @param startRow Initial row index
 * @param endRow Final row index (inclusive)
 * @param startColumn Initial column index
 * @param endColumn Final column index (inclusive)
 * @return The subMatrix containing the data of the
 *         specified rows and columns
 * @exception MatrixIndexException  if the indices are not valid
 */
public double[][] getSubMatrixData(final int startRow, final int endRow, final int startColumn,
        final int endColumn) throws MatrixIndexException {

    // safety checks
    MatrixUtils.checkSubMatrixIndex(this, startRow, endRow, startColumn, endColumn);

    // create the output matrix
    final double[][] out = new double[endRow - startRow + 1][endColumn - startColumn + 1];

    // compute blocks shifts
    final int blockStartRow = startRow / BLOCK_SIZE;
    final int blockEndRow = endRow / BLOCK_SIZE;
    final int blockStartColumn = startColumn / BLOCK_SIZE;
    final int blockEndColumn = endColumn / BLOCK_SIZE;

    // perform extraction block-wise, to ensure good cache behavior
    for (int iBlock = blockStartRow; iBlock <= blockEndRow; ++iBlock) {
        for (int jBlock = blockStartColumn; jBlock <= blockEndColumn; ++jBlock) {
            // blocks are rows major order
            final int blockIndex = iBlock * this.blockColumns + jBlock;
            try {
                final long blockOffset = this.getBlockOffset(blockIndex);
                final DoubleBuffer block = this.dataFileChannel
                        .map(FileChannel.MapMode.READ_WRITE, blockOffset, BLOCK_BYTE_SIZE).asDoubleBuffer();
                block.clear();
                final int iStart = Math.max(startRow, iBlock * BLOCK_SIZE);
                final int iEnd = Math.min(endRow, (iBlock * BLOCK_SIZE) + BLOCK_SIZE);
                final int jStart = Math.max(startColumn, jBlock * BLOCK_SIZE);
                final int jEnd = Math.min(endColumn, (jBlock * BLOCK_SIZE) + BLOCK_SIZE);

                LOG.info(String.format("blockIndex=%d (%d, %d), iStart=%d, iEnd=%d, jStart=%d, jEnd=%d",
                        blockIndex, jBlock, iBlock, iStart, iEnd, jStart, jEnd));
                for (int i = iStart; i < iEnd; ++i) {
                    for (int j = jStart; j < jEnd; ++j) {
                        // data in blocks are rows major, get our index in the block
                        final int index = (i - iBlock * BLOCK_SIZE) * BLOCK_SIZE + (j - jBlock * BLOCK_SIZE);
                        out[i - startRow][j - startColumn] = block.get(index);
                    }
                }
            } catch (IOException ioe) {
                throw new MathRuntimeException(
                        "IO Exception while visiting blockIndex {0} (iBlock={1}, jBlock={2})", blockIndex,
                        iBlock, jBlock);
            }
        }
    }
    return out;
}

From source file:org.mitre.math.linear.BufferRealMatrix.java

/** {@inheritDoc} */
@Override/*w w w.  j  a va  2  s .  c om*/
public double walkInOptimizedOrder(final RealMatrixChangingVisitor visitor) throws MatrixVisitorException {
    visitor.start(rows, columns, 0, rows - 1, 0, columns - 1);
    for (int iBlock = 0, blockIndex = 0; iBlock < blockRows; ++iBlock) {
        final int pStart = iBlock * BLOCK_SIZE;
        final int pEnd = Math.min(pStart + BLOCK_SIZE, rows);

        for (int jBlock = 0; jBlock < blockColumns; ++jBlock, ++blockIndex) {
            try {
                final int qStart = jBlock * BLOCK_SIZE;
                final int qEnd = Math.min(qStart + BLOCK_SIZE, columns);

                final long blockOffset = this.getBlockOffset(blockIndex);
                LOG.debug(String.format("BlockIndex=%d (offset=%d) pStart=%d pEnd=%d qStart=%d qEnd=%d",
                        blockIndex, blockOffset, pStart, pEnd, qStart, qEnd));

                final DoubleBuffer block = this.dataFileChannel
                        .map(FileChannel.MapMode.READ_WRITE, blockOffset, BLOCK_BYTE_SIZE).asDoubleBuffer();
                block.clear();

                for (int p = pStart, k = 0; p < pEnd; ++p) {
                    // jump to end of row incase we are not there
                    k = (p - pStart) * BLOCK_SIZE;
                    for (int q = qStart; q < qEnd; ++q, ++k) {
                        block.put(k, visitor.visit(p, q, block.get(k)));
                    }
                }
                this.dataFileChannel.force(false);
            } catch (IOException ioe) {
                throw new MathRuntimeException(
                        "IO Exception while visiting blockIndex {0} (iBlock={1}, jBlock={2})", blockIndex,
                        iBlock, jBlock);
            }
        }
    }
    return visitor.end();
}

From source file:org.mitre.math.linear.BufferRealMatrix.java

/** {@inheritDoc} */
@Override/*from  www.j  a  v  a  2 s  .  c o m*/
public double walkInOptimizedOrder(final RealMatrixPreservingVisitor visitor) throws MatrixVisitorException {
    visitor.start(rows, columns, 0, rows - 1, 0, columns - 1);
    for (int iBlock = 0, blockIndex = 0; iBlock < blockRows; ++iBlock) {
        final int pStart = iBlock * BLOCK_SIZE;
        final int pEnd = Math.min(pStart + BLOCK_SIZE, rows);

        for (int jBlock = 0; jBlock < blockColumns; ++jBlock, ++blockIndex) {
            try {
                final int qStart = jBlock * BLOCK_SIZE;
                final int qEnd = Math.min(qStart + BLOCK_SIZE, columns);

                final long blockOffset = this.getBlockOffset(blockIndex);
                final DoubleBuffer block = this.dataFileChannel
                        .map(FileChannel.MapMode.READ_WRITE, blockOffset, BLOCK_BYTE_SIZE).asDoubleBuffer();
                block.clear();

                LOG.debug(String.format("BlockIndex=%d pStart=%d pEnd=%d qStart=%d qEnd=%d", blockIndex, pStart,
                        pEnd, qStart, qEnd));
                for (int p = pStart, k = 0; p < pEnd; ++p) {
                    // jump to end of row incase we are not there
                    k = (p - pStart) * BLOCK_SIZE;
                    for (int q = qStart; q < qEnd; ++q, ++k) {
                        visitor.visit(p, q, block.get(k));
                    }
                }

                this.dataFileChannel.force(false);
            } catch (IOException ioe) {
                throw new MathRuntimeException(
                        "IO Exception while visiting blockIndex {0} (iBlock={1}, jBlock={2})", blockIndex,
                        iBlock, jBlock);
            }
        }
    }
    return visitor.end();
}