Search in sources :

Example 21 with DoubleBuffer

use of java.nio.DoubleBuffer in project robovm by robovm.

the class Main method storeValues.

static void storeValues(ByteBuffer directBuf) {
    directBuf.position(0);
    ShortBuffer shortBuf = directBuf.asShortBuffer();
    CharBuffer charBuf = directBuf.asCharBuffer();
    IntBuffer intBuf = directBuf.asIntBuffer();
    FloatBuffer floatBuf = directBuf.asFloatBuffer();
    LongBuffer longBuf = directBuf.asLongBuffer();
    DoubleBuffer doubleBuf = directBuf.asDoubleBuffer();
    final byte byteValue = -5;
    final short shortValue = -1234;
    final char charValue = 49200;
    final int intValue = 0x12345678;
    final float floatValue = 3.14159f;
    final long longValue = 0x1122334455667788L;
    final double doubleValue = Double.MIN_VALUE;
    if (directBuf.put(1, byteValue).get(1) != byteValue) {
        throw new RuntimeException("byte get/store failed");
    }
    if (shortBuf.put(1, shortValue).get(1) != shortValue) {
        throw new RuntimeException("short get/store failed");
    }
    if (charBuf.put(2, charValue).get(2) != charValue) {
        throw new RuntimeException("char get/store failed");
    }
    if (intBuf.put(2, intValue).get(2) != intValue) {
        throw new RuntimeException("int get/store failed");
    }
    if (floatBuf.put(3, floatValue).get(3) != floatValue) {
        throw new RuntimeException("float get/store failed");
    }
    if (longBuf.put(2, longValue).get(2) != longValue) {
        throw new RuntimeException("long get/store failed");
    }
    if (doubleBuf.put(3, doubleValue).get(3) != doubleValue) {
        throw new RuntimeException("double get/store failed");
    }
    directBuf.position(0);
    char[] outBuf = new char[directBuf.limit() * 2];
    for (int i = 0; i < directBuf.limit(); i++) {
        byte b = directBuf.get();
        outBuf[i * 2] = hexChar((byte) ((b >> 4) & 0x0f));
        outBuf[i * 2 + 1] = hexChar((byte) (b & 0x0f));
    }
    System.out.println(new String(outBuf));
}
Also used : DoubleBuffer(java.nio.DoubleBuffer) LongBuffer(java.nio.LongBuffer) CharBuffer(java.nio.CharBuffer) FloatBuffer(java.nio.FloatBuffer) IntBuffer(java.nio.IntBuffer) ShortBuffer(java.nio.ShortBuffer)

Example 22 with DoubleBuffer

use of java.nio.DoubleBuffer in project angel by Tencent.

the class DefaultRowUpdater method updateDoubleDenseToDoubleDense.

public void updateDoubleDenseToDoubleDense(int size, ByteBuf buf, ServerDenseDoubleRow row, int compressRatio) {
    int bitPerItem = 8 * 8 / compressRatio;
    DoubleBuffer data = row.getData();
    LOG.debug("update double to double, size: " + size);
    if (size <= 0)
        return;
    double maxAbs = buf.readDouble();
    int maxPoint = (int) Math.pow(2, bitPerItem - 1) - 1;
    for (int i = 0; i < size - 1; i++) {
        byte[] itemBytes = new byte[bitPerItem / 8];
        buf.readBytes(itemBytes);
        int point = byteArray2int(itemBytes);
        double parsedValue = (double) point / (double) maxPoint * maxAbs;
        data.put(i, data.get(i) + parsedValue);
    }
    LOG.info(String.format("parse compressed %d double data, max abs: %f, max point: %d", size - 1, maxAbs, maxPoint));
}
Also used : DoubleBuffer(java.nio.DoubleBuffer)

Example 23 with DoubleBuffer

use of java.nio.DoubleBuffer in project angel by Tencent.

the class Amax method doProcessRow.

@Override
protected double doProcessRow(ServerDenseDoubleRow row) {
    double amax = Double.MIN_VALUE;
    DoubleBuffer data = row.getData();
    int size = row.size();
    for (int i = 0; i < size; i++) {
        amax = Math.max(amax, Math.abs(data.get(i)));
    }
    return amax;
}
Also used : DoubleBuffer(java.nio.DoubleBuffer)

Example 24 with DoubleBuffer

use of java.nio.DoubleBuffer in project angel by Tencent.

the class Asum method doProcessRow.

@Override
protected double doProcessRow(ServerDenseDoubleRow row) {
    double asum = 0.0;
    DoubleBuffer data = row.getData();
    int size = row.size();
    for (int i = 0; i < size; i++) {
        asum += Math.abs(data.get(i));
    }
    return asum;
}
Also used : DoubleBuffer(java.nio.DoubleBuffer)

Example 25 with DoubleBuffer

use of java.nio.DoubleBuffer in project angel by Tencent.

the class Equal method doProcessRow.

@Override
protected double doProcessRow(ServerDenseDoubleRow row1, ServerDenseDoubleRow row2) {
    double isEqual = 1.0;
    DoubleBuffer data1 = row1.getData();
    DoubleBuffer data2 = row2.getData();
    int size = row1.size();
    for (int i = 0; i < size; i++) {
        if (Math.abs(data1.get(i) - data2.get(i)) > 1e-10) {
            isEqual = 0.0;
            break;
        }
    }
    return isEqual;
}
Also used : DoubleBuffer(java.nio.DoubleBuffer)

Aggregations

DoubleBuffer (java.nio.DoubleBuffer)162 ByteBuffer (java.nio.ByteBuffer)39 FloatBuffer (java.nio.FloatBuffer)26 IntBuffer (java.nio.IntBuffer)25 ShortBuffer (java.nio.ShortBuffer)22 LongBuffer (java.nio.LongBuffer)14 CharBuffer (java.nio.CharBuffer)11 BufferOverflowException (java.nio.BufferOverflowException)8 IOException (java.io.IOException)5 BufferUnderflowException (java.nio.BufferUnderflowException)5 ArrayNode (com.fasterxml.jackson.databind.node.ArrayNode)4 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)4 ServerDenseDoubleRow (com.tencent.angel.ps.impl.matrix.ServerDenseDoubleRow)4 Test (org.junit.Test)4 InvalidMarkException (java.nio.InvalidMarkException)3 Random (java.util.Random)3 BytePointer (org.bytedeco.javacpp.BytePointer)3 DoublePointer (org.bytedeco.javacpp.DoublePointer)3 FloatPointer (org.bytedeco.javacpp.FloatPointer)3 IntPointer (org.bytedeco.javacpp.IntPointer)3