Search in sources :

Example 96 with DataBuffer

use of org.nd4j.linalg.api.buffer.DataBuffer in project nd4j by deeplearning4j.

the class Shape method strideArr.

/**
 * Get array shape from the buffer, as an int[]
 * @param buffer    Buffer to get the shape from
 * @return          Shape array
 */
public static int[] strideArr(DataBuffer buffer) {
    int[] ret = new int[rank(buffer)];
    DataBuffer stride = Shape.stride(buffer);
    for (int i = 0; i < ret.length; i++) ret[i] = stride.getInt(i);
    return ret;
}
Also used : DataBuffer(org.nd4j.linalg.api.buffer.DataBuffer)

Example 97 with DataBuffer

use of org.nd4j.linalg.api.buffer.DataBuffer in project nd4j by deeplearning4j.

the class Shape method length.

/**
 * 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;
    DataBuffer shape = Shape.shapeOf(buffer);
    int rank = Shape.rank(buffer);
    for (int i = 0; i < rank; i++) ret *= shape.getInt(i);
    return ret;
}
Also used : DataBuffer(org.nd4j.linalg.api.buffer.DataBuffer)

Example 98 with DataBuffer

use of org.nd4j.linalg.api.buffer.DataBuffer in project nd4j by deeplearning4j.

the class ProtectedCudaConstantHandler method getConstantBuffer.

/**
 * 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;
    }
    return buffersCache.get(deviceId).get(descriptor);
}
Also used : ArrayDescriptor(org.nd4j.linalg.cache.ArrayDescriptor) DataBuffer(org.nd4j.linalg.api.buffer.DataBuffer) CudaIntDataBuffer(org.nd4j.linalg.jcublas.buffer.CudaIntDataBuffer) CudaHalfDataBuffer(org.nd4j.linalg.jcublas.buffer.CudaHalfDataBuffer) CudaFloatDataBuffer(org.nd4j.linalg.jcublas.buffer.CudaFloatDataBuffer) CudaDoubleDataBuffer(org.nd4j.linalg.jcublas.buffer.CudaDoubleDataBuffer)

Example 99 with DataBuffer

use of org.nd4j.linalg.api.buffer.DataBuffer in project nd4j by deeplearning4j.

the class ProtectedCudaConstantHandler method getConstantBuffer.

/**
 * 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;
    }
    return buffersCache.get(deviceId).get(descriptor);
}
Also used : ArrayDescriptor(org.nd4j.linalg.cache.ArrayDescriptor) DataBuffer(org.nd4j.linalg.api.buffer.DataBuffer) CudaIntDataBuffer(org.nd4j.linalg.jcublas.buffer.CudaIntDataBuffer) CudaHalfDataBuffer(org.nd4j.linalg.jcublas.buffer.CudaHalfDataBuffer) CudaFloatDataBuffer(org.nd4j.linalg.jcublas.buffer.CudaFloatDataBuffer) CudaDoubleDataBuffer(org.nd4j.linalg.jcublas.buffer.CudaDoubleDataBuffer)

Example 100 with DataBuffer

use of org.nd4j.linalg.api.buffer.DataBuffer in project nd4j by deeplearning4j.

the class SynchronousFlowController method prepareAction.

@Override
public CudaContext prepareAction(INDArray result, INDArray... operands) {
    CudaContext context = (CudaContext) allocator.getDeviceContext().getContext();
    int cId = allocator.getDeviceId();
    if (result != null) {
        Nd4j.getCompressor().autoDecompress(result);
        prepareDelayedMemory(result);
        AllocationPoint pointData = allocator.getAllocationPoint(result);
        AllocationPoint pointShape = allocator.getAllocationPoint(result.shapeInfoDataBuffer());
        pointData.acquireLock();
        if (pointData.getDeviceId() != cId && pointData.getDeviceId() >= 0 && (!CudaEnvironment.getInstance().getConfiguration().isCrossDeviceAccessAllowed() || !NativeOpsHolder.getInstance().getDeviceNativeOps().isP2PAvailable())) {
            DataBuffer buffer = result.data().originalDataBuffer() == null ? result.data() : result.data().originalDataBuffer();
            allocator.getMemoryHandler().relocateObject(buffer);
        }
        if (pointShape.getDeviceId() != cId && pointShape.getDeviceId() >= 0) {
            ((JCublasNDArray) result).setShapeInfoDataBuffer(Nd4j.getConstantHandler().relocateConstantSpace(result.shapeInfoDataBuffer()));
        }
        allocator.getAllocationPoint(result).setCurrentContext(context);
    }
    for (INDArray operand : operands) {
        if (operand == null)
            continue;
        Nd4j.getCompressor().autoDecompress(operand);
        AllocationPoint pointData = allocator.getAllocationPoint(operand);
        AllocationPoint pointShape = allocator.getAllocationPoint(operand.shapeInfoDataBuffer());
        pointData.acquireLock();
        if (pointData.getDeviceId() != cId && pointData.getDeviceId() >= 0 && (!CudaEnvironment.getInstance().getConfiguration().isCrossDeviceAccessAllowed() || !NativeOpsHolder.getInstance().getDeviceNativeOps().isP2PAvailable())) {
            DataBuffer buffer = operand.data().originalDataBuffer() == null ? operand.data() : operand.data().originalDataBuffer();
            allocator.getMemoryHandler().relocateObject(buffer);
        }
        if (pointShape.getDeviceId() != cId && pointShape.getDeviceId() >= 0) {
            ((JCublasNDArray) operand).setShapeInfoDataBuffer(Nd4j.getConstantHandler().relocateConstantSpace(operand.shapeInfoDataBuffer()));
        }
        prepareDelayedMemory(operand);
        allocator.getAllocationPoint(operand).setCurrentContext(context);
    }
    return context;
}
Also used : INDArray(org.nd4j.linalg.api.ndarray.INDArray) JCublasNDArray(org.nd4j.linalg.jcublas.JCublasNDArray) CudaContext(org.nd4j.linalg.jcublas.context.CudaContext) AllocationPoint(org.nd4j.jita.allocator.impl.AllocationPoint) AllocationPoint(org.nd4j.jita.allocator.impl.AllocationPoint) DataBuffer(org.nd4j.linalg.api.buffer.DataBuffer)

Aggregations

DataBuffer (org.nd4j.linalg.api.buffer.DataBuffer)186 INDArray (org.nd4j.linalg.api.ndarray.INDArray)79 Test (org.junit.Test)47 CompressedDataBuffer (org.nd4j.linalg.compression.CompressedDataBuffer)44 CudaContext (org.nd4j.linalg.jcublas.context.CudaContext)39 CudaPointer (org.nd4j.jita.allocator.pointers.CudaPointer)30 AllocationPoint (org.nd4j.jita.allocator.impl.AllocationPoint)25 ND4JIllegalStateException (org.nd4j.linalg.exception.ND4JIllegalStateException)23 BaseDataBuffer (org.nd4j.linalg.api.buffer.BaseDataBuffer)19 Pointer (org.bytedeco.javacpp.Pointer)18 BaseNd4jTest (org.nd4j.linalg.BaseNd4jTest)16 CudaDoubleDataBuffer (org.nd4j.linalg.jcublas.buffer.CudaDoubleDataBuffer)16 IntPointer (org.bytedeco.javacpp.IntPointer)13 PagedPointer (org.nd4j.linalg.api.memory.pointers.PagedPointer)13 CudaIntDataBuffer (org.nd4j.linalg.jcublas.buffer.CudaIntDataBuffer)13 DoublePointer (org.bytedeco.javacpp.DoublePointer)12 FloatPointer (org.bytedeco.javacpp.FloatPointer)12 GridExecutioner (org.nd4j.linalg.api.ops.executioner.GridExecutioner)12 LongPointerWrapper (org.nd4j.nativeblas.LongPointerWrapper)11 CUstream_st (org.bytedeco.javacpp.cuda.CUstream_st)10