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:MainClass.java

public static void main(String[] args) {
    long[] primes = new long[] { 1, 2, 3, 5, 7 };
    File aFile = new File("C:/test/primes.txt");
    FileOutputStream outputFile = null;
    try {//from   w w w.ja  v  a2 s  .  co m
        outputFile = new FileOutputStream(aFile);
    } catch (FileNotFoundException e) {
        e.printStackTrace(System.err);
    }
    FileChannel file = outputFile.getChannel();
    final int BUFFERSIZE = 100;
    ByteBuffer buf = ByteBuffer.allocate(BUFFERSIZE);
    DoubleBuffer doubleBuf = buf.asDoubleBuffer();
    buf.position(8);
    CharBuffer charBuf = buf.asCharBuffer();
    for (long prime : primes) {
        String primeStr = "prime = " + prime;
        doubleBuf.put(0, (double) primeStr.length());
        charBuf.put(primeStr);
        buf.position(2 * charBuf.position() + 8);
        LongBuffer longBuf = buf.asLongBuffer();
        longBuf.put(prime);
        buf.position(buf.position() + 8);
        buf.flip();
        try {
            file.write(buf);
        } catch (IOException e) {
            e.printStackTrace(System.err);
        }
        buf.clear();
        doubleBuf.clear();
        charBuf.clear();
    }
    try {
        System.out.println("File written is " + file.size() + "bytes.");
        outputFile.close();
    } catch (IOException e) {
        e.printStackTrace(System.err);
    }
}

From source file:Main.java

public static DoubleBuffer createDoubleBufferOnHeap(final int size) {
    final DoubleBuffer buf = ByteBuffer.allocate(8 * size).order(ByteOrder.nativeOrder()).asDoubleBuffer();
    buf.clear();
    return buf;//  w  ww  . j a  v  a2 s  . c  om
}

From source file:Main.java

public static DoubleBuffer createDoubleBufferOnHeap(final int size) {
    final DoubleBuffer buf = ByteBuffer.allocate(SIZEOF_DOUBLE * size).order(ByteOrder.nativeOrder())
            .asDoubleBuffer();/*  w w  w  .j  a  v  a  2s.co  m*/
    buf.clear();
    return buf;
}

From source file:Main.java

public static DoubleBuffer createDoubleBuffer(final int size) {
    final DoubleBuffer buf = ByteBuffer.allocateDirect(SIZEOF_DOUBLE * size).order(ByteOrder.nativeOrder())
            .asDoubleBuffer();//from   w  w  w  . j  a  v a 2  s.c  o  m
    buf.clear();
    return buf;
}

From source file:Main.java

public static DoubleBuffer createDoubleBuffer(final int size) {
    final DoubleBuffer buf = ByteBuffer.allocateDirect(8 * size).order(ByteOrder.nativeOrder())
            .asDoubleBuffer();//from   ww  w .  ja  v a  2 s . c  om
    buf.clear();
    return buf;
}

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

/**
 * Compute the sum of this and <code>m</code>.
 *
 * @param m    matrix to be added//  w  w  w. j  ava 2  s  .c  o  m
 * @return     this + m
 * @throws  IllegalArgumentException if m is not the same size as this
 */
public BufferRealMatrix add(final BufferRealMatrix b) throws IllegalArgumentException {

    // safety checks
    if (b == this) {
        return this.addSelf();
    }
    MatrixUtils.checkAdditionCompatible(this, b);

    try {
        final BufferRealMatrix c = new BufferRealMatrix(rows, columns, null);

        // perform addition block-wise, to ensure good cache behavior
        for (int blockIndex = 0; blockIndex < this.blockRows * this.blockColumns; ++blockIndex) {
            // all the same size, so should all be the same blockOffsets and layout
            final long blockOffset = this.getBlockOffset(blockIndex);
            DoubleBuffer adb = this.dataFileChannel
                    .map(FileChannel.MapMode.READ_WRITE, blockOffset, BLOCK_BYTE_SIZE).asDoubleBuffer();
            adb.clear();
            DoubleBuffer bdb = b.dataFileChannel
                    .map(FileChannel.MapMode.READ_WRITE, blockOffset, BLOCK_BYTE_SIZE).asDoubleBuffer();
            bdb.clear();
            DoubleBuffer cdb = c.dataFileChannel
                    .map(FileChannel.MapMode.READ_WRITE, blockOffset, BLOCK_BYTE_SIZE).asDoubleBuffer();
            cdb.clear();
            for (int k = 0; k < BLOCK_BYTE_SIZE / DOUBLE_BYTE_SIZE; k++) {
                try {
                    cdb.put(adb.get() + bdb.get());
                } catch (BufferUnderflowException e) {
                    LOG.fatal(String.format("BufferUnderflowException while adding elements at %d in block %d",
                            k, blockIndex));
                    throw e;
                }
            }
        }
        return c;
    } catch (IOException ioe) {
        throw new RuntimeException(ioe);
    }
}

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

/** Special case for adding to ourself */
private BufferRealMatrix addSelf() {
    try {// w  w w. java2 s  . c om
        final BufferRealMatrix c = new BufferRealMatrix(rows, columns, null);

        // perform addition block-wise, to ensure good cache behavior
        for (int blockIndex = 0; blockIndex < this.blockRows * this.blockColumns; ++blockIndex) {
            // all the same size, so should all be the same blockOffsets and layout
            final long blockOffset = this.getBlockOffset(blockIndex);
            DoubleBuffer adb = this.dataFileChannel
                    .map(FileChannel.MapMode.READ_WRITE, blockOffset, BLOCK_BYTE_SIZE).asDoubleBuffer();
            adb.clear();
            DoubleBuffer cdb = c.dataFileChannel
                    .map(FileChannel.MapMode.READ_WRITE, blockOffset, BLOCK_BYTE_SIZE).asDoubleBuffer();
            cdb.clear();
            for (int k = 0; k < BLOCK_BYTE_SIZE / DOUBLE_BYTE_SIZE; k++) {
                try {
                    double ad = adb.get();
                    cdb.put(ad + ad);
                } catch (BufferUnderflowException e) {
                    LOG.fatal(String.format("BufferUnderflowException while adding elements at %d in block %d",
                            k, blockIndex));
                    throw e;
                }
            }
        }
        return c;
    } catch (IOException ioe) {
        throw new RuntimeException(ioe);
    }
}

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

/** {@inheritDoc} */
public BufferRealMatrix subtract(final BufferRealMatrix b) throws IllegalArgumentException {

    //        if (b == this) {
    //    return this.subtractSelf();
    // }//from  w  ww .j a v a 2s.c om
    // safety check
    MatrixUtils.checkSubtractionCompatible(this, b);

    try {
        final BufferRealMatrix c = new BufferRealMatrix(rows, columns, null);

        // perform addition block-wise, to ensure good cache behavior
        for (int blockIndex = 0; blockIndex < this.blockRows * this.blockColumns; ++blockIndex) {
            // all the same size, so should all be the same blockOffsets and layout
            final long blockOffset = this.getBlockOffset(blockIndex);
            DoubleBuffer adb = this.dataFileChannel
                    .map(FileChannel.MapMode.READ_WRITE, blockOffset, BLOCK_BYTE_SIZE).asDoubleBuffer();
            adb.clear();
            DoubleBuffer bdb = b.dataFileChannel
                    .map(FileChannel.MapMode.READ_WRITE, blockOffset, BLOCK_BYTE_SIZE).asDoubleBuffer();
            bdb.clear();
            DoubleBuffer cdb = c.dataFileChannel
                    .map(FileChannel.MapMode.READ_WRITE, blockOffset, BLOCK_BYTE_SIZE).asDoubleBuffer();
            cdb.clear();
            for (int k = 0; k < BLOCK_BYTE_SIZE / DOUBLE_BYTE_SIZE; k++) {
                try {
                    cdb.put(adb.get() - bdb.get());
                } catch (BufferUnderflowException e) {
                    LOG.fatal(String.format("BufferUnderflowException while adding elements at %d in block %d",
                            k, blockIndex));
                    throw e;
                }
            }
        }
        return c;
    } catch (IOException ioe) {
        throw new RuntimeException(ioe);
    }
}

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

/** {@inheritDoc} */
public BufferRealMatrix scalarAdd(final double d) {
    try {//from  w  ww  .  ja  v  a  2 s.c  o  m
        final BufferRealMatrix c = new BufferRealMatrix(rows, columns, null);

        // perform addition block-wise, to ensure good cache behavior
        for (int blockIndex = 0; blockIndex < this.blockRows * this.blockColumns; ++blockIndex) {
            // all the same size, so should all be the same blockOffsets and layout
            final long blockOffset = this.getBlockOffset(blockIndex);
            DoubleBuffer adb = this.dataFileChannel
                    .map(FileChannel.MapMode.READ_WRITE, blockOffset, BLOCK_BYTE_SIZE).asDoubleBuffer();
            adb.clear();
            DoubleBuffer cdb = c.dataFileChannel
                    .map(FileChannel.MapMode.READ_WRITE, blockOffset, BLOCK_BYTE_SIZE).asDoubleBuffer();
            cdb.clear();
            for (int k = 0; k < BLOCK_BYTE_SIZE / DOUBLE_BYTE_SIZE; k++) {
                try {
                    double ad = adb.get();
                    cdb.put(ad + d);
                } catch (BufferUnderflowException e) {
                    LOG.fatal(String.format("BufferUnderflowException while adding elements at %d in block %d",
                            k, blockIndex));
                    throw e;
                }
            }
        }
        return c;
    } catch (IllegalArgumentException ex) {
        LOG.fatal(ex);
        throw new RuntimeException(ex);
    } catch (IOException ex) {
        LOG.fatal(ex);
        throw new RuntimeException(ex);
    }
}

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

/**
 * Returns the result of postmultiplying this by m.
 *
 * @param m    matrix to postmultiply by
 * @return     this * m/*from  w w  w  .  j  a v a 2  s  .  c  om*/
 * @throws     IllegalArgumentException
 *             if columnDimension(this) != rowDimension(m)
 */
public BufferRealMatrix multiply(final BufferRealMatrix b) throws IllegalArgumentException {

    // safety check
    MatrixUtils.checkMultiplicationCompatible(this, b);

    try {
        final BufferRealMatrix c = new BufferRealMatrix(rows, b.columns, null);
        // allocate one row for our matrix
        final ByteBuffer abb = ByteBuffer.allocate(BLOCK_SIZE * DOUBLE_BYTE_SIZE);
        // for some funny reason we can't get an array, even if we wrap it before! So, allocate it here and use latter
        //  final double[] ar = new double[BLOCK_SIZE]; This isn't faster

        // perform multiplication block-wise, to ensure good cache behavior
        int blockIndex = 0;
        for (int iBlock = 0; iBlock < c.blockRows; ++iBlock) {
            final int pStart = iBlock * BLOCK_SIZE;
            final int pEnd = Math.min(pStart + BLOCK_SIZE, rows);
            //System.err.printf("pStart=%d\tpEnd=%d\tblockRows=%d\tblockColumns=%d\n", pStart, pEnd, c.blockRows, c.blockColumns);
            for (int jBlock = 0; jBlock < c.blockColumns; ++jBlock) {
                final int jWidth = BLOCK_SIZE; // square block no matter what
                final int jWidth2 = jWidth + jWidth;
                final int jWidth3 = jWidth2 + jWidth;
                final int jWidth4 = jWidth3 + jWidth;

                // select current product block
                DoubleBuffer cdb = c.dataFileChannel
                        .map(FileChannel.MapMode.READ_WRITE, c.getBlockOffset(blockIndex), BLOCK_BYTE_SIZE)
                        .asDoubleBuffer();
                cdb.clear();

                // perform multiplication on current block
                for (int kBlock = 0; kBlock < blockColumns; ++kBlock) {
                    //final int kWidth = blockWidth(kBlock);
                    final int kWidth = BLOCK_SIZE;

                    LOG.debug(String.format("Getting a block %d and b block %d", iBlock * blockColumns + kBlock,
                            kBlock * b.blockColumns + jBlock));

                    // walk down the blocks columns
                    DoubleBuffer bdb = b.dataFileChannel
                            .map(FileChannel.MapMode.READ_WRITE,
                                    b.getBlockOffset(kBlock * b.blockColumns + jBlock), BLOCK_BYTE_SIZE)
                            .asDoubleBuffer();
                    bdb.clear();

                    LOG.debug("Processing blocks");
                    for (int p = pStart, k = 0; p < pEnd; ++p) {
                        // a's width (# cols) is the same as b's height (# rows) and c's width
                        final int lStart = (p - pStart) * kWidth; // Square padded with zeros    
                        final int lEnd = blockWidth(kBlock); // Can stop at the last column in a's block
                        //System.err.printf("k=%d\tp=%d\tlstart=%d\tlend=%d\t\n", k, p, lStart, lEnd);
                        // For each row in a, multiple the columns in b
                        // Can stop at the last column in the c's block which should be the last column in b

                        // walk across A's blocks rows grabbing a row at a time
                        abb.clear();
                        this.dataFileChannel.position(this.getBlockOffset(iBlock * blockColumns + kBlock)
                                + (lStart * DOUBLE_BYTE_SIZE));
                        final int r = this.dataFileChannel.read(abb); // relative get into local bytebuffer
                        //System.err.printf("Got %d bytes (%d doubles) for %d block width\n", r, r / DOUBLE_BYTE_SIZE, kWidth);
                        if (r == -1) {
                            LOG.fatal("Unable to read in data");
                        }
                        abb.clear();
                        final DoubleBuffer adb = abb.asDoubleBuffer();
                        adb.clear();
                        // tried getting access to local copy (array) but it wasn't faster access

                        for (int nStart = 0; nStart < c.blockWidth(jBlock); ++nStart) {
                            double sum = 0;
                            int l = 0; // first column in this row
                            int n = nStart;
                            // do four at a time (why four?)
                            adb.position(l);

                            while (l < lEnd - 3) {
                                sum += adb.get() * bdb.get(n) + adb.get() * bdb.get(n + jWidth)
                                        + adb.get() * bdb.get(n + jWidth2) + adb.get() * bdb.get(n + jWidth3);
                                l += 4;
                                n += jWidth4;
                            }
                            while (l < lEnd) {
                                sum += adb.get() * bdb.get(n);
                                n += jWidth;
                                l++;
                            }
                            sum += cdb.get(k);
                            cdb.put(k++, sum);
                            //System.err.printf("k=%d\tn=%d\n", k, n);
                        }
                        // correct k for difference in blockWidth since we are always square
                        k = (p + 1) * BLOCK_SIZE;
                        //System.err.printf("end of p-loop (%d), k=%d\n", p, k);
                    }
                }
                this.dataFileChannel.force(false);
                System.err.printf("Finished block %d\n", blockIndex);
                // go to next block
                ++blockIndex;
            }
        }
        return c;
    } catch (IOException ex) {
        throw new IllegalArgumentException(ex.getMessage());
    }
}