Java Code Examples for org.nd4j.linalg.api.shape.Shape#shapeInfoLength()

The following examples show how to use org.nd4j.linalg.api.shape.Shape#shapeInfoLength() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TadDescriptor.java    From nd4j with Apache License 2.0 5 votes vote down vote up
/**
 * Obtain the values from the shape buffer
 * for the array
 * @param buffer the buffer to get the values from
 * @return the int array version of this data buffer
 */
public static long[] dataBufferToArray(DataBuffer buffer) {
    int rank = buffer.getInt(0);
    val ret = new long[Shape.shapeInfoLength(rank)];
    ret[0] = rank;
    for (int e = 1; e < Shape.shapeInfoLength(rank); e++) {
        ret[e] = buffer.getInt(e);
    }

    return ret;
}
 
Example 2
Source File: BinarySerde.java    From nd4j with Apache License 2.0 5 votes vote down vote up
/**
 * This method returns shape databuffer from saved earlier file
 *
 * @param readFrom
 * @return
 * @throws IOException
 */
public static DataBuffer readShapeFromDisk(File readFrom) throws IOException {
    try (FileInputStream os = new FileInputStream(readFrom)) {
        FileChannel channel = os.getChannel();
        // we read shapeinfo up to max_rank value, which is 32
        int len = (int) Math.min((32 * 2 + 3) * 4, readFrom.length());
        ByteBuffer buffer = ByteBuffer.allocateDirect(len);
        channel.read(buffer);

        ByteBuffer byteBuffer = buffer == null ? ByteBuffer.allocateDirect(buffer.array().length)
                .put(buffer.array()).order(ByteOrder.nativeOrder()) : buffer.order(ByteOrder.nativeOrder());

        buffer.position(0);
        int rank = byteBuffer.getInt();

        int result[] = new int[Shape.shapeInfoLength(rank)];

        // filling DataBuffer with shape info
        result[0] = rank;

        // skipping two next values (dtype and rank again)
        byteBuffer.position(12);

        // filling shape information
        for (int e = 1; e < Shape.shapeInfoLength(rank); e++) {
            result[e] = byteBuffer.getInt();
        }

        // creating nd4j databuffer now
        DataBuffer dataBuffer = Nd4j.getDataBufferFactory().createInt(result);
        return dataBuffer;
    }
}
 
Example 3
Source File: NativeOpExecutioner.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
@Override
public DataBuffer createShapeInfo(long[] shape, long[] stride, long elementWiseStride, char order, DataType dtype, boolean empty) {
    val dbf = loop.shapeBuffer(shape.length, new LongPointer(shape), new LongPointer(stride), dtype.toInt(), order, elementWiseStride, empty);
    if (loop.lastErrorCode() != 0)
        throw new RuntimeException(loop.lastErrorMessage());

    val result = new LongBuffer(loop.getConstantShapeBufferPrimary(dbf), Shape.shapeInfoLength(shape.length));

    loop.deleteConstantShapeBuffer(dbf);

    return result;
}
 
Example 4
Source File: TadDescriptor.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
/**
 * Obtain the values from the shape buffer
 * for the array
 * @param buffer the buffer to get the values from
 * @return the int array version of this data buffer
 */
public static long[] dataBufferToArray(DataBuffer buffer) {
    int rank = buffer.getInt(0);
    val ret = new long[Shape.shapeInfoLength(rank)];
    ret[0] = rank;
    for (int e = 1; e < Shape.shapeInfoLength(rank); e++) {
        ret[e] = buffer.getInt(e);
    }

    return ret;
}
 
Example 5
Source File: BinarySerde.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
/**
 * This method returns shape databuffer from saved earlier file
 *
 * @param readFrom file to read
 * @return the created databuffer,
 * @throws IOException on an I/O exception.
 */
public static DataBuffer readShapeFromDisk(File readFrom) throws IOException {
    try (FileInputStream os = new FileInputStream(readFrom)) {
        FileChannel channel = os.getChannel();
        // we read shapeinfo up to max_rank value, which is 32
        int len = (int) Math.min((32 * 2 + 3) * 8, readFrom.length());
        ByteBuffer buffer = ByteBuffer.allocateDirect(len);
        channel.read(buffer);

        ByteBuffer byteBuffer = buffer.order(ByteOrder.nativeOrder());

        ((Buffer) buffer).position(0);
        int rank = byteBuffer.getInt();

        val result = new long[Shape.shapeInfoLength(rank)];

        // filling DataBuffer with shape info
        result[0] = rank;

        // skipping two next values (dtype and rank again)
        // please , that this time rank has dtype of LONG, so takes 8 bytes.
        ((Buffer) byteBuffer).position(16);

        // filling shape information
        for (int e = 1; e < Shape.shapeInfoLength(rank); e++) {
            result[e] = byteBuffer.getLong();
        }

        // creating nd4j databuffer now
        return Nd4j.getDataBufferFactory().createLong(result);
    }
}
 
Example 6
Source File: CudaExecutioner.java    From deeplearning4j with Apache License 2.0 4 votes vote down vote up
@Override
public DataBuffer createShapeInfo(long[] shape, long[] stride, long elementWiseStride, char order, DataType dtype, boolean empty) {
    if (nativeOps.lastErrorCode() != 0)
        throw new RuntimeException(nativeOps.lastErrorMessage());

    val dbf = nativeOps.shapeBuffer(shape.length, new LongPointer(shape), new LongPointer(stride), dtype.toInt(), order, elementWiseStride, empty);

    if (nativeOps.lastErrorCode() != 0)
        throw new RuntimeException(nativeOps.lastErrorMessage());

    val result = new CudaLongDataBuffer(nativeOps.getConstantShapeBufferPrimary(dbf), nativeOps.getConstantShapeBufferSpecial(dbf), Shape.shapeInfoLength(shape.length));

    nativeOps.deleteConstantShapeBuffer(dbf);

    return result;
}
 
Example 7
Source File: ArrayOptionsHelper.java    From deeplearning4j with Apache License 2.0 4 votes vote down vote up
public static void setOptionBit(long[] storage, ArrayType type) {
    int length = Shape.shapeInfoLength(storage);
    storage[length - 3] = setOptionBit(storage[length-3], type);
}