Java Code Examples for org.nd4j.linalg.api.buffer.DataBuffer#getInt()

The following examples show how to use org.nd4j.linalg.api.buffer.DataBuffer#getInt() . 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: DataBufferStruct.java    From nd4j with Apache License 2.0 6 votes vote down vote up
public static int createDataBufferStruct(FlatBufferBuilder bufferBuilder,DataBuffer create) {
    bufferBuilder.prep(create.getElementSize(), (int) create.length() * create.getElementSize());
    for(int i = (int) (create.length() - 1); i >= 0; i--) {
        switch(create.dataType()) {
            case DOUBLE:
                double putDouble = create.getDouble(i);
                bufferBuilder.putDouble(putDouble);
                break;
            case FLOAT:
                float putFloat = create.getFloat(i);
                bufferBuilder.putFloat(putFloat);
                break;
            case INT:
                int putInt = create.getInt(i);
                bufferBuilder.putInt(putInt);
                break;
            case LONG:
                long putLong = create.getLong(i);
                bufferBuilder.putLong(putLong);
        }
    }

    return bufferBuilder.offset();

}
 
Example 2
Source File: TADTests.java    From deeplearning4j with Apache License 2.0 6 votes vote down vote up
/**
 * this method compares rank, shape and stride for two given shapeBuffers
 * @param shapeA
 * @param shapeB
 * @return
 */
protected boolean compareShapes(@NonNull DataBuffer shapeA, @NonNull DataBuffer shapeB) {
    if (shapeA.dataType() != DataType.INT)
        throw new IllegalStateException("ShapeBuffer should have dataType of INT");

    if (shapeA.dataType() != shapeB.dataType())
        return false;

    int rank = shapeA.getInt(0);
    if (rank != shapeB.getInt(0))
        return false;

    for (int e = 1; e <= rank * 2; e++) {
        if (shapeA.getInt(e) != shapeB.getInt(e))
            return false;
    }

    return true;
}
 
Example 3
Source File: NativeOpExecutioner.java    From nd4j with Apache License 2.0 6 votes vote down vote up
@Override
public INDArray thresholdDecode(INDArray encoded, INDArray target) {
    DataBuffer buffer = encoded.data();

    if (buffer.dataType() != DataBuffer.Type.INT)
        throw new ND4JIllegalStateException("thresholdEncoded array should have dataType of INT");

    long compressedLength = buffer.getInt(0);
    long originalLength = buffer.getInt(1);
    float threshold = buffer.getInt(2);

    if (target.lengthLong() != originalLength)
        throw new ND4JIllegalStateException("originalLength ["+ originalLength+"] stored in encoded array doesn't match target length ["+ target.lengthLong()+"]");

    DataBuffer.TypeEx typeDst = AbstractCompressor.getBufferTypeEx(target.data());

    loop.convertTypes(null, DataBuffer.TypeEx.THRESHOLD.ordinal(), buffer.addressPointer(), target.length(), typeDst.ordinal(), target.data().addressPointer());

    return target;
}
 
Example 4
Source File: TADTests.java    From nd4j with Apache License 2.0 6 votes vote down vote up
/**
 * this method compares rank, shape and stride for two given shapeBuffers
 * @param shapeA
 * @param shapeB
 * @return
 */
protected boolean compareShapes(@NonNull DataBuffer shapeA, @NonNull DataBuffer shapeB) {
    if (shapeA.dataType() != DataBuffer.Type.INT)
        throw new IllegalStateException("ShapeBuffer should have dataType of INT");

    if (shapeA.dataType() != shapeB.dataType())
        return false;

    int rank = shapeA.getInt(0);
    if (rank != shapeB.getInt(0))
        return false;

    for (int e = 1; e <= rank * 2; e++) {
        if (shapeA.getInt(e) != shapeB.getInt(e))
            return false;
    }

    return true;
}
 
Example 5
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 6
Source File: Shape.java    From nd4j with Apache License 2.0 5 votes vote down vote up
public static int[] flags(DataBuffer buffer) {
    int length = buffer.getInt(0);
    int[] ret = new int[length];
    for (int i = 0; i < ret.length; i++)
        ret[i] = buffer.getInt(1 + i);
    return ret;
}
 
Example 7
Source File: Shape.java    From nd4j with Apache License 2.0 5 votes vote down vote up
public static int[] hiddenDimension(DataBuffer buffer) {
    int flagsLength = buffer.getInt(0);
    int offLength = buffer.getInt(flagsLength + 1);
    int hiddenDimLength = buffer.getInt(flagsLength + offLength + 2);

    int[] ret = new int[hiddenDimLength];
    for (int i = 0; i < hiddenDimLength; i++) {
        ret[i] = buffer.getInt(i + flagsLength + offLength + 3);
    }
    return ret;
}
 
Example 8
Source File: Shape.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
/**
 *
 * Compare the contents of a buffer and
 * an array for equals
 * @param arr the array
 * @param other the buffer
 * @return true if the content equals false otherwise
 */
public static boolean contentEquals(int[] arr, DataBuffer other) {
    for (int i = 0; i < arr.length; i++) {
        if (other.getInt(i) != arr[i]) {
            return false;
        }
    }
    return true;
}
 
Example 9
Source File: Shape.java    From nd4j with Apache License 2.0 5 votes vote down vote up
/**
 * Get array shape from the buffer, as an int[]
 * @param buffer    Buffer to get the shape from
 * @return          Shape array
 */
public static long[] shape(DataBuffer buffer) {
    val ret = new long[rank(buffer)];
    for (int i = 0; i < ret.length; i++)
        ret[i] = buffer.getInt(1 + i);
    return ret;
}
 
Example 10
Source File: Shape.java    From nd4j with Apache License 2.0 5 votes vote down vote up
public static int underlyingRank(DataBuffer buffer) {
    int flagsLength = buffer.getInt(0);
    int offLength = buffer.getInt(flagsLength + 1);
    int hiddenDimLength = buffer.getInt(flagsLength + offLength + 2);

    return buffer.getInt(flagsLength + offLength + hiddenDimLength + 3);
}
 
Example 11
Source File: Shape.java    From nd4j with Apache License 2.0 5 votes vote down vote up
/**
 * Returns whether the given shape is a vector
 *
 * @param shapeInfo the shapeinfo to test
 * @return whether the given shape is a vector
 */
public static boolean isVector(DataBuffer shapeInfo) {
    int rank = Shape.rank(shapeInfo);
    if (rank > 2 || rank < 1)
        return false;
    else {
        int len = Shape.length(shapeInfo);
        DataBuffer shape = Shape.shapeOf(shapeInfo);
        return shape.getInt(0) == len || shape.getInt(1) == len;
    }
}
 
Example 12
Source File: Shape.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
public static int[] hiddenDimension(DataBuffer buffer) {
    int flagsLength = buffer.getInt(0);
    int offLength = buffer.getInt(flagsLength + 1);
    int hiddenDimLength = buffer.getInt(flagsLength + offLength + 2);

    int[] ret = new int[hiddenDimLength];
    for (int i = 0; i < hiddenDimLength; i++) {
        ret[i] = buffer.getInt(i + flagsLength + offLength + 3);
    }
    return ret;
}
 
Example 13
Source File: Shape.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
/**
 * Get array shape from the buffer, as an int[]
 * @param buffer    Buffer to get the shape from
 * @return          Shape array
 */
public static long[] strideArr(DataBuffer buffer) {
    val ret = new long[rank(buffer)];
    DataBuffer stride = Shape.stride(buffer);
    for (int i = 0; i < ret.length; i++)
        ret[i] = stride.getInt(i);
    return ret;
}
 
Example 14
Source File: Shape.java    From nd4j with Apache License 2.0 5 votes vote down vote up
/**
 *
 * Compare the contents of a buffer and
 * an array for equals
 * @param arr the array
 * @param other the buffer
 * @return true if the content equals false otherwise
 */
public static boolean contentEquals(int[] arr, DataBuffer other) {
    for (int i = 0; i < arr.length; i++) {
        if (other.getInt(i) != arr[i]) {
            return false;
        }
    }
    return true;
}
 
Example 15
Source File: JcublasLapack.java    From nd4j with Apache License 2.0 4 votes vote down vote up
@Override
public void dgetrf(int M, int N, INDArray A, INDArray IPIV, INDArray INFO) {
    INDArray a = A;

    if (Nd4j.dataType() != DataBuffer.Type.DOUBLE)
        log.warn("FLOAT getrf called in FLOAT environment");

    if (A.ordering() == 'c')
        a = A.dup('f');

    if (Nd4j.getExecutioner() instanceof GridExecutioner)
        ((GridExecutioner) Nd4j.getExecutioner()).flushQueue();

    // Get context for current thread
    CudaContext ctx = (CudaContext) allocator.getDeviceContext().getContext();

    // setup the solver handles for cuSolver calls
    cusolverDnHandle_t handle = ctx.getSolverHandle();
    cusolverDnContext solverDn = new cusolverDnContext(handle);

    // synchronized on the solver
    synchronized (handle) {
        int result = cusolverDnSetStream(new cusolverDnContext(handle), new CUstream_st(ctx.getOldStream()));
        if (result != 0)
            throw new BlasException("solverSetStream failed");

        // transfer the INDArray into GPU memory
        CublasPointer xAPointer = new CublasPointer(a, ctx);

        // this output - indicates how much memory we'll need for the real operation
        DataBuffer worksizeBuffer = Nd4j.getDataBufferFactory().createInt(1);

        int stat = cusolverDnDgetrf_bufferSize(solverDn, M, N, (DoublePointer) xAPointer.getDevicePointer(), M,
                        (IntPointer) worksizeBuffer.addressPointer() // we intentionally use host pointer here
        );

        if (stat != CUSOLVER_STATUS_SUCCESS) {
            throw new BlasException("cusolverDnDgetrf_bufferSize failed", stat);
        }
        int worksize = worksizeBuffer.getInt(0);

        // Now allocate memory for the workspace, the permutation matrix and a return code
        Pointer workspace = new Workspace(worksize * Nd4j.sizeOfDataType());

        // Do the actual LU decomp
        stat = cusolverDnDgetrf(solverDn, M, N, (DoublePointer) xAPointer.getDevicePointer(), M,
                        new CudaPointer(workspace).asDoublePointer(),
                        new CudaPointer(allocator.getPointer(IPIV, ctx)).asIntPointer(),
                        new CudaPointer(allocator.getPointer(INFO, ctx)).asIntPointer());

        if (stat != CUSOLVER_STATUS_SUCCESS) {
            throw new BlasException("cusolverDnSgetrf failed", stat);
        }
    }
    allocator.registerAction(ctx, a);
    allocator.registerAction(ctx, INFO);
    allocator.registerAction(ctx, IPIV);

    if (a != A)
        A.assign(a);
}
 
Example 16
Source File: CudaThreshold.java    From nd4j with Apache License 2.0 4 votes vote down vote up
@Override
public DataBuffer decompress(DataBuffer buffer) {
    if (buffer.dataType() != DataBuffer.Type.INT)
        throw new UnsupportedOperationException();

    long compressedLength = buffer.getInt(0);
    long originalLength = buffer.getInt(1);

    DataBuffer result = Nd4j.createBuffer(originalLength);

    CudaContext context = (CudaContext) AtomicAllocator.getInstance().getDeviceContext().getContext();

    PointerPointer extras = new PointerPointer(32).put(1, context.getOldStream());

    //log.info("DEC Source length: {}", buffer.length());
    //log.info("DEC Source: {}", Arrays.toString(buffer.asInt()));

    NativeOpsHolder.getInstance().getDeviceNativeOps().decodeThresholdFloat(extras, AtomicAllocator.getInstance().getPointer(buffer), compressedLength, (FloatPointer) AtomicAllocator.getInstance().getPointer(result));
    AtomicAllocator.getInstance().getAllocationPoint(result).tickDeviceWrite();

    //DataBuffer result = Nd4j.getNDArrayFactory().convertDataEx(DataBuffer.TypeEx.THRESHOLD, buffer, getGlobalTypeEx());

    return result;
}
 
Example 17
Source File: Shape.java    From deeplearning4j with Apache License 2.0 3 votes vote down vote up
/**
 * Returns true if the given shape is of length 1
 * or provided the shape length is 2:
 * element 0 is 1
 * @param shapeInfo the shape info to check
 * @return true if the above conditions hold,false otherwise
 */
public static boolean isRowVectorShape(DataBuffer shapeInfo) {
    int rank = Shape.rank(shapeInfo);
    DataBuffer shape = Shape.shapeOf(shapeInfo);
    return (rank == 2 && shape.getInt(0) == 1) || rank == 1;

}
 
Example 18
Source File: Shape.java    From nd4j with Apache License 2.0 2 votes vote down vote up
/**
 * Get the element wise stride for the
 * shape info buffer
 * @param buffer the buffer to get the element
 *               wise stride from
 * @return the element wise stride for the buffer
 */
public static int elementWiseStride(DataBuffer buffer) {
    int length2 = shapeInfoLength(buffer.getInt(0));
    return buffer.getInt(length2 - 2);
}
 
Example 19
Source File: Shape.java    From deeplearning4j with Apache License 2.0 2 votes vote down vote up
/**
 * Get the size of the specified dimension. Identical to Shape.size(...), but does not perform any input validation
 * @param buffer       The buffer to get the shape from
 * @param dimension    The dimension to get.
 * @return             The size of the specified dimension
 */
public static int sizeUnsafe(DataBuffer buffer, int dimension) {
    return buffer.getInt(1 + dimension);
}
 
Example 20
Source File: Shape.java    From deeplearning4j with Apache License 2.0 2 votes vote down vote up
/**
 * Get the element wise stride for the
 * shape info buffer
 * @param buffer the buffer to get the element
 *               wise stride from
 * @return the element wise stride for the buffer
 */
public static int elementWiseStride(DataBuffer buffer) {
    int length2 = shapeInfoLength(buffer.getInt(0));
    return buffer.getInt(length2 - 2);
}