Java Code Examples for org.bytedeco.javacpp.Pointer#address()

The following examples show how to use org.bytedeco.javacpp.Pointer#address() . 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: CpuMemoryManager.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
/**
 * This method returns
 * PLEASE NOTE: Cache options depend on specific implementations
 *
 * @param bytes
 * @param kind
 * @param initialize
 */
@Override
public Pointer allocate(long bytes, MemoryKind kind, boolean initialize) {
    Pointer ptr = NativeOpsHolder.getInstance().getDeviceNativeOps().mallocHost(bytes, 0);

    if (ptr == null || ptr.address() == 0L)
        throw new OutOfMemoryError("Failed to allocate [" + bytes + "] bytes");

    //log.info("Allocating {} bytes at MemoryManager", bytes);

    if (initialize)
        Pointer.memset(ptr, 0, bytes);

    return ptr;
}
 
Example 2
Source File: CpuMemoryManager.java    From nd4j with Apache License 2.0 4 votes vote down vote up
/**
 * This method returns
 * PLEASE NOTE: Cache options depend on specific implementations
 *
 * @param bytes
 * @param kind
 * @param initialize
 */
@Override
public Pointer allocate(long bytes, MemoryKind kind, boolean initialize) {
    Pointer ptr = NativeOpsHolder.getInstance().getDeviceNativeOps().mallocHost(bytes, 0);

    if (ptr == null || ptr.address() == 0L)
        throw new ND4JIllegalStateException("Failed to allocate [" + bytes + "] bytes");

    //log.info("Allocating {} bytes at MemoryManager", bytes);


    if (initialize)
        Pointer.memset(ptr, 0, bytes);

    return ptr;
}
 
Example 3
Source File: LongPointerWrapper.java    From nd4j with Apache License 2.0 4 votes vote down vote up
public LongPointerWrapper(Pointer pointer) {
    this.address = pointer.address();
    this.capacity = pointer.capacity();
    this.limit = pointer.limit();
    this.position = pointer.position();
}
 
Example 4
Source File: PythonObject.java    From deeplearning4j with Apache License 2.0 4 votes vote down vote up
public NumpyArray toNumpy() throws PythonException{
    PyObject np = PyImport_ImportModule("numpy");
    PyObject ndarray = PyObject_GetAttrString(np, "ndarray");
    if (PyObject_IsInstance(nativePythonObject, ndarray) != 1){
        throw new PythonException("Object is not a numpy array! Use Python.ndarray() to convert object to a numpy array.");
    }
    Py_DecRef(ndarray);
    Py_DecRef(np);

    Pointer objPtr = new Pointer(nativePythonObject);
    PyArrayObject npArr = new PyArrayObject(objPtr);
    Pointer ptr = PyArray_DATA(npArr);
    long[] shape = new long[PyArray_NDIM(npArr)];
    SizeTPointer shapePtr = PyArray_SHAPE(npArr);
    if (shapePtr != null)
        shapePtr.get(shape, 0, shape.length);
    long[] strides = new long[shape.length];
    SizeTPointer stridesPtr = PyArray_STRIDES(npArr);
    if (stridesPtr != null)
        stridesPtr.get(strides, 0, strides.length);
    int npdtype = PyArray_TYPE(npArr);

    DataType dtype;
    switch (npdtype){
        case NPY_DOUBLE:
            dtype = DataType.DOUBLE; break;
        case NPY_FLOAT:
            dtype = DataType.FLOAT; break;
        case NPY_SHORT:
            dtype = DataType.SHORT; break;
        case NPY_INT:
            dtype = DataType.INT32; break;
        case NPY_LONG:
            dtype = DataType.LONG; break;
        case NPY_UINT:
            dtype = DataType.UINT32; break;
        case NPY_BYTE:
            dtype = DataType.INT8; break;
        case NPY_UBYTE:
            dtype = DataType.UINT8; break;
        case NPY_BOOL:
            dtype = DataType.BOOL; break;
        case NPY_HALF:
            dtype = DataType.FLOAT16; break;
        case NPY_LONGLONG:
            dtype = DataType.INT64; break;
        case NPY_USHORT:
            dtype = DataType.UINT16; break;
        case NPY_ULONG:
        case NPY_ULONGLONG:
            dtype = DataType.UINT64; break;
        default:
                throw new PythonException("Unsupported array data type: " + npdtype);
    }

    return new NumpyArray(ptr.address(), shape, strides, dtype);

}
 
Example 5
Source File: CudaZeroHandler.java    From deeplearning4j with Apache License 2.0 4 votes vote down vote up
/**
 * Asynchronous version of memcpy
 *
 * PLEASE NOTE: This is device-dependent method, if it's not supported in your environment, blocking call will be used instead.
 *
 * @param dstBuffer
 * @param srcPointer
 * @param length
 * @param dstOffset
 */
@Override
public void memcpyAsync(DataBuffer dstBuffer, Pointer srcPointer, long length, long dstOffset) {
    if (length < 1)
        return;

    Preconditions.checkArgument(length <= (dstBuffer.length() * Nd4j.sizeOfDataType(dstBuffer.dataType())), "Length requested is bigger than target DataBuffer length");

    val point = ((BaseCudaDataBuffer) dstBuffer).getAllocationPoint();
    CudaContext tContext = null;

    if (dstBuffer.isConstant()) {
        org.bytedeco.javacpp.Pointer dstPointer = new CudaPointer(point.getHostPointer().address() + dstOffset, 0L);
        org.bytedeco.javacpp.Pointer srcPointerJ = new CudaPointer(srcPointer, length);

        val profD = PerformanceTracker.getInstance().helperStartTransaction();
        org.bytedeco.javacpp.Pointer.memcpy(dstPointer, srcPointerJ, length);
        PerformanceTracker.getInstance().helperRegisterTransaction(point.getDeviceId(), profD, point.getNumberOfBytes(), MemcpyDirection.HOST_TO_HOST);

        point.tickHostRead();
    } else {
        // if we're copying something into host memory, but we're on device - we need to provide exact copy to device as well
        Pointer rDP = new CudaPointer(point.getDevicePointer().address() + dstOffset);

        if (tContext == null)
            tContext = flowController.prepareAction(point);

        var prof = PerformanceTracker.getInstance().helperStartTransaction();

        flowController.commitTransfer(tContext.getSpecialStream());

        if (nativeOps.memcpyAsync(rDP, srcPointer, length, CudaConstants.cudaMemcpyHostToDevice, tContext.getSpecialStream()) == 0)
            throw new IllegalStateException("MemcpyAsync H2D failed: [" + srcPointer.address() + "] -> [" + rDP.address() + "]");

        flowController.commitTransfer(tContext.getSpecialStream());

        PerformanceTracker.getInstance().helperRegisterTransaction(point.getDeviceId(), prof, point.getNumberOfBytes(), MemcpyDirection.HOST_TO_DEVICE);

        flowController.registerAction(tContext, point);
        point.tickDeviceWrite();

        // we optionally copy to host memory
        if (point.getHostPointer() != null) {
            Pointer dP = new CudaPointer((point.getHostPointer().address()) + dstOffset);

            CudaContext context = flowController.prepareAction(point);
            tContext = context;

            prof = PerformanceTracker.getInstance().helperStartTransaction();

            if (nativeOps.memcpyAsync(dP, srcPointer, length, CudaConstants.cudaMemcpyHostToHost, context.getSpecialStream()) == 0)
                throw new IllegalStateException("MemcpyAsync H2H failed: [" + srcPointer.address() + "] -> [" + dP.address() + "]");

            flowController.commitTransfer(tContext.getSpecialStream());

            PerformanceTracker.getInstance().helperRegisterTransaction(point.getDeviceId(), prof, point.getNumberOfBytes(), MemcpyDirection.HOST_TO_HOST);

            if (point.getAllocationStatus() == AllocationStatus.HOST)
                flowController.registerAction(context, point);

            point.tickHostRead();
        }
    }
}
 
Example 6
Source File: LongPointerWrapper.java    From deeplearning4j with Apache License 2.0 4 votes vote down vote up
public LongPointerWrapper(Pointer pointer) {
    this.address = pointer.address();
    this.capacity = pointer.capacity();
    this.limit = pointer.limit();
    this.position = pointer.position();
}