Search in sources :

Example 51 with BytePointer

use of org.bytedeco.javacpp.BytePointer in project nd4j by deeplearning4j.

the class Gzip method compress.

@Override
public DataBuffer compress(DataBuffer buffer) {
    try {
        ByteArrayOutputStream stream = new ByteArrayOutputStream();
        GZIPOutputStream gzip = new GZIPOutputStream(stream);
        DataOutputStream dos = new DataOutputStream(gzip);
        buffer.write(dos);
        dos.flush();
        dos.close();
        byte[] bytes = stream.toByteArray();
        // logger.info("Bytes: {}", Arrays.toString(bytes));
        BytePointer pointer = new BytePointer(bytes);
        CompressionDescriptor descriptor = new CompressionDescriptor(buffer, this);
        descriptor.setCompressedLength(bytes.length);
        CompressedDataBuffer result = new CompressedDataBuffer(pointer, descriptor);
        return result;
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
Also used : CompressionDescriptor(org.nd4j.linalg.compression.CompressionDescriptor) GZIPOutputStream(java.util.zip.GZIPOutputStream) CompressedDataBuffer(org.nd4j.linalg.compression.CompressedDataBuffer) DataOutputStream(java.io.DataOutputStream) BytePointer(org.bytedeco.javacpp.BytePointer) ByteArrayOutputStream(org.apache.commons.io.output.ByteArrayOutputStream)

Example 52 with BytePointer

use of org.bytedeco.javacpp.BytePointer in project nd4j by deeplearning4j.

the class Float8 method compressPointer.

@Override
protected CompressedDataBuffer compressPointer(DataBuffer.TypeEx srcType, Pointer srcPointer, int length, int elementSize) {
    BytePointer ptr = new BytePointer(length);
    CompressionDescriptor descriptor = new CompressionDescriptor();
    descriptor.setCompressedLength(length * 1);
    descriptor.setOriginalLength(length * elementSize);
    descriptor.setOriginalElementSize(elementSize);
    descriptor.setNumberOfElements(length);
    descriptor.setCompressionAlgorithm(getDescriptor());
    descriptor.setCompressionType(getCompressionType());
    CompressedDataBuffer buffer = new CompressedDataBuffer(ptr, descriptor);
    Nd4j.getNDArrayFactory().convertDataEx(srcType, srcPointer, DataBuffer.TypeEx.FLOAT8, ptr, length);
    return buffer;
}
Also used : CompressionDescriptor(org.nd4j.linalg.compression.CompressionDescriptor) CompressedDataBuffer(org.nd4j.linalg.compression.CompressedDataBuffer) BytePointer(org.bytedeco.javacpp.BytePointer)

Example 53 with BytePointer

use of org.bytedeco.javacpp.BytePointer in project nd4j by deeplearning4j.

the class Int16 method compressPointer.

@Override
protected CompressedDataBuffer compressPointer(DataBuffer.TypeEx srcType, Pointer srcPointer, int length, int elementSize) {
    BytePointer ptr = new BytePointer(length * 2);
    CompressionDescriptor descriptor = new CompressionDescriptor();
    descriptor.setCompressedLength(length * 2);
    descriptor.setOriginalLength(length * elementSize);
    descriptor.setOriginalElementSize(elementSize);
    descriptor.setNumberOfElements(length);
    descriptor.setCompressionAlgorithm(getDescriptor());
    descriptor.setCompressionType(getCompressionType());
    CompressedDataBuffer buffer = new CompressedDataBuffer(ptr, descriptor);
    Nd4j.getNDArrayFactory().convertDataEx(srcType, srcPointer, DataBuffer.TypeEx.INT16, ptr, length);
    return buffer;
}
Also used : CompressionDescriptor(org.nd4j.linalg.compression.CompressionDescriptor) CompressedDataBuffer(org.nd4j.linalg.compression.CompressedDataBuffer) BytePointer(org.bytedeco.javacpp.BytePointer)

Example 54 with BytePointer

use of org.bytedeco.javacpp.BytePointer in project nd4j by deeplearning4j.

the class CompressedDataBuffer method dup.

@Override
public DataBuffer dup() {
    Pointer nPtr = new BytePointer(compressionDescriptor.getCompressedLength());
    Pointer.memcpy(nPtr, pointer, compressionDescriptor.getCompressedLength());
    CompressionDescriptor nDesc = compressionDescriptor.clone();
    CompressedDataBuffer nBuf = new CompressedDataBuffer(nPtr, nDesc);
    return nBuf;
}
Also used : BytePointer(org.bytedeco.javacpp.BytePointer) BytePointer(org.bytedeco.javacpp.BytePointer) Pointer(org.bytedeco.javacpp.Pointer)

Example 55 with BytePointer

use of org.bytedeco.javacpp.BytePointer in project nd4j by deeplearning4j.

the class CompressedDataBuffer method readUnknown.

/**
 * Drop-in replacement wrapper for BaseDataBuffer.read() method, aware of CompressedDataBuffer
 * @param s
 * @return
 */
public static DataBuffer readUnknown(DataInputStream s, long length) {
    DataBuffer buffer = Nd4j.createBuffer(length);
    buffer.read(s);
    // if buffer is uncompressed, it'll be valid buffer, so we'll just return it
    if (buffer.dataType() != Type.COMPRESSED)
        return buffer;
    else {
        try {
            // if buffer is compressed one, we''ll restore and decompress it here
            String compressionAlgorithm = s.readUTF();
            long compressedLength = s.readLong();
            long originalLength = s.readLong();
            long numberOfElements = s.readLong();
            byte[] temp = new byte[(int) compressedLength];
            for (int i = 0; i < compressedLength; i++) {
                temp[i] = s.readByte();
            }
            try (Pointer pointer = new BytePointer(temp)) {
                CompressionDescriptor descriptor = new CompressionDescriptor();
                descriptor.setCompressedLength(compressedLength);
                descriptor.setCompressionAlgorithm(compressionAlgorithm);
                descriptor.setOriginalLength(originalLength);
                descriptor.setNumberOfElements(numberOfElements);
                CompressedDataBuffer compressedBuffer = new CompressedDataBuffer(pointer, descriptor);
                return Nd4j.getCompressor().decompress(compressedBuffer);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}
Also used : BytePointer(org.bytedeco.javacpp.BytePointer) BytePointer(org.bytedeco.javacpp.BytePointer) Pointer(org.bytedeco.javacpp.Pointer) IOException(java.io.IOException) DataBuffer(org.nd4j.linalg.api.buffer.DataBuffer) BaseDataBuffer(org.nd4j.linalg.api.buffer.BaseDataBuffer)

Aggregations

BytePointer (org.bytedeco.javacpp.BytePointer)79 IntPointer (org.bytedeco.javacpp.IntPointer)22 PointerPointer (org.bytedeco.javacpp.PointerPointer)20 ByteBuffer (java.nio.ByteBuffer)19 IOException (java.io.IOException)16 Pointer (org.bytedeco.javacpp.Pointer)16 PointerScope (org.bytedeco.javacpp.PointerScope)13 DoublePointer (org.bytedeco.javacpp.DoublePointer)12 FloatPointer (org.bytedeco.javacpp.FloatPointer)12 CompressedDataBuffer (org.nd4j.linalg.compression.CompressedDataBuffer)10 CompressionDescriptor (org.nd4j.linalg.compression.CompressionDescriptor)10 ShortBuffer (java.nio.ShortBuffer)9 ShortPointer (org.bytedeco.javacpp.ShortPointer)9 IntBuffer (java.nio.IntBuffer)7 DoubleBuffer (java.nio.DoubleBuffer)6 FloatBuffer (java.nio.FloatBuffer)6 Nonnull (javax.annotation.Nonnull)5 LongPointer (org.bytedeco.javacpp.LongPointer)5 TF_Status (org.tensorflow.internal.c_api.TF_Status)4 ByteOrder (java.nio.ByteOrder)3