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

The following examples show how to use org.nd4j.linalg.api.buffer.DataBuffer#getLong() . 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: DataBufferStruct.java    From deeplearning4j with Apache License 2.0 6 votes vote down vote up
/**
 * Create a data buffer struct within
 * the passed in {@link FlatBufferBuilder}
 * @param bufferBuilder the existing flatbuffer
 *                      to use to serialize the {@link DataBuffer}
 * @param create the databuffer to serialize
 * @return an int representing the offset of the buffer
 */
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 3
Source File: Shape.java    From nd4j with Apache License 2.0 5 votes vote down vote up
/**
 * Gets the rank given the shape info buffer
 * @param buffer the buffer to get the rank for
 * @return the rank for the shape buffer
 */
public static int length(DataBuffer buffer) {
    int ret = 1;
    val rr = buffer.asLong();
    DataBuffer shape = Shape.shapeOf(buffer);
    int rank = Shape.rank(buffer);
    for (int i = 0; i < rank; i++)
        ret *= shape.getLong(i);
    return ret;
}
 
Example 4
Source File: Shape.java    From nd4j with Apache License 2.0 5 votes vote down vote up
public static boolean contentEquals(long[] arr, DataBuffer other) {
    for (int i = 0; i < arr.length; i++) {
        if (other.getLong(i) != arr[i]) {
            return false;
        }
    }
    return true;
}
 
Example 5
Source File: SortCooTests.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
@Test
public void sortSparseCooIndicesSort3() {
    // FIXME: we don't want this test running on cuda for now
    if (Nd4j.getExecutioner().getClass().getCanonicalName().toLowerCase().contains("cuda"))
        return;

    Random rng = Nd4j.getRandom();
    rng.setSeed(12040483421383L);
    long shape[] = {50,50,50};
    int nnz = 100;
    val indices = Nd4j.rand(new int[]{nnz, shape.length}, rng).muli(50).ravel().toLongVector();
    val values = Nd4j.rand(new long[]{nnz}).ravel().toDoubleVector();


    DataBuffer indiceBuffer = Nd4j.getDataBufferFactory().createLong(indices);
    DataBuffer valueBuffer = Nd4j.createBuffer(values);
    INDArray indMatrix = Nd4j.create(indiceBuffer).reshape(new long[]{nnz, shape.length});

    NativeOpsHolder.getInstance().getDeviceNativeOps().sortCooIndices(null, (LongPointer) indiceBuffer.addressPointer(),
            valueBuffer.addressPointer(), nnz, 3);

    for (long i = 1; i < nnz; ++i){
        for(long j = 0; j < shape.length; ++j){
            long prev = indiceBuffer.getLong(((i - 1) * shape.length + j));
            long current = indiceBuffer.getLong((i * shape.length + j));
            if (prev < current){
                break;
            } else if(prev > current){
                long[] prevRow = getLongsAt(indiceBuffer, (i - 1) * shape.length, shape.length);
                long[] currentRow = getLongsAt(indiceBuffer, i * shape.length, shape.length);
                throw new AssertionError(String.format("indices are not correctly sorted between element %d and %d. %s > %s",
                        i - 1, i, Arrays.toString(prevRow), Arrays.toString(currentRow)));
            }
        }
    }
}
 
Example 6
Source File: Shape.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
/**
 * Gets the rank given the shape info buffer
 * @param buffer the buffer to get the rank for
 * @return the rank for the shape buffer
 */
public static long length(DataBuffer buffer) {
    long ret = 1;
    val rr = buffer.asLong();
    DataBuffer shape = Shape.shapeOf(buffer);
    int rank = Shape.rank(buffer);
    for (int i = 0; i < rank; i++)
        ret *= shape.getLong(i);

    return ret;
}
 
Example 7
Source File: Shape.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
public static boolean contentEquals(long[] arr, DataBuffer other) {
    for (int i = 0; i < arr.length; i++) {
        if (other.getLong(i) != arr[i]) {
            return false;
        }
    }
    return true;
}
 
Example 8
Source File: Shape.java    From nd4j with Apache License 2.0 2 votes vote down vote up
/**
 * Get the shape from
 * the given int buffer
 * @param buffer the buffer to get the shape information for
 * @return
 */
public static DataBuffer shapeOf(DataBuffer buffer) {
    int rank = (int) buffer.getLong(0);
    return Nd4j.createBuffer(buffer, 1, rank);
}
 
Example 9
Source File: Shape.java    From deeplearning4j with Apache License 2.0 2 votes vote down vote up
/**
 * Get the shape from
 * the given int buffer
 * @param buffer the buffer to get the shape information for
 * @return
 */
public static DataBuffer shapeOf(DataBuffer buffer) {
    int rank = (int) buffer.getLong(0);
    return Nd4j.createBuffer(buffer, 1, rank);
}