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

The following examples show how to use org.nd4j.linalg.api.buffer.DataBuffer#setConstant() . 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: Shape.java    From nd4j with Apache License 2.0 6 votes vote down vote up
/**
 * Creates the shape information buffer
 * given the shape,stride
 * @param shape the shape for the buffer
 * @param stride the stride for the buffer
 * @param offset the offset for the buffer
 * @param elementWiseStride the element wise stride for the buffer
 * @param order the order for the buffer
 * @return the shape information buffer given the parameters
 */
public static DataBuffer createShapeInformation(int[] shape, int[] stride, long offset, int elementWiseStride, char order) {
    if (shape.length != stride.length)
        throw new IllegalStateException("Shape and stride must be the same length");

    int rank = shape.length;
    int shapeBuffer[] = new int[rank * 2 + 4];
    shapeBuffer[0] = rank;
    int count = 1;
    for (int e = 0; e < shape.length; e++)
        shapeBuffer[count++] = shape[e];

    for (int e = 0; e < stride.length; e++)
        shapeBuffer[count++] = stride[e];

    shapeBuffer[count++] = (int) offset;
    shapeBuffer[count++] = elementWiseStride;
    shapeBuffer[count] = (int) order;

    DataBuffer ret = Nd4j.createBufferDetached(shapeBuffer);
    ret.setConstant(true);

    return ret;
}
 
Example 2
Source File: Shape.java    From nd4j with Apache License 2.0 6 votes vote down vote up
public static DataBuffer createShapeInformation(long[] shape, long[] stride, long offset, long elementWiseStride, char order) {
    offset = 0;

    if (shape.length != stride.length)
        throw new IllegalStateException("Shape and stride must be the same length");

    int rank = shape.length;
    long shapeBuffer[] = new long[rank * 2 + 4];
    shapeBuffer[0] = rank;
    int count = 1;
    for (int e = 0; e < shape.length; e++)
        shapeBuffer[count++] = shape[e];

    for (int e = 0; e < stride.length; e++)
        shapeBuffer[count++] = stride[e];

    shapeBuffer[count++] = (int) offset;
    shapeBuffer[count++] = elementWiseStride;
    shapeBuffer[count] = (int) order;

    DataBuffer ret = Nd4j.createBufferDetached(shapeBuffer);
    ret.setConstant(true);

    return ret;
}
 
Example 3
Source File: ProtectedCudaConstantHandler.java    From nd4j with Apache License 2.0 5 votes vote down vote up
/**
 * This method returns DataBuffer with contant equal to input array.
 *
 * PLEASE NOTE: This method assumes that you'll never ever change values within result DataBuffer
 *
 * @param array
 * @return
 */
@Override
public DataBuffer getConstantBuffer(int[] array) {
    //  logger.info("getConstantBuffer(int[]) called");
    ArrayDescriptor descriptor = new ArrayDescriptor(array);

    Integer deviceId = AtomicAllocator.getInstance().getDeviceId();

    ensureMaps(deviceId);

    if (!buffersCache.get(deviceId).containsKey(descriptor)) {
        // we create new databuffer
        //logger.info("Creating new constant buffer...");
        DataBuffer buffer = Nd4j.createBufferDetached(array);

        if (constantOffsets.get(deviceId).get() + (array.length * 4) < MAX_CONSTANT_LENGTH) {
            buffer.setConstant(true);
            // now we move data to constant memory, and keep happy
            moveToConstantSpace(buffer);

            buffersCache.get(deviceId).put(descriptor, buffer);

            bytes.addAndGet(array.length * 4);
        }
        return buffer;
    } //else logger.info("Reusing constant buffer...");

    return buffersCache.get(deviceId).get(descriptor);
}
 
Example 4
Source File: ProtectedCudaConstantHandler.java    From nd4j with Apache License 2.0 5 votes vote down vote up
@Override
public DataBuffer getConstantBuffer(long[] array) {
    //  logger.info("getConstantBuffer(int[]) called");
    ArrayDescriptor descriptor = new ArrayDescriptor(array);

    Integer deviceId = AtomicAllocator.getInstance().getDeviceId();

    ensureMaps(deviceId);

    if (!buffersCache.get(deviceId).containsKey(descriptor)) {
        // we create new databuffer
        //logger.info("Creating new constant buffer...");
        DataBuffer buffer = Nd4j.createBufferDetached(array);

        if (constantOffsets.get(deviceId).get() + (array.length * 8) < MAX_CONSTANT_LENGTH) {
            buffer.setConstant(true);
            // now we move data to constant memory, and keep happy
            moveToConstantSpace(buffer);

            buffersCache.get(deviceId).put(descriptor, buffer);

            bytes.addAndGet(array.length * 8);
        }
        return buffer;
    } //else logger.info("Reusing constant buffer...");

    return buffersCache.get(deviceId).get(descriptor);
}
 
Example 5
Source File: ProtectedCudaConstantHandler.java    From nd4j with Apache License 2.0 5 votes vote down vote up
/**
 * This method returns DataBuffer with contant equal to input array.
 *
 * PLEASE NOTE: This method assumes that you'll never ever change values within result DataBuffer
 *
 * @param array
 * @return
 */
@Override
public DataBuffer getConstantBuffer(float[] array) {
    //   logger.info("getConstantBuffer(float[]) called");
    ArrayDescriptor descriptor = new ArrayDescriptor(array);

    Integer deviceId = AtomicAllocator.getInstance().getDeviceId();

    ensureMaps(deviceId);

    if (!buffersCache.get(deviceId).containsKey(descriptor)) {
        // we create new databuffer
             //logger.info("Creating new constant buffer...");
        DataBuffer buffer = Nd4j.createBufferDetached(array);

        if (constantOffsets.get(deviceId).get() + (array.length * Nd4j.sizeOfDataType()) < MAX_CONSTANT_LENGTH) {
            buffer.setConstant(true);
            // now we move data to constant memory, and keep happy
            moveToConstantSpace(buffer);

            buffersCache.get(deviceId).put(descriptor, buffer);

            bytes.addAndGet(array.length * Nd4j.sizeOfDataType());
        }
        return buffer;
    } // else logger.info("Reusing constant buffer...");

    return buffersCache.get(deviceId).get(descriptor);
}
 
Example 6
Source File: ProtectedCudaConstantHandler.java    From nd4j with Apache License 2.0 5 votes vote down vote up
/**
 * This method returns DataBuffer with contant equal to input array.
 *
 * PLEASE NOTE: This method assumes that you'll never ever change values within result DataBuffer
 *
 * @param array
 * @return
 */
@Override
public DataBuffer getConstantBuffer(double[] array) {
            //logger.info("getConstantBuffer(double[]) called: {}", Arrays.toString(array));
    ArrayDescriptor descriptor = new ArrayDescriptor(array);

    Integer deviceId = AtomicAllocator.getInstance().getDeviceId();

    ensureMaps(deviceId);

    if (!buffersCache.get(deviceId).containsKey(descriptor)) {
        // we create new databuffer
        //logger.info("Creating new constant buffer...");
        DataBuffer buffer = Nd4j.createBufferDetached(array);

        if (constantOffsets.get(deviceId).get() + (array.length * Nd4j.sizeOfDataType()) < MAX_CONSTANT_LENGTH) {
            buffer.setConstant(true);
            // now we move data to constant memory, and keep happy
            moveToConstantSpace(buffer);

            buffersCache.get(deviceId).put(descriptor, buffer);

            bytes.addAndGet(array.length * Nd4j.sizeOfDataType());
        }
        return buffer;
    } //else logger.info("Reusing constant buffer...");

    return buffersCache.get(deviceId).get(descriptor);
}
 
Example 7
Source File: BaseShapeInfoProvider.java    From nd4j with Apache License 2.0 5 votes vote down vote up
@Override
public Pair<DataBuffer, long[]> createShapeInformation(int[] shape, int[] stride, long offset, int elementWiseStride,
                char order) {
    DataBuffer buffer = Shape.createShapeInformation(ArrayUtil.toLongArray(shape), ArrayUtil.toLongArray(stride), offset, (long) elementWiseStride, order);
    buffer.setConstant(true);
    return Pair.create(buffer, buffer.asLong());
}
 
Example 8
Source File: BaseShapeInfoProvider.java    From nd4j with Apache License 2.0 4 votes vote down vote up
@Override
public Pair<DataBuffer, long[]> createShapeInformation(long[] shape, long[] stride, long offset, long elementWiseStride, char order) {
    DataBuffer buffer = Shape.createShapeInformation(shape, stride, offset, elementWiseStride, order);
    buffer.setConstant(true);
    return Pair.create(buffer, buffer.asLong());
}
 
Example 9
Source File: BaseShapeInfoProvider.java    From deeplearning4j with Apache License 2.0 4 votes vote down vote up
@Override
public Pair<DataBuffer, long[]> createShapeInformation(long[] shape, long[] stride, long elementWiseStride, char order, DataType dataType, boolean empty) {
    DataBuffer buffer = Shape.createShapeInformation(shape, stride, elementWiseStride, order, dataType, empty);
    buffer.setConstant(true);
    return Pair.create(buffer, buffer.asLong());
}
 
Example 10
Source File: BaseShapeInfoProvider.java    From deeplearning4j with Apache License 2.0 4 votes vote down vote up
@Override
public Pair<DataBuffer, long[]> createShapeInformation(long[] shape, long[] stride, long elementWiseStride, char order, long extras) {
    DataBuffer buffer = Shape.createShapeInformation(shape, stride, elementWiseStride, order, extras);
    buffer.setConstant(true);
    return Pair.create(buffer, buffer.asLong());
}