Search in sources :

Example 26 with FloatPointer

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

the class MetaOpTests method testPooling2D.

@Test
public void testPooling2D() {
    Nd4j.create(1);
    val input = Nd4j.linspace(1, 600, 600).reshape(2, 10, 10, 3);
    val permuted = input.permute(0, 3, 1, 2);
    val nativeOps = NativeOpsHolder.getInstance().getDeviceNativeOps();
    val output = Nd4j.create(2, 3, 4, 4);
    val context = AtomicAllocator.getInstance().getFlowController().prepareAction(output, permuted);
    val ptrBIn = (FloatPointer) AtomicAllocator.getInstance().getPointer(permuted, context);
    val ptrBOut = (FloatPointer) AtomicAllocator.getInstance().getPointer(output, context);
    val ptrSIn = (IntPointer) AtomicAllocator.getInstance().getPointer(permuted.shapeInfoDataBuffer());
    val ptrSOut = (IntPointer) AtomicAllocator.getInstance().getPointer(output.shapeInfoDataBuffer());
    // kY  kX  sY  sX  pY  pX  dY  dX  N   M   P
    val bufParams = Nd4j.getConstantHandler().getConstantBuffer(new float[] { 3, 3, 3, 3, 1, 1, 1, 1, 1, 2, 2 });
    val ptrBParams = (FloatPointer) AtomicAllocator.getInstance().getPointer(bufParams, context);
    PointerPointer xShapeInfoHostPointer = // 0
    new PointerPointer(32).put(// 0
    AddressRetriever.retrieveHostPointer(permuted.shapeInfoDataBuffer()), // 1
    context.getOldStream(), // 2
    AtomicAllocator.getInstance().getDeviceIdPointer(), // 3
    context.getBufferAllocation(), // 4
    context.getBufferReduction(), // 5
    context.getBufferScalar(), // 6
    context.getBufferSpecial(), // 7
    null, // 8
    AddressRetriever.retrieveHostPointer(output.shapeInfoDataBuffer()));
    nativeOps.execTransformFloat(xShapeInfoHostPointer, 71, ptrBIn, ptrSIn, ptrBOut, ptrSOut, ptrBParams);
    AtomicAllocator.getInstance().getFlowController().registerAction(context, output, permuted);
    nativeOps.streamSynchronize(context.getOldStream());
    nativeOps.streamSynchronize(context.getOldStream());
    val reverted = output.permute(0, 2, 3, 1);
    System.out.println("Result: " + reverted.toString());
}
Also used : lombok.val(lombok.val) FloatPointer(org.bytedeco.javacpp.FloatPointer) PointerPointer(org.bytedeco.javacpp.PointerPointer) IntPointer(org.bytedeco.javacpp.IntPointer) Test(org.junit.Test)

Example 27 with FloatPointer

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

the class CpuLapack method sgeqrf.

// =========================
// Q R DECOMP
@Override
public void sgeqrf(int M, int N, INDArray A, INDArray R, INDArray INFO) {
    INDArray tau = Nd4j.create(N);
    int status = LAPACKE_sgeqrf(getColumnOrder(A), M, N, (FloatPointer) A.data().addressPointer(), getLda(A), (FloatPointer) tau.data().addressPointer());
    if (status != 0) {
        throw new BlasException("Failed to execute sgeqrf", status);
    }
    // Copy R ( upper part of Q ) into result
    if (R != null) {
        R.assign(A.get(NDArrayIndex.interval(0, A.columns()), NDArrayIndex.all()));
        INDArrayIndex[] ix = new INDArrayIndex[2];
        for (int i = 1; i < Math.min(A.rows(), A.columns()); i++) {
            ix[0] = NDArrayIndex.point(i);
            ix[1] = NDArrayIndex.interval(0, i);
            R.put(ix, 0);
        }
    }
    status = LAPACKE_sorgqr(getColumnOrder(A), M, N, N, (FloatPointer) A.data().addressPointer(), getLda(A), (FloatPointer) tau.data().addressPointer());
    if (status != 0) {
        throw new BlasException("Failed to execute sorgqr", status);
    }
}
Also used : BlasException(org.nd4j.linalg.api.blas.BlasException) INDArray(org.nd4j.linalg.api.ndarray.INDArray) FloatPointer(org.bytedeco.javacpp.FloatPointer) INDArrayIndex(org.nd4j.linalg.indexing.INDArrayIndex)

Example 28 with FloatPointer

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

the class CpuLapack method sgesvd.

// =========================
// U S V' DECOMP  (aka SVD)
@Override
public void sgesvd(byte jobu, byte jobvt, int M, int N, INDArray A, INDArray S, INDArray U, INDArray VT, INDArray INFO) {
    INDArray superb = Nd4j.create(M < N ? M : N);
    int status = LAPACKE_sgesvd(getColumnOrder(A), jobu, jobvt, M, N, (FloatPointer) A.data().addressPointer(), getLda(A), (FloatPointer) S.data().addressPointer(), U == null ? null : (FloatPointer) U.data().addressPointer(), U == null ? 1 : getLda(U), VT == null ? null : (FloatPointer) VT.data().addressPointer(), VT == null ? 1 : getLda(VT), (FloatPointer) superb.data().addressPointer());
    if (status != 0) {
        throw new BlasException("Failed to execute sgesvd", status);
    }
}
Also used : BlasException(org.nd4j.linalg.api.blas.BlasException) INDArray(org.nd4j.linalg.api.ndarray.INDArray) FloatPointer(org.bytedeco.javacpp.FloatPointer)

Example 29 with FloatPointer

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

the class FloatDataBufferTest method testPointerCreation.

@Test
public void testPointerCreation() {
    FloatPointer floatPointer = new FloatPointer(1, 2, 3, 4);
    Indexer indexer = FloatIndexer.create(floatPointer);
    DataBuffer buffer = Nd4j.createBuffer(floatPointer, DataBuffer.Type.FLOAT, 4, indexer);
    DataBuffer other = Nd4j.createBuffer(new float[] { 1, 2, 3, 4 });
    assertArrayEquals(other.asFloat(), buffer.asFloat(), 0.001f);
}
Also used : FloatIndexer(org.bytedeco.javacpp.indexer.FloatIndexer) Indexer(org.bytedeco.javacpp.indexer.Indexer) FloatPointer(org.bytedeco.javacpp.FloatPointer) Test(org.junit.Test) BaseNd4jTest(org.nd4j.linalg.BaseNd4jTest)

Example 30 with FloatPointer

use of org.bytedeco.javacpp.FloatPointer 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)

Aggregations

FloatPointer (org.bytedeco.javacpp.FloatPointer)30 CudaContext (org.nd4j.linalg.jcublas.context.CudaContext)15 CublasPointer (org.nd4j.linalg.jcublas.CublasPointer)14 IntPointer (org.bytedeco.javacpp.IntPointer)11 INDArray (org.nd4j.linalg.api.ndarray.INDArray)11 DoublePointer (org.bytedeco.javacpp.DoublePointer)9 CUstream_st (org.bytedeco.javacpp.cuda.CUstream_st)9 org.nd4j.jita.allocator.pointers.cuda.cublasHandle_t (org.nd4j.jita.allocator.pointers.cuda.cublasHandle_t)9 DataBuffer (org.nd4j.linalg.api.buffer.DataBuffer)7 Pointer (org.bytedeco.javacpp.Pointer)6 BlasException (org.nd4j.linalg.api.blas.BlasException)6 BytePointer (org.bytedeco.javacpp.BytePointer)5 ShortPointer (org.bytedeco.javacpp.ShortPointer)5 CudaPointer (org.nd4j.jita.allocator.pointers.CudaPointer)5 org.nd4j.jita.allocator.pointers.cuda.cusolverDnHandle_t (org.nd4j.jita.allocator.pointers.cuda.cusolverDnHandle_t)5 GridExecutioner (org.nd4j.linalg.api.ops.executioner.GridExecutioner)5 ByteBuffer (java.nio.ByteBuffer)4 DoubleBuffer (java.nio.DoubleBuffer)4 FloatBuffer (java.nio.FloatBuffer)4 IntBuffer (java.nio.IntBuffer)4