Search in sources :

Example 51 with Pointer

use of org.bytedeco.javacpp.Pointer in project nd4j by deeplearning4j.

the class GridExecutionerTest method testOpPointerizeReduce1.

/**
 * Reduce along dimensions
 *
 * @throws Exception
 */
@Test
public void testOpPointerizeReduce1() throws Exception {
    CudaGridExecutioner executioner = new CudaGridExecutioner();
    INDArray array = Nd4j.create(10, 10);
    Sum opA = new Sum(array);
    // we need exec here, to init Op.Z for specific dimension
    executioner.exec(opA, 1);
    GridPointers pointers = executioner.pointerizeOp(opA, 1);
    assertEquals(opA.opNum(), pointers.getOpNum());
    assertEquals(Op.Type.REDUCE, pointers.getType());
    CudaContext context = (CudaContext) AtomicAllocator.getInstance().getDeviceContext().getContext();
    Pointer x = AtomicAllocator.getInstance().getPointer(array, context);
    Pointer xShapeInfo = AtomicAllocator.getInstance().getPointer(array.shapeInfoDataBuffer(), context);
    Pointer z = AtomicAllocator.getInstance().getPointer(opA.z(), context);
    Pointer zShapeInfo = AtomicAllocator.getInstance().getPointer(opA.z().shapeInfoDataBuffer(), context);
    DataBuffer dimBuff = Nd4j.getConstantHandler().getConstantBuffer(new int[] { 1 });
    Pointer ptrBuff = AtomicAllocator.getInstance().getPointer(dimBuff, context);
    assertEquals(x, pointers.getX());
    assertEquals(null, pointers.getY());
    assertNotEquals(null, pointers.getZ());
    assertEquals(z, pointers.getZ());
    assertEquals(10, opA.z().length());
    assertEquals(10, pointers.getZLength());
    /*      // We dont really care about EWS here, since we're testing TAD-based operation

        assertEquals(1, pointers.getXStride());
        assertEquals(-1, pointers.getYStride());
        assertEquals(1, pointers.getZStride());
*/
    assertEquals(xShapeInfo, pointers.getXShapeInfo());
    assertEquals(null, pointers.getYShapeInfo());
    assertEquals(zShapeInfo, pointers.getZShapeInfo());
    assertEquals(ptrBuff, pointers.getDimensions());
    assertEquals(1, pointers.getDimensionsLength());
    assertNotEquals(null, pointers.getTadShape());
    assertNotEquals(null, pointers.getTadOffsets());
    assertEquals(null, pointers.getExtraArgs());
}
Also used : INDArray(org.nd4j.linalg.api.ndarray.INDArray) GridPointers(org.nd4j.linalg.api.ops.grid.GridPointers) CudaContext(org.nd4j.linalg.jcublas.context.CudaContext) Sum(org.nd4j.linalg.api.ops.impl.accum.Sum) Pointer(org.bytedeco.javacpp.Pointer) DataBuffer(org.nd4j.linalg.api.buffer.DataBuffer) Test(org.junit.Test)

Example 52 with Pointer

use of org.bytedeco.javacpp.Pointer in project nd4j by deeplearning4j.

the class CpuTADManager method getTADOnlyShapeInfo.

@Override
public Pair<DataBuffer, DataBuffer> getTADOnlyShapeInfo(INDArray array, int[] dimension) {
    if (dimension != null && dimension.length > 1)
        Arrays.sort(dimension);
    if (dimension == null || dimension.length >= 1 && dimension[0] == Integer.MAX_VALUE) {
        return new Pair<>(array.shapeInfoDataBuffer(), null);
    } else {
        TadDescriptor descriptor = new TadDescriptor(array, dimension);
        if (!cache.containsKey(descriptor)) {
            int dimensionLength = dimension.length;
            // FIXME: this is fast triage, remove it later
            // dimensionLength <= 1 ? 2 : dimensionLength;
            int targetRank = array.rank();
            long offsetLength;
            long tadLength = 1;
            for (int i = 0; i < dimensionLength; i++) {
                tadLength *= array.shape()[dimension[i]];
            }
            offsetLength = array.lengthLong() / tadLength;
            DataBuffer outputBuffer = new IntBuffer(targetRank * 2 + 4);
            DataBuffer offsetsBuffer = new LongBuffer(offsetLength);
            DataBuffer dimensionBuffer = constantHandler.getConstantBuffer(dimension);
            Pointer dimensionPointer = dimensionBuffer.addressPointer();
            Pointer xShapeInfo = array.shapeInfoDataBuffer().addressPointer();
            Pointer targetPointer = outputBuffer.addressPointer();
            Pointer offsetsPointer = offsetsBuffer.addressPointer();
            nativeOps.tadOnlyShapeInfo((IntPointer) xShapeInfo, (IntPointer) dimensionPointer, dimension.length, (IntPointer) targetPointer, new LongPointerWrapper(offsetsPointer));
            // If the line below will be uncommented, shapes from JVM will be used on native side
            // outputBuffer = array.tensorAlongDimension(0, dimension).shapeInfoDataBuffer();
            Pair<DataBuffer, DataBuffer> pair = new Pair<>(outputBuffer, offsetsBuffer);
            if (counter.get() < MAX_ENTRIES) {
                counter.incrementAndGet();
                cache.put(descriptor, pair);
                bytes.addAndGet((outputBuffer.length() * 4) + (offsetsBuffer.length() * 8));
            }
            return pair;
        }
        return cache.get(descriptor);
    }
}
Also used : LongBuffer(org.nd4j.linalg.api.buffer.LongBuffer) IntBuffer(org.nd4j.linalg.api.buffer.IntBuffer) LongPointerWrapper(org.nd4j.nativeblas.LongPointerWrapper) IntPointer(org.bytedeco.javacpp.IntPointer) Pointer(org.bytedeco.javacpp.Pointer) TadDescriptor(org.nd4j.linalg.cache.TadDescriptor) Pair(org.nd4j.linalg.primitives.Pair) DataBuffer(org.nd4j.linalg.api.buffer.DataBuffer)

Example 53 with Pointer

use of org.bytedeco.javacpp.Pointer in project nd4j by deeplearning4j.

the class TestNDArrayCreation method testBufferCreation.

@Test
public void testBufferCreation() {
    DataBuffer dataBuffer = Nd4j.createBuffer(new double[] { 1, 2 });
    Pointer pointer = dataBuffer.pointer();
    FloatPointer floatPointer = new FloatPointer(pointer);
    DataBuffer dataBuffer1 = Nd4j.createBuffer(floatPointer, 2);
    assertEquals(2, dataBuffer1.length());
    assertEquals(1.0, dataBuffer1.getDouble(0), 1e-1);
    assertEquals(2.0, dataBuffer1.getDouble(1), 1e-1);
    INDArray arr = Nd4j.create(dataBuffer1);
    System.out.println(arr);
}
Also used : INDArray(org.nd4j.linalg.api.ndarray.INDArray) FloatPointer(org.bytedeco.javacpp.FloatPointer) FloatPointer(org.bytedeco.javacpp.FloatPointer) Pointer(org.bytedeco.javacpp.Pointer) DataBuffer(org.nd4j.linalg.api.buffer.DataBuffer) Test(org.junit.Test) BaseNd4jTest(org.nd4j.linalg.BaseNd4jTest)

Example 54 with Pointer

use of org.bytedeco.javacpp.Pointer in project nd4j by deeplearning4j.

the class NativeGraphExecutioner method executeGraph.

/**
 * This method executes given graph and returns results
 *
 * @param sd
 * @return
 */
@Override
public INDArray[] executeGraph(SameDiff sd, ExecutorConfiguration configuration) {
    Map<Integer, Node> intermediate = new HashMap<>();
    ByteBuffer buffer = convertToFlatBuffers(sd, configuration, intermediate);
    BytePointer bPtr = new BytePointer(buffer);
    log.info("Buffer length: {}", buffer.limit());
    Pointer res = NativeOpsHolder.getInstance().getDeviceNativeOps().executeFlatGraphFloat(null, bPtr);
    if (res == null)
        throw new ND4JIllegalStateException("Graph execution failed");
    // FIXME: this is BAD
    PagedPointer pagedPointer = new PagedPointer(res, 1024 * 1024L);
    FlatResult fr = FlatResult.getRootAsFlatResult(pagedPointer.asBytePointer().asByteBuffer());
    log.info("VarMap: {}", sd.variableMap());
    INDArray[] results = new INDArray[fr.variablesLength()];
    for (int e = 0; e < fr.variablesLength(); e++) {
        FlatVariable var = fr.variables(e);
        log.info("Var received: id: [{}:{}/<{}>];", var.id().first(), var.id().second(), var.name());
        FlatArray ndarray = var.ndarray();
        INDArray val = Nd4j.createFromFlatArray(ndarray);
        results[e] = val;
        if (var.name() != null && sd.variableMap().containsKey(var.name())) {
            // log.info("VarName: {}; Exists: {}; NDArrayInfo: {};", var.opName(), sd.variableMap().containsKey(var.opName()), sd.getVertexToArray().containsKey(var.opName()));
            // log.info("storing: {}; array: {}", var.name(), val);
            sd.associateArrayWithVariable(val, sd.variableMap().get(var.name()));
        } else {
            // log.info("Original id: {}; out: {}; out2: {}", original, sd.getVertexIdxToInfo().get(original), graph.getVariableForVertex(original));
            if (sd.variableMap().get(var.name()) != null) {
                sd.associateArrayWithVariable(val, sd.getVariable(var.name()));
            } else {
                throw new ND4JIllegalStateException("Unknown variable received as result: [" + var.name() + "]");
            }
        }
    }
    return results;
}
Also used : HashMap(java.util.HashMap) BytePointer(org.bytedeco.javacpp.BytePointer) BytePointer(org.bytedeco.javacpp.BytePointer) PagedPointer(org.nd4j.linalg.api.memory.pointers.PagedPointer) Pointer(org.bytedeco.javacpp.Pointer) ByteBuffer(java.nio.ByteBuffer) PagedPointer(org.nd4j.linalg.api.memory.pointers.PagedPointer) FlatArray(org.nd4j.graph.FlatArray) FlatVariable(org.nd4j.graph.FlatVariable) INDArray(org.nd4j.linalg.api.ndarray.INDArray) ND4JIllegalStateException(org.nd4j.linalg.exception.ND4JIllegalStateException) FlatResult(org.nd4j.graph.FlatResult)

Example 55 with Pointer

use of org.bytedeco.javacpp.Pointer in project nd4j by deeplearning4j.

the class BasicContextPool method getDeviceBuffers.

/**
 * This method is used to allocate
 * @param context
 * @param deviceId
 */
protected void getDeviceBuffers(CudaContext context, int deviceId) {
    // ((CudaExecutioner) Nd4j.getExecutioner()).getNativeOps();
    NativeOps nativeOps = NativeOpsHolder.getInstance().getDeviceNativeOps();
    // we hardcode sizeOf to sizeOf(double)
    int sizeOf = 8;
    Pointer reductionPointer = nativeOps.mallocDevice(16385 * sizeOf * 2, new CudaPointer(deviceId), 0);
    if (reductionPointer == null)
        throw new IllegalStateException("Can't allocate [DEVICE] reduction buffer memory!");
    nativeOps.memsetAsync(reductionPointer, 0, 16385 * sizeOf * 2, 0, context.getOldStream());
    context.syncOldStream();
    Pointer allocationPointer = nativeOps.mallocDevice(1024 * 1024, new CudaPointer(deviceId), 0);
    if (allocationPointer == null)
        throw new IllegalStateException("Can't allocate [DEVICE] allocation buffer memory!");
    Pointer scalarPointer = nativeOps.mallocHost(1 * sizeOf, 0);
    if (scalarPointer == null)
        throw new IllegalStateException("Can't allocate [HOST] scalar buffer memory!");
    context.setBufferScalar(scalarPointer);
    context.setBufferAllocation(allocationPointer);
    context.setBufferReduction(reductionPointer);
    Pointer specialPointer = nativeOps.mallocDevice(1024 * 1024 * sizeOf, new CudaPointer(deviceId), 0);
    if (specialPointer == null)
        throw new IllegalStateException("Can't allocate [DEVICE] special buffer memory!");
    nativeOps.memsetAsync(specialPointer, 0, 65536 * sizeOf, 0, context.getOldStream());
    context.setBufferSpecial(specialPointer);
}
Also used : NativeOps(org.nd4j.nativeblas.NativeOps) Pointer(org.bytedeco.javacpp.Pointer) CudaPointer(org.nd4j.jita.allocator.pointers.CudaPointer) CudaPointer(org.nd4j.jita.allocator.pointers.CudaPointer)

Aggregations

Pointer (org.bytedeco.javacpp.Pointer)68 FloatPointer (org.bytedeco.javacpp.FloatPointer)33 DoublePointer (org.bytedeco.javacpp.DoublePointer)31 IntPointer (org.bytedeco.javacpp.IntPointer)29 BytePointer (org.bytedeco.javacpp.BytePointer)23 CudaContext (org.nd4j.linalg.jcublas.context.CudaContext)23 INDArray (org.nd4j.linalg.api.ndarray.INDArray)21 ShortPointer (org.bytedeco.javacpp.ShortPointer)20 CudaPointer (org.nd4j.jita.allocator.pointers.CudaPointer)19 DataBuffer (org.nd4j.linalg.api.buffer.DataBuffer)18 GridExecutioner (org.nd4j.linalg.api.ops.executioner.GridExecutioner)16 ByteBuffer (java.nio.ByteBuffer)13 PointerPointer (org.bytedeco.javacpp.PointerPointer)12 LongPointer (org.bytedeco.javacpp.LongPointer)10 CUstream_st (org.bytedeco.javacpp.cuda.CUstream_st)10 org.nd4j.jita.allocator.pointers.cuda.cusolverDnHandle_t (org.nd4j.jita.allocator.pointers.cuda.cusolverDnHandle_t)10 CublasPointer (org.nd4j.linalg.jcublas.CublasPointer)10 FunctionPointer (org.bytedeco.javacpp.FunctionPointer)9 BoolPointer (org.bytedeco.javacpp.BoolPointer)8 CLongPointer (org.bytedeco.javacpp.CLongPointer)8