org.bytedeco.javacpp.IntPointer Java Examples

The following examples show how to use org.bytedeco.javacpp.IntPointer. 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: EagerOperationBuilder.java    From java with Apache License 2.0 6 votes vote down vote up
private static TFE_TensorHandle[] execute(TFE_Op opHandle, EagerSession session) {
  requireOp(opHandle);
  try (PointerScope scope = new PointerScope()) {
    IntPointer numRetvals = new IntPointer(1).put(MAX_OUTPUTS_PER_OP);
    PointerPointer<TFE_TensorHandle> retvals = new PointerPointer<TFE_TensorHandle>(MAX_OUTPUTS_PER_OP);
    TF_Status status = TF_Status.newStatus();
    TFE_Execute(opHandle, retvals, numRetvals, status);
    status.throwExceptionIfNotOK();

    TFE_TensorHandle[] rethandles = new TFE_TensorHandle[numRetvals.get()];
    for (int i = 0; i < rethandles.length; ++i) {
      rethandles[i] = retvals.get(TFE_TensorHandle.class, i).withDeallocator();
      session.attach(rethandles[i]);
    }
    return rethandles;
  }
}
 
Example #2
Source File: Codec.java    From JavaAV with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Get all supported pixel formats by this {@code Codec}. If this {@code Codec}
 * is not a video codec, then {@code null} is returned. The pixel formats are
 * ordered in ascending order.
 *
 * @return all supported pixel formats by this {@code Codec}.
 */
public PixelFormat[] getSupportedPixelFormats() {
	IntPointer formatsPointer = avCodec.pix_fmts();

	if (getType() != MediaType.VIDEO || formatsPointer == null)
		return null;

	List<PixelFormat> pixelFormats = new ArrayList<PixelFormat>();

	int format;
	int index = 0;
	while ((format = formatsPointer.get(index++)) != -1)
		pixelFormats.add(PixelFormat.byId(format));

	// ascending order
	Collections.sort(pixelFormats);

	return pixelFormats.toArray(new PixelFormat[0]);
}
 
Example #3
Source File: Codec.java    From JavaAV with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Get all supported sample rates by this {@code Codec}. If this {@code Codec}
 * is not an audio codec, then {@code null} is returned. The sample rates are
 * ordered in ascending order.
 *
 * @return all supported sample rates by this {@code Codec}.
 */
public Integer[] getSupportedSampleRates() {
	IntPointer sampleRatesPointer = avCodec.supported_samplerates();

	if (getType() != MediaType.AUDIO || sampleRatesPointer == null)
		return null;

	List<Integer> sampleRates = new ArrayList<Integer>();

	int sampleRate;
	int index = 0;
	while ((sampleRate = sampleRatesPointer.get(index++)) != 0)
		sampleRates.add(sampleRate);

	// ascending order
	Collections.sort(sampleRates);

	return sampleRates.toArray(new Integer[0]);
}
 
Example #4
Source File: Codec.java    From JavaAV with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Get all supported sample formats by this {@code Codec}. If this {@code Codec}
 * is not an audio codec, then {@code null} is returned.
 *
 * @return all supported sample formats by this {@code Codec}.
 */
public SampleFormat[] getSupportedSampleFormats() {
	IntPointer sampleFormatsPointer = avCodec.sample_fmts();

	if (getType() != MediaType.AUDIO || sampleFormatsPointer == null)
		return null;

	List<SampleFormat> sampleFormats = new ArrayList<SampleFormat>();

	int format;
	int index = 0;
	while ((format = sampleFormatsPointer.get(index++)) != -1)
		sampleFormats.add(SampleFormat.byId(format));

	return sampleFormats.toArray(new SampleFormat[0]);
}
 
Example #5
Source File: ALEMDP.java    From deeplearning4j with Apache License 2.0 6 votes vote down vote up
public ALEMDP(String romFile, boolean render, Configuration configuration) {
    this.romFile = romFile;
    this.configuration = configuration;
    this.render = render;
    ale = new ALEInterface();
    setupGame();

    // Get the vector of minimal or legal actions
    IntPointer a = (getConfiguration().minimalActionSet ? ale.getMinimalActionSet()
                    : ale.getLegalActionSet());
    actions = new int[(int)a.limit()];
    a.get(actions);

    int height = (int)ale.getScreen().height();
    int width = (int)(int)ale.getScreen().width();

    discreteSpace = new DiscreteSpace(actions.length);
    int[] shape = {3, height, width};
    observationSpace = new ArrayObservationSpace<>(shape);
    screenBuffer = new byte[shape[0] * shape[1] * shape[2]];

}
 
Example #6
Source File: JcublasLevel1.java    From nd4j with Apache License 2.0 6 votes vote down vote up
@Override
protected int idamax(long N, INDArray X, int incX) {
    if (Nd4j.dataType() != DataBuffer.Type.DOUBLE)
        logger.warn("DOUBLE imax called");

    Nd4j.getExecutioner().push();

    CudaContext ctx = allocator.getFlowController().prepareAction(null, X);
    int ret2;

    CublasPointer xCPointer = new CublasPointer(X, ctx);

    cublasHandle_t handle = ctx.getHandle();
    synchronized (handle) {
        cublasSetStream_v2(new cublasContext(handle), new CUstream_st(ctx.getOldStream()));

        IntPointer resultPointer = new IntPointer(new int[] {0});
        cublasIdamax_v2(new cublasContext(handle), (int) N, (DoublePointer) xCPointer.getDevicePointer(), incX,
                        resultPointer);
        ret2 = resultPointer.get();
    }

    allocator.registerAction(ctx, null, X);

    return ret2 - 1;
}
 
Example #7
Source File: JcublasLevel1.java    From nd4j with Apache License 2.0 6 votes vote down vote up
@Override
protected int isamax(long N, INDArray X, int incX) {
    if (Nd4j.dataType() != DataBuffer.Type.FLOAT)
        logger.warn("FLOAT iamax called");

    Nd4j.getExecutioner().push();

    CudaContext ctx = allocator.getFlowController().prepareAction(null, X);
    int ret2;

    CublasPointer xCPointer = new CublasPointer(X, ctx);

    cublasHandle_t handle = ctx.getHandle();
    synchronized (handle) {
        cublasSetStream_v2(new cublasContext(handle), new CUstream_st(ctx.getOldStream()));

        IntPointer resultPointer = new IntPointer(new int[] {0});
        cublasIsamax_v2(new cublasContext(handle), (int) N, (FloatPointer) xCPointer.getDevicePointer(), incX,
                        resultPointer);
        ret2 = resultPointer.get();
    }
    allocator.registerAction(ctx, null, X);

    return ret2 - 1;
}
 
Example #8
Source File: IntIndexer.java    From tapir with MIT License 6 votes vote down vote up
/**
 * Creates a int indexer to access efficiently the data of a pointer.
 *
 * @param pointer data to access via a buffer or to copy to an array
 * @param direct {@code true} to use a direct buffer, see {@link Indexer} for details
 * @return the new int array backed by a buffer or an array
 */
public static IntIndexer create(final IntPointer pointer, int[] sizes, int[] strides, boolean direct) {
    if (direct) {
        return new IntBufferIndexer(pointer.asBuffer(), sizes, strides);
    } else {
        final int position = pointer.position();
        int[] array = new int[pointer.limit() - position];
        pointer.get(array);
        return new IntArrayIndexer(array, sizes, strides) {
            @Override public void release() {
                pointer.position(position).put(array);
                super.release();
            }
        };
    }
}
 
Example #9
Source File: EagerSessionTest.java    From java with Apache License 2.0 6 votes vote down vote up
@Test
public void cleanupResourceInBackground() {
  try (EagerSession s = EagerSession.create()) {

    Pointer ref = new IntPointer(1024 * 1024);
    s.attach(ref);
    assertFalse(ref.isNull());
    System.gc();
    sleep(50); // allow some time to the background thread for cleaning up resources

    long before = Pointer.totalBytes();
    s.detach(ref.retainReference());
    ref = null;
    System.gc();
    sleep(50); // allow some time to the background thread for cleaning up resources
    long after = Pointer.totalBytes();
    assertEquals(4 * 1024 * 1024, before - after);
  }
}
 
Example #10
Source File: CpuLapack.java    From nd4j with Apache License 2.0 5 votes vote down vote up
@Override
public void sgetrf(int M, int N, INDArray A, INDArray IPIV, INDArray INFO) {
    int status = LAPACKE_sgetrf(getColumnOrder(A), M, N, 
        (FloatPointer)A.data().addressPointer(), 
        getLda(A), (IntPointer)IPIV.data().addressPointer()
        );
    if( status < 0 ) {
        throw new BlasException( "Failed to execute sgetrf", status ) ;
    }
}
 
Example #11
Source File: GraphOperationBuilder.java    From java with Apache License 2.0 5 votes vote down vote up
private static void setAttrShapeList(TF_OperationDescription handle, String name, long[] shapes, int[] numDims) {
  requireHandle(handle);

  try (PointerScope scope = new PointerScope()) {
    LongPointer shapesPointer = new LongPointer(shapes);
    PointerPointer<LongPointer> shapesPointers = new PointerPointer<LongPointer>(numDims.length);
    for (int i = 0; i < numDims.length; i++) {
      shapesPointers.put(i, shapesPointer);
      shapesPointer.position(shapesPointer.position() + numDims[i] * 8);
    }
    TF_SetAttrShapeList(handle, new BytePointer(name), shapesPointers, new IntPointer(numDims), numDims.length);
  }
}
 
Example #12
Source File: EagerOperationBuilder.java    From java with Apache License 2.0 5 votes vote down vote up
private static void setAttrShapeList(TFE_Op opHandle, String name, long[] shapes, int[] numDims) {
  requireOp(opHandle);
  try (PointerScope scope = new PointerScope()) {
    LongPointer shapesPointer = new LongPointer(shapes);
    PointerPointer<LongPointer> shapesPointers = new PointerPointer<LongPointer>(numDims.length);
    for (int i = 0; i < numDims.length; i++) {
      shapesPointers.put(i, shapesPointer);
      shapesPointer.position(shapesPointer.position() + numDims[i] * 8);
    }
    TF_Status status = TF_Status.newStatus();
    TFE_OpSetAttrShapeList(opHandle, new BytePointer(name), shapesPointers, new IntPointer(numDims),
                          numDims.length, status);
  }
}
 
Example #13
Source File: EagerSessionTest.java    From java with Apache License 2.0 5 votes vote down vote up
@Test
public void cleanupResourceOnSessionClose() {
  Pointer ref;
  try (EagerSession s = EagerSession.create()) {
    s.attach(ref = new IntPointer(1));
    assertFalse(ref.isNull());

    // check that reaching safe point did not release resources
    buildOp(s);
    assertFalse(ref.isNull());
  }
  assertTrue(ref.isNull());
}
 
Example #14
Source File: CpuLapack.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
@Override
public void dgetrf(int M, int N, INDArray A, INDArray IPIV, INDArray INFO) {
    int status = LAPACKE_dgetrf(getColumnOrder(A), M, N, (DoublePointer)A.data().addressPointer(), 
        getLda(A), (IntPointer)IPIV.data().addressPointer()
        );
    if( status < 0 ) {
        throw new BlasException( "Failed to execute dgetrf", status ) ;
    }
}
 
Example #15
Source File: CpuLapack.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
@Override
public void sgetrf(int M, int N, INDArray A, INDArray IPIV, INDArray INFO) {
    int status = LAPACKE_sgetrf(getColumnOrder(A), M, N, 
        (FloatPointer)A.data().addressPointer(), 
        getLda(A), (IntPointer)IPIV.data().addressPointer()
        );
    if( status < 0 ) {
        throw new BlasException( "Failed to execute sgetrf", status ) ;
    }
}
 
Example #16
Source File: CpuThreshold.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
@Override
public DataBuffer compress(DataBuffer buffer) {
    INDArray temp = Nd4j.createArrayFromShapeBuffer(buffer, Nd4j.getShapeInfoProvider().createShapeInformation(new long[]{1, buffer.length()}, buffer.dataType()).getFirst());
    MatchCondition condition = new MatchCondition(temp, Conditions.absGreaterThanOrEqual(threshold));
    int cntAbs = Nd4j.getExecutioner().exec(condition).getInt(0);


    //log.info("density ratio: {}", String.format("%.2f", cntAbs * 100.0f / buffer.length()));

    if (cntAbs < 2)
        return null;

    long originalLength = buffer.length() * Nd4j.sizeOfDataType(buffer.dataType());
    int compressedLength = cntAbs + 4;
    // first 3 elements contain header
    IntPointer pointer = new IntPointer(compressedLength);
    pointer.put(0, cntAbs);
    pointer.put(1, (int) buffer.length());
    pointer.put(2, Float.floatToIntBits(threshold));
    pointer.put(3, 0);

    CompressionDescriptor descriptor = new CompressionDescriptor();
    descriptor.setCompressedLength(compressedLength * 4); // sizeOf(INT)
    descriptor.setOriginalLength(originalLength);
    descriptor.setOriginalElementSize(Nd4j.sizeOfDataType(buffer.dataType()));
    descriptor.setNumberOfElements(buffer.length());

    descriptor.setCompressionAlgorithm(getDescriptor());
    descriptor.setCompressionType(getCompressionType());



    CompressedDataBuffer cbuff = new CompressedDataBuffer(pointer, descriptor);

    Nd4j.getNDArrayFactory().convertDataEx(getBufferTypeEx(buffer), buffer.addressPointer(), DataTypeEx.THRESHOLD, pointer, buffer.length());

    Nd4j.getAffinityManager().tagLocation(buffer, AffinityManager.Location.HOST);

    return cbuff;
}
 
Example #17
Source File: CpuFlexibleThreshold.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
@Override
public DataBuffer compress(DataBuffer buffer) {
    INDArray temp = Nd4j.createArrayFromShapeBuffer(buffer, Nd4j.getShapeInfoProvider().createShapeInformation(new long[]{1, buffer.length()}, DataType.INT).getFirst());
    double max = temp.amaxNumber().doubleValue();

    int cntAbs = temp.scan(Conditions.absGreaterThanOrEqual(max - (max * threshold))).intValue();

    long originalLength = buffer.length() * Nd4j.sizeOfDataType(buffer.dataType());
    int compressedLength = cntAbs + 4;
    // first 3 elements contain header
    IntPointer pointer = new IntPointer(compressedLength);
    pointer.put(0, cntAbs);
    pointer.put(1, (int) buffer.length());
    pointer.put(2, Float.floatToIntBits(threshold)); // please note, this value will be ovewritten anyway
    pointer.put(3, 0);

    CompressionDescriptor descriptor = new CompressionDescriptor();
    descriptor.setCompressedLength(compressedLength * 4); // sizeOf(INT)
    descriptor.setOriginalLength(originalLength);
    descriptor.setOriginalElementSize(Nd4j.sizeOfDataType(buffer.dataType()));
    descriptor.setNumberOfElements(buffer.length());

    descriptor.setCompressionAlgorithm(getDescriptor());
    descriptor.setCompressionType(getCompressionType());

    CompressedDataBuffer cbuff = new CompressedDataBuffer(pointer, descriptor);

    Nd4j.getNDArrayFactory().convertDataEx(getBufferTypeEx(buffer), buffer.addressPointer(), DataTypeEx.FTHRESHOLD, pointer, buffer.length());

    Nd4j.getAffinityManager().tagLocation(buffer, AffinityManager.Location.HOST);

    return cbuff;
}
 
Example #18
Source File: DefaultDataBufferFactory.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
/**
 * @param intPointer
 * @param length
 * @return
 */
@Override
public DataBuffer create(IntPointer intPointer, long length) {
    intPointer.capacity(length);
    intPointer.limit(length);
    intPointer.position(0);
    return new IntBuffer(intPointer, IntIndexer.create(intPointer), length);
}
 
Example #19
Source File: DefaultDataBufferFactory.java    From nd4j with Apache License 2.0 5 votes vote down vote up
/**
 * @param intPointer
 * @param length
 * @return
 */
@Override
public DataBuffer create(IntPointer intPointer, long length) {
    intPointer.capacity(length);
    intPointer.limit(length);
    intPointer.position(0);
    return new IntBuffer(intPointer, IntIndexer.create(intPointer), length);
}
 
Example #20
Source File: SparseCpuLevel2.java    From nd4j with Apache License 2.0 5 votes vote down vote up
public void dcoomv(char transA, int M, DataBuffer values, DataBuffer rowInd, DataBuffer colInd, int nnz, INDArray x, INDArray y){
    mkl_cspblas_dcoogemv(
            Character.toString(transA),
            (IntPointer) Nd4j.createBuffer(new int[]{M}).addressPointer(),
            (DoublePointer) values.addressPointer(),
            (IntPointer) rowInd.addressPointer(),
            (IntPointer) colInd.addressPointer(),
            (IntPointer) Nd4j.createBuffer(nnz).addressPointer(),
            (DoublePointer) x.data().addressPointer(),
            (DoublePointer)y.data().addressPointer());
}
 
Example #21
Source File: SparseCpuLevel2.java    From nd4j with Apache License 2.0 5 votes vote down vote up
public void scoomv(char transA, int M, DataBuffer values, DataBuffer rowInd, DataBuffer colInd, int nnz, INDArray x, INDArray y){
    mkl_cspblas_scoogemv(
            Character.toString(transA),
            (IntPointer) Nd4j.createBuffer(new int[]{M}).addressPointer(),
            (FloatPointer) values.addressPointer(),
            (IntPointer) rowInd.addressPointer(),
            (IntPointer) colInd.addressPointer(),
            (IntPointer) Nd4j.createBuffer(new int[]{nnz}).addressPointer(),
            (FloatPointer) x.data().addressPointer(),
            (FloatPointer)y.data().addressPointer());
}
 
Example #22
Source File: CpuLapack.java    From nd4j with Apache License 2.0 5 votes vote down vote up
@Override
public void dgetrf(int M, int N, INDArray A, INDArray IPIV, INDArray INFO) {
    int status = LAPACKE_dgetrf(getColumnOrder(A), M, N, (DoublePointer)A.data().addressPointer(), 
        getLda(A), (IntPointer)IPIV.data().addressPointer()
        );
    if( status < 0 ) {
        throw new BlasException( "Failed to execute dgetrf", status ) ;
    }
}
 
Example #23
Source File: CpuThreshold.java    From nd4j with Apache License 2.0 5 votes vote down vote up
@Override
public DataBuffer compress(DataBuffer buffer) {
    INDArray temp = Nd4j.createArrayFromShapeBuffer(buffer, Nd4j.getShapeInfoProvider().createShapeInformation(new int[]{1, (int) buffer.length()}).getFirst());
    MatchCondition condition = new MatchCondition(temp, Conditions.absGreaterThanOrEqual(threshold));
    int cntAbs = Nd4j.getExecutioner().exec(condition, Integer.MAX_VALUE).getInt(0);


    //log.info("density ratio: {}", String.format("%.2f", cntAbs * 100.0f / buffer.length()));

    if (cntAbs < 2)
        return null;

    long originalLength = buffer.length() * Nd4j.sizeOfDataType(buffer.dataType());
    int compressedLength = cntAbs + 4;
    // first 3 elements contain header
    IntPointer pointer = new IntPointer(compressedLength);
    pointer.put(0, cntAbs);
    pointer.put(1, (int) buffer.length());
    pointer.put(2, Float.floatToIntBits(threshold));
    pointer.put(3, 0);

    CompressionDescriptor descriptor = new CompressionDescriptor();
    descriptor.setCompressedLength(compressedLength * 4); // sizeOf(INT)
    descriptor.setOriginalLength(originalLength);
    descriptor.setOriginalElementSize(Nd4j.sizeOfDataType(buffer.dataType()));
    descriptor.setNumberOfElements(buffer.length());

    descriptor.setCompressionAlgorithm(getDescriptor());
    descriptor.setCompressionType(getCompressionType());



    CompressedDataBuffer cbuff = new CompressedDataBuffer(pointer, descriptor);

    Nd4j.getNDArrayFactory().convertDataEx(getBufferTypeEx(buffer), buffer.addressPointer(), DataBuffer.TypeEx.THRESHOLD, pointer, buffer.length());

    Nd4j.getAffinityManager().tagLocation(buffer, AffinityManager.Location.HOST);

    return cbuff;
}
 
Example #24
Source File: CpuFlexibleThreshold.java    From nd4j with Apache License 2.0 5 votes vote down vote up
@Override
public DataBuffer compress(DataBuffer buffer) {
    INDArray temp = Nd4j.createArrayFromShapeBuffer(buffer, Nd4j.getShapeInfoProvider().createShapeInformation(new int[]{1, (int) buffer.length()}).getFirst());
    double max = temp.amaxNumber().doubleValue();

    int cntAbs = temp.scan(Conditions.absGreaterThanOrEqual(max - (max * threshold))).intValue();

    long originalLength = buffer.length() * Nd4j.sizeOfDataType(buffer.dataType());
    int compressedLength = cntAbs + 4;
    // first 3 elements contain header
    IntPointer pointer = new IntPointer(compressedLength);
    pointer.put(0, cntAbs);
    pointer.put(1, (int) buffer.length());
    pointer.put(2, Float.floatToIntBits(threshold)); // please note, this value will be ovewritten anyway
    pointer.put(3, 0);

    CompressionDescriptor descriptor = new CompressionDescriptor();
    descriptor.setCompressedLength(compressedLength * 4); // sizeOf(INT)
    descriptor.setOriginalLength(originalLength);
    descriptor.setOriginalElementSize(Nd4j.sizeOfDataType(buffer.dataType()));
    descriptor.setNumberOfElements(buffer.length());

    descriptor.setCompressionAlgorithm(getDescriptor());
    descriptor.setCompressionType(getCompressionType());

    CompressedDataBuffer cbuff = new CompressedDataBuffer(pointer, descriptor);

    Nd4j.getNDArrayFactory().convertDataEx(getBufferTypeEx(buffer), buffer.addressPointer(), DataBuffer.TypeEx.FTHRESHOLD, pointer, buffer.length());

    Nd4j.getAffinityManager().tagLocation(buffer, AffinityManager.Location.HOST);

    return cbuff;
}
 
Example #25
Source File: EagerSessionTest.java    From java with Apache License 2.0 5 votes vote down vote up
@Test
public void clearedResourcesAreNotCleanedUp() {
  Pointer ref;
  try (EagerSession s = EagerSession.create()) {
    s.attach(ref = new IntPointer(1));
    s.detach(ref.retainReference());
  }
  assertFalse(ref.isNull());
}
 
Example #26
Source File: EagerSessionTest.java    From java with Apache License 2.0 5 votes vote down vote up
@Test
public void addingReferenceToClosedSessionFails() {
  EagerSession s = EagerSession.create();
  s.close();
  try {
    s.attach(new IntPointer(1));
    fail();
  } catch (IllegalStateException e) {
    // ok
  }
}
 
Example #27
Source File: CudaFlexibleThreshold.java    From nd4j with Apache License 2.0 5 votes vote down vote up
@Override
public DataBuffer compress(DataBuffer buffer) {
    INDArray temp = Nd4j.createArrayFromShapeBuffer(buffer, Nd4j.getShapeInfoProvider().createShapeInformation(new int[]{1, (int) buffer.length()}));
    double max = temp.amaxNumber().doubleValue();

    int cntAbs = temp.scan(Conditions.absGreaterThanOrEqual(max - (max * threshold))).intValue();

    long originalLength = buffer.length() * Nd4j.sizeOfDataType(buffer.dataType());
    int compressedLength = cntAbs + 3;
    // first 3 elements contain header
    IntPointer pointer = new IntPointer(compressedLength);
    pointer.put(0, cntAbs);
    pointer.put(1, (int) buffer.length());
    pointer.put(2, Float.floatToIntBits(threshold)); // please note, this value will be ovewritten anyway

    CompressionDescriptor descriptor = new CompressionDescriptor();
    descriptor.setCompressedLength(compressedLength * 4); // sizeOf(INT)
    descriptor.setOriginalLength(originalLength);
    descriptor.setOriginalElementSize(Nd4j.sizeOfDataType(buffer.dataType()));
    descriptor.setNumberOfElements(buffer.length());

    descriptor.setCompressionAlgorithm(getDescriptor());
    descriptor.setCompressionType(getCompressionType());

    CompressedDataBuffer cbuff = new CompressedDataBuffer(pointer, descriptor);

    Nd4j.getNDArrayFactory().convertDataEx(getBufferTypeEx(buffer), buffer.addressPointer(), DataBuffer.TypeEx.FTHRESHOLD, pointer, buffer.length());

    Nd4j.getAffinityManager().tagLocation(buffer, AffinityManager.Location.HOST);

    return cbuff;
}
 
Example #28
Source File: JcublasLapack.java    From deeplearning4j with Apache License 2.0 4 votes vote down vote up
@Override
public void sgetrf(int M, int N, INDArray A, INDArray IPIV, INDArray INFO) {
    INDArray a = A;
    if (Nd4j.dataType() != DataType.FLOAT)
        log.warn("FLOAT getrf called in DOUBLE environment");

    if (A.ordering() == 'c')
        a = A.dup('f');

    if (Nd4j.getExecutioner() instanceof GridExecutioner)
        ((GridExecutioner) Nd4j.getExecutioner()).flushQueue();

    // Get context for current thread
    val ctx = allocator.getDeviceContext();

    // setup the solver handles for cuSolver calls
    cusolverDnHandle_t handle = ctx.getSolverHandle();
    cusolverDnContext solverDn = new cusolverDnContext(handle);

    // synchronized on the solver
    synchronized (handle) {
        int result = cusolverDnSetStream(new cusolverDnContext(handle), new CUstream_st(ctx.getCublasStream()));
        if (result != 0)
            throw new BlasException("solverSetStream failed");

        // transfer the INDArray into GPU memory
        CublasPointer xAPointer = new CublasPointer(a, ctx);

        // this output - indicates how much memory we'll need for the real operation
        val worksizeBuffer = (BaseCudaDataBuffer) Nd4j.getDataBufferFactory().createInt(1);
        worksizeBuffer.lazyAllocateHostPointer();

        int stat = cusolverDnSgetrf_bufferSize(solverDn, M, N, (FloatPointer) xAPointer.getDevicePointer(), M,
                (IntPointer) worksizeBuffer.addressPointer() // we intentionally use host pointer here
        );

        if (stat != CUSOLVER_STATUS_SUCCESS) {
            throw new BlasException("cusolverDnSgetrf_bufferSize failed", stat);
        }

        int worksize = worksizeBuffer.getInt(0);
        // Now allocate memory for the workspace, the permutation matrix and a return code
        Pointer workspace = new Workspace(worksize * Nd4j.sizeOfDataType());

        // Do the actual LU decomp
        stat = cusolverDnSgetrf(solverDn, M, N, (FloatPointer) xAPointer.getDevicePointer(), M,
                new CudaPointer(workspace).asFloatPointer(),
                new CudaPointer(allocator.getPointer(IPIV, ctx)).asIntPointer(),
                new CudaPointer(allocator.getPointer(INFO, ctx)).asIntPointer());

        // we do sync to make sure getrf is finished
        //ctx.syncOldStream();

        if (stat != CUSOLVER_STATUS_SUCCESS) {
            throw new BlasException("cusolverDnSgetrf failed", stat);
        }
    }
    allocator.registerAction(ctx, a);
    allocator.registerAction(ctx, INFO);
    allocator.registerAction(ctx, IPIV);

    if (a != A)
        A.assign(a);
}
 
Example #29
Source File: BGR_Histogram_ComparisonJ_.java    From IJ-OpenCV with GNU General Public License v3.0 4 votes vote down vote up
@Override
public void run() {
    int stacksize = imp.getStack().getSize();

    if (imp.getStack().getSize() == 1) {
        IJ.error("You need a stack of images");
        return;
    }

    // Converter
    ImagePlusMatVectorConverter isc = new ImagePlusMatVectorConverter();
    opencv_core.MatVector mvec = isc.convert(imp,MatVector.class);

    if (!showDialog()) {
        return;
    }

    double[][] comparison = new double[4][stacksize * (stacksize - 1) / 2];

    opencv_core.Mat mask = new opencv_core.Mat();
    IntPointer intPtrChannels = new IntPointer(0, 1, 2);
    IntPointer intPtrHistSize = new IntPointer(blueBins, greenBins, redBins);
    FloatPointer fltPtrRanges = new FloatPointer(0.0f, 255.0f, 0.0f, 255.0f, 0.0f, 255.0f);
    PointerPointer ptptranges = new PointerPointer(fltPtrRanges, fltPtrRanges, fltPtrRanges);

    opencv_core.Mat hist1 = new opencv_core.Mat();
    opencv_core.Mat hist2 = new opencv_core.Mat();
    int n = 0;
    for (int i = 0; i < mvec.size() - 1; i++) {
        calcHist(mvec.get(i), 1, intPtrChannels, mask, hist1, 3, intPtrHistSize, ptptranges, true, false);
        opencv_core.normalize(hist1, hist1);
        for (int j = i + 1; j < mvec.size(); j++) {
            calcHist(mvec.get(j), 1, intPtrChannels, mask, hist2, 3, intPtrHistSize, ptptranges, true, false);
            opencv_core.normalize(hist2, hist2);
            comparison[0][n] = opencv_imgproc.compareHist(hist1, hist2, opencv_imgproc.CV_COMP_CORREL);
            comparison[1][n] = opencv_imgproc.compareHist(hist1, hist2, opencv_imgproc.CV_COMP_CHISQR);
            comparison[2][n] = opencv_imgproc.compareHist(hist1, hist2, opencv_imgproc.CV_COMP_INTERSECT);
            comparison[3][n] = opencv_imgproc.compareHist(hist1, hist2, opencv_imgproc.CV_COMP_BHATTACHARYYA);
            n++;
        }
    }

    String headings = "Method\t";
    for (int i = 0; i < mvec.size() - 1; i++) {
        for (int j = i + 1; j < mvec.size(); j++) {
            headings = headings + (i + 1) + "-" + (j + 1) + "\t";
        }
    }
    headings = headings.substring(0, headings.lastIndexOf("\t"));
    ArrayList list = new ArrayList();

    String row1 = "Correlation\t";
    String row2 = "CHI Square\t";
    String row3 = "Intersection\t";
    String row4 = "BHATTACHARYYA\t";
    for (int i = 0; i < comparison[0].length - 1; i++) {
        row1 = row1 + comparison[0][i] + "\t";
        row2 = row2 + comparison[1][i] + "\t";
        row3 = row3 + comparison[2][i] + "\t";
        row4 = row4 + comparison[3][i] + "\t";

    }
    row1 = row1 + comparison[0][comparison[0].length - 1] ;
    row2 = row2 + comparison[1][comparison[0].length - 1];
    row3 = row3 + comparison[2][comparison[0].length - 1];
    row4 = row4 + comparison[3][comparison[0].length - 1];
    
    list.add(row1);
    list.add(row2);
    list.add(row3);
    list.add(row4);

    TextWindow textWindow = new TextWindow("Similarity Table", headings, list, 600, 400);
    textWindow.setVisible(true);

}
 
Example #30
Source File: measureOxidationJ_.java    From IJ-OpenCV with GNU General Public License v3.0 4 votes vote down vote up
public double computeOxidation(ImagePlus imp, OvalRoi or) throws Exception {

        ImagePlusMatConverter ic  = new ImagePlusMatConverter();
        OvalRoiCircleCVConverter cc = new OvalRoiCircleCVConverter();
        
        opencv_core.Mat newImage = ic.convert(imp,Mat.class);
        // Create mask
        opencv_core.Mat mask = new opencv_core.Mat(newImage.size(), CV_8UC1, opencv_core.Scalar.all(0));
        CircleCV c = cc.convert(or,CircleCV.class);

        opencv_imgproc.circle(mask, c.getCenter(), c.getRadius(), new opencv_core.Scalar(255, 255, 255, 0), -1, 8, 0);

        // Image in LUV colorspace
        opencv_core.Mat imageLUV = new opencv_core.Mat();
        opencv_imgproc.cvtColor(newImage, imageLUV, opencv_imgproc.COLOR_BGR2Luv);

        opencv_core.Mat hist = new opencv_core.Mat();
        opencv_core.Mat blackHist = new opencv_core.Mat();

        // Since C++ `calcHist` is using arrays of arrays we need wrap to do some wrapping
        // in `IntPointer` and `PointerPointer` objects.
        int[] channels = {0, 1, 2};
        int[] bins = {8, 8, 8};
        float[] ranges = {0.0f, 255.0f};
        IntPointer intPtrChannels = new IntPointer(0, 1, 2);
        IntPointer intPtrHistSize = new IntPointer(8, 8, 8);
        FloatPointer fltPtrRanges = new FloatPointer(0.0f, 255.0f, 0.0f, 255.0f, 0.0f, 255.0f);
        PointerPointer ptptranges = new PointerPointer(fltPtrRanges, fltPtrRanges, fltPtrRanges);

        opencv_imgproc.calcHist(imageLUV, 1, intPtrChannels, mask, hist, 3, intPtrHistSize, ptptranges, true, false);
        opencv_core.normalize(hist, hist);

        opencv_core.Mat newImage2 = new opencv_core.Mat(newImage.size(), CV_8UC1, opencv_core.Scalar.all(0));
        opencv_imgproc.cvtColor(newImage2, newImage2, opencv_imgproc.COLOR_GRAY2BGR);

        opencv_imgproc.calcHist(newImage2, 1, intPtrChannels, mask, blackHist, 3, intPtrHistSize, ptptranges, true, false);
        opencv_core.normalize(blackHist, blackHist);

        return opencv_imgproc.compareHist(hist, blackHist, opencv_imgproc.CV_COMP_CHISQR);

    }