Search in sources :

Example 31 with DataBuffer

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

the class NDArrayTestsFortran method testColumnMmul.

@Test
public void testColumnMmul() {
    DataBuffer data = Nd4j.linspace(1, 10, 18).data();
    INDArray x2 = Nd4j.create(data, new int[] { 2, 3, 3 });
    data = Nd4j.linspace(1, 12, 9).data();
    INDArray y2 = Nd4j.create(data, new int[] { 3, 3 });
    INDArray z2 = Nd4j.create(new int[] { 3, 2 }, 'f');
    z2.putColumn(0, y2.getColumn(0));
    z2.putColumn(1, y2.getColumn(1));
    INDArray nofOffset = Nd4j.create(new int[] { 3, 3 }, 'f');
    nofOffset.assign(x2.slice(0));
    assertEquals(nofOffset, x2.slice(0));
    INDArray slice = x2.slice(0);
    INDArray zeroOffsetResult = slice.mmul(z2);
    INDArray offsetResult = nofOffset.mmul(z2);
    assertEquals(zeroOffsetResult, offsetResult);
    INDArray slice1 = x2.slice(1);
    INDArray noOffset2 = Nd4j.create(slice1.shape());
    noOffset2.assign(slice1);
    assertEquals(slice1, noOffset2);
    INDArray noOffsetResult = noOffset2.mmul(z2);
    INDArray slice1OffsetResult = slice1.mmul(z2);
    assertEquals(noOffsetResult, slice1OffsetResult);
}
Also used : INDArray(org.nd4j.linalg.api.ndarray.INDArray) DataBuffer(org.nd4j.linalg.api.buffer.DataBuffer) Test(org.junit.Test)

Example 32 with DataBuffer

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

the class ProtectedCudaConstantHandler method ensureMaps.

private void ensureMaps(Integer deviceId) {
    if (!buffersCache.containsKey(deviceId)) {
        if (flowController == null)
            flowController = AtomicAllocator.getInstance().getFlowController();
        try {
            synchronized (this) {
                if (!buffersCache.containsKey(deviceId)) {
                    // TODO: this op call should be checked
                    // nativeOps.setDevice(new CudaPointer(deviceId));
                    buffersCache.put(deviceId, new ConcurrentHashMap<ArrayDescriptor, DataBuffer>());
                    constantOffsets.put(deviceId, new AtomicLong(0));
                    deviceLocks.put(deviceId, new Semaphore(1));
                    Pointer cAddr = NativeOpsHolder.getInstance().getDeviceNativeOps().getConstantSpace();
                    // logger.info("constant pointer: {}", cAddr.address() );
                    deviceAddresses.put(deviceId, cAddr);
                }
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}
Also used : AtomicLong(java.util.concurrent.atomic.AtomicLong) ArrayDescriptor(org.nd4j.linalg.cache.ArrayDescriptor) CudaPointer(org.nd4j.jita.allocator.pointers.CudaPointer) Pointer(org.bytedeco.javacpp.Pointer) Semaphore(java.util.concurrent.Semaphore) ND4JIllegalStateException(org.nd4j.linalg.exception.ND4JIllegalStateException) 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 33 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(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;
    }
    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 34 with DataBuffer

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

the class SynchronousFlowController method prepareDelayedMemory.

protected void prepareDelayedMemory(INDArray array) {
    if (configuration.getMemoryModel() == Configuration.MemoryModel.DELAYED) {
        AllocationPoint pointData = allocator.getAllocationPoint(array.shapeInfoDataBuffer());
        AllocationPoint pointShape = allocator.getAllocationPoint(array.shapeInfoDataBuffer());
        if (pointData.getAllocationStatus() != AllocationStatus.DEVICE)
            prepareDelayedMemory(array.data());
        if (pointShape.getAllocationStatus() == AllocationStatus.HOST) {
            DataBuffer oShape = array.shapeInfoDataBuffer();
            DataBuffer nShape = Nd4j.getConstantHandler().relocateConstantSpace(oShape);
            if (nShape == oShape)
                Nd4j.getConstantHandler().moveToConstantSpace(nShape);
            ((JCublasNDArray) array).setShapeInfoDataBuffer(nShape);
        }
    }
}
Also used : JCublasNDArray(org.nd4j.linalg.jcublas.JCublasNDArray) AllocationPoint(org.nd4j.jita.allocator.impl.AllocationPoint) DataBuffer(org.nd4j.linalg.api.buffer.DataBuffer)

Example 35 with DataBuffer

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

the class SynchronousFlowController method prepareActionAllWrite.

@Override
public CudaContext prepareActionAllWrite(INDArray... operands) {
    CudaContext context = (CudaContext) allocator.getDeviceContext().getContext();
    int cId = allocator.getDeviceId();
    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) {
            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