org.bytedeco.javacpp.Pointer Java Examples
The following examples show how to use
org.bytedeco.javacpp.Pointer.
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: LongBuffer.java From deeplearning4j with Apache License 2.0 | 6 votes |
public LongBuffer(@NonNull Pointer hostPointer, long numberOfElements) { this.allocationMode = AllocationMode.MIXED_DATA_TYPES; this.offset = 0; this.originalOffset = 0; this.underlyingLength = numberOfElements; this.length = numberOfElements; initTypeAndSize(); this.pointer = new PagedPointer(hostPointer, numberOfElements).asLongPointer(); indexer = LongIndexer.create((LongPointer) this.pointer); // we still want this buffer to have native representation ptrDataBuffer = OpaqueDataBuffer.externalizedDataBuffer(numberOfElements, DataType.INT64, this.pointer, null); Nd4j.getDeallocatorService().pickObject(this); }
Example #2
Source File: CudaDataBufferFactory.java From nd4j with Apache License 2.0 | 6 votes |
/** * Create a data buffer based on the * given pointer, data buffer opType, * and length of the buffer * * @param pointer the pointer to use * @param type the opType of buffer * @param length the length of the buffer * @param indexer * @return the data buffer * backed by this pointer with the given * opType and length. */ @Override public DataBuffer create(Pointer pointer, DataBuffer.Type type, long length, Indexer indexer) { switch (type) { case LONG: return new CudaLongDataBuffer(pointer, indexer, length); case INT: return new CudaIntDataBuffer(pointer, indexer, length); case DOUBLE: return new CudaDoubleDataBuffer(pointer, indexer, length); case FLOAT: return new CudaFloatDataBuffer(pointer, indexer, length); case HALF: return new CudaHalfDataBuffer(pointer, indexer, length); } throw new IllegalArgumentException("Illegal dtype " + type); }
Example #3
Source File: Int8.java From nd4j with Apache License 2.0 | 6 votes |
@Override protected CompressedDataBuffer compressPointer(DataBuffer.TypeEx srcType, Pointer srcPointer, int length, int elementSize) { BytePointer ptr = new BytePointer(length); CompressionDescriptor descriptor = new CompressionDescriptor(); descriptor.setCompressedLength(length * 1); descriptor.setOriginalLength(length * elementSize); descriptor.setOriginalElementSize(elementSize); descriptor.setNumberOfElements(length); descriptor.setCompressionAlgorithm(getDescriptor()); descriptor.setCompressionType(getCompressionType()); CompressedDataBuffer buffer = new CompressedDataBuffer(ptr, descriptor); Nd4j.getNDArrayFactory().convertDataEx(srcType, srcPointer, DataBuffer.TypeEx.INT8, ptr, length); return buffer; }
Example #4
Source File: CudaLongDataBuffer.java From deeplearning4j with Apache License 2.0 | 6 votes |
/** * This constructor is special one - it's used for ShapeInfo * @param hostPointer * @param devicePointer * @param numberOfElements */ public CudaLongDataBuffer(@NonNull Pointer hostPointer, @NonNull Pointer devicePointer, long numberOfElements) { super(); this.allocationMode = AllocationMode.MIXED_DATA_TYPES; this.offset = 0; this.originalOffset = 0; this.underlyingLength = numberOfElements; this.length = numberOfElements; initTypeAndSize(); // creating empty native DataBuffer and filling it with pointers ptrDataBuffer = OpaqueDataBuffer.externalizedDataBuffer(numberOfElements, DataType.INT64, hostPointer, devicePointer); // setting up java side of things this.pointer = new CudaPointer(hostPointer, numberOfElements).asLongPointer(); indexer = LongIndexer.create((LongPointer) this.pointer); this.allocationPoint = new AllocationPoint(ptrDataBuffer, numberOfElements * DataType.INT64.width()); }
Example #5
Source File: EagerSessionTest.java From java with Apache License 2.0 | 6 votes |
@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 #6
Source File: NumpyArray.java From deeplearning4j with Apache License 2.0 | 5 votes |
private void setND4JArray() { long size = 1; for (long d : shape) { size *= d; } String cacheKey = address + "_" + size + "_" + dtype + "_" + ArrayUtils.toString(strides); nd4jArray = arrayCache.get(cacheKey); if (nd4jArray == null) { Pointer ptr = nativeOps.pointerForAddress(address); ptr = ptr.limit(size); ptr = ptr.capacity(size); DataBuffer buff = Nd4j.createBuffer(ptr, size, dtype); int elemSize = buff.getElementSize(); long[] nd4jStrides = new long[strides.length]; for (int i = 0; i < strides.length; i++) { nd4jStrides[i] = strides[i] / elemSize; } nd4jArray = Nd4j.create(buff, shape, nd4jStrides, 0, Shape.getOrder(shape, nd4jStrides, 1), dtype); arrayCache.put(cacheKey, nd4jArray); } else{ if (!Arrays.equals(nd4jArray.shape(), shape)){ nd4jArray = nd4jArray.reshape(shape); } } Nd4j.getAffinityManager().ensureLocation(nd4jArray, AffinityManager.Location.HOST); }
Example #7
Source File: CudaWorkspace.java From deeplearning4j with Apache License 2.0 | 5 votes |
@Override protected void init() { if (workspaceConfiguration.getPolicyLocation() == LocationPolicy.MMAP) { throw new ND4JIllegalStateException("CUDA do not support MMAP workspaces yet"); } super.init(); if (currentSize.get() > 0) { //log.info("Allocating {} bytes at DEVICE & HOST space...", currentSize.get()); isInit.set(true); long bytes = currentSize.get(); if (isDebug.get()) log.info("Allocating [{}] workspace on device_{}, {} bytes...", id, Nd4j.getAffinityManager().getDeviceForCurrentThread(), bytes); if (isDebug.get()) { Nd4j.getWorkspaceManager().printAllocationStatisticsForCurrentThread(); } Pointer ptr = memoryManager.allocate((bytes + SAFETY_OFFSET), MemoryKind.HOST, false); if (ptr == null) throw new ND4JIllegalStateException("Can't allocate memory for workspace"); workspace.setHostPointer(new PagedPointer(ptr)); if (workspaceConfiguration.getPolicyMirroring() != MirroringPolicy.HOST_ONLY) { workspace.setDevicePointer(new PagedPointer(memoryManager.allocate((bytes + SAFETY_OFFSET), MemoryKind.DEVICE, false))); AllocationsTracker.getInstance().markAllocated(AllocationKind.GENERAL, Nd4j.getAffinityManager().getDeviceForCurrentThread(), bytes + SAFETY_OFFSET); MemoryTracker.getInstance().incrementWorkspaceAllocatedAmount(Nd4j.getAffinityManager().getDeviceForCurrentThread(), bytes + SAFETY_OFFSET); } //log.info("Workspace [{}] initialized successfully", id); } }
Example #8
Source File: CompressedDataBuffer.java From nd4j with Apache License 2.0 | 5 votes |
/** * Drop-in replacement wrapper for BaseDataBuffer.read() method, aware of CompressedDataBuffer * @param s * @return */ public static DataBuffer readUnknown(DataInputStream s, long length) { DataBuffer buffer = Nd4j.createBuffer(length); buffer.read(s); // if buffer is uncompressed, it'll be valid buffer, so we'll just return it if (buffer.dataType() != Type.COMPRESSED) return buffer; else { try { // if buffer is compressed one, we''ll restore it here String compressionAlgorithm = s.readUTF(); long compressedLength = s.readLong(); long originalLength = s.readLong(); long numberOfElements = s.readLong(); byte[] temp = new byte[(int) compressedLength]; for (int i = 0; i < compressedLength; i++) { temp[i] = s.readByte(); } Pointer pointer = new BytePointer(temp); CompressionDescriptor descriptor = new CompressionDescriptor(); descriptor.setCompressedLength(compressedLength); descriptor.setCompressionAlgorithm(compressionAlgorithm); descriptor.setOriginalLength(originalLength); descriptor.setNumberOfElements(numberOfElements); return new CompressedDataBuffer(pointer, descriptor); } catch (Exception e) { throw new RuntimeException(e); } } }
Example #9
Source File: Nd4jTest.java From deeplearning4j with Apache License 2.0 | 5 votes |
@Test @Ignore("AB 2019/05/23 - Failing on linux-x86_64-cuda-9.2 - see issue #7657") public void testNumpyConversion() throws Exception { INDArray linspace = Nd4j.linspace(1,4,4, DataType.FLOAT); Pointer convert = Nd4j.getNDArrayFactory().convertToNumpy(linspace); convert.position(0); Pointer pointer = NativeOpsHolder.getInstance().getDeviceNativeOps().loadNpyFromHeader(convert); Pointer pointer1 = NativeOpsHolder.getInstance().getDeviceNativeOps().dataPointForNumpyStruct(pointer); pointer1.capacity(linspace.data().getElementSize() * linspace.data().length()); ByteBuffer byteBuffer = linspace.data().pointer().asByteBuffer(); byte[] originalData = new byte[byteBuffer.capacity()]; byteBuffer.get(originalData); ByteBuffer floatBuffer = pointer1.asByteBuffer(); byte[] dataTwo = new byte[floatBuffer.capacity()]; floatBuffer.get(dataTwo); assertArrayEquals(originalData,dataTwo); floatBuffer.position(0); DataBuffer dataBuffer = Nd4j.createBuffer(new FloatPointer(floatBuffer.asFloatBuffer()),linspace.length(), DataType.FLOAT); assertArrayEquals(new float[]{1,2,3,4}, dataBuffer.asFloat(), 1e-5f); INDArray convertedFrom = Nd4j.getNDArrayFactory().createFromNpyHeaderPointer(convert); assertEquals(linspace,convertedFrom); File tmpFile = new File(System.getProperty("java.io.tmpdir"),"nd4j-numpy-tmp-" + UUID.randomUUID().toString() + ".bin"); tmpFile.deleteOnExit(); Nd4j.writeAsNumpy(linspace,tmpFile); INDArray numpyFromFile = Nd4j.createFromNpyFile(tmpFile); assertEquals(linspace,numpyFromFile); }
Example #10
Source File: BasicMemoryManager.java From nd4j with Apache License 2.0 | 5 votes |
@Override public void memcpy(DataBuffer dstBuffer, DataBuffer srcBuffer) { val perfD = PerformanceTracker.getInstance().helperStartTransaction(); Pointer.memcpy(dstBuffer.addressPointer(), srcBuffer.addressPointer(), srcBuffer.length() * srcBuffer.getElementSize()); PerformanceTracker.getInstance().helperRegisterTransaction(0, perfD, srcBuffer.length() * srcBuffer.getElementSize(), MemcpyDirection.HOST_TO_HOST); }
Example #11
Source File: CpuMemoryManager.java From deeplearning4j with Apache License 2.0 | 5 votes |
@Override public void memset(INDArray array) { if (array.isView()) { array.assign(0.0); return; } Pointer.memset(array.data().addressPointer(), 0, array.data().length() * Nd4j.sizeOfDataType(array.data().dataType())); }
Example #12
Source File: CompressedDataBuffer.java From deeplearning4j with Apache License 2.0 | 5 votes |
/** * Drop-in replacement wrapper for BaseDataBuffer.read() method, aware of CompressedDataBuffer * @param s * @return */ public static DataBuffer readUnknown(DataInputStream s, AllocationMode allocMode, long length, DataType type) { // if buffer is uncompressed, it'll be valid buffer, so we'll just return it if (type != DataType.COMPRESSED) { DataBuffer buffer = Nd4j.createBuffer(type, length, false); buffer.read(s, allocMode, length, type); return buffer; } else { try { // if buffer is compressed one, we''ll restore it here String compressionAlgorithm = s.readUTF(); long compressedLength = s.readLong(); long originalLength = s.readLong(); long numberOfElements = s.readLong(); DataType originalType = DataType.values()[s.readInt()]; byte[] temp = new byte[(int) compressedLength]; for (int i = 0; i < compressedLength; i++) { temp[i] = s.readByte(); } Pointer pointer = new BytePointer(temp); CompressionDescriptor descriptor = new CompressionDescriptor(); descriptor.setCompressedLength(compressedLength); descriptor.setCompressionAlgorithm(compressionAlgorithm); descriptor.setOriginalLength(originalLength); descriptor.setNumberOfElements(numberOfElements); descriptor.setOriginalDataType(originalType); return new CompressedDataBuffer(pointer, descriptor); } catch (Exception e) { throw new RuntimeException(e); } } }
Example #13
Source File: CudaZeroHandler.java From nd4j with Apache License 2.0 | 5 votes |
@Override public void memcpyDevice(DataBuffer dstBuffer, Pointer srcPointer, long length, long dstOffset, CudaContext context) { AllocationPoint point = ((BaseCudaDataBuffer) dstBuffer).getAllocationPoint(); Pointer dP = new CudaPointer((point.getPointers().getDevicePointer().address()) + dstOffset); if (nativeOps.memcpyAsync(dP, srcPointer, length, CudaConstants.cudaMemcpyDeviceToDevice, context.getOldStream()) == 0) throw new ND4JIllegalStateException("memcpyAsync failed"); point.tickDeviceWrite(); }
Example #14
Source File: Session.java From java with Apache License 2.0 | 5 votes |
private static void resolveHandles(String type, Pointer[] src, PointerPointer dst, int n) { if (src.length != n) { throw new IllegalArgumentException("expected " + n + ", got " + src.length + " " + type); } for (int i = 0; i < n; ++i) { if (src[i] == null || src[i].isNull()) { throw new IllegalStateException("invalid " + type + " (#" + i + " of " + n + ")"); } dst.put(i, src[i]); } }
Example #15
Source File: CudaZeroHandler.java From nd4j with Apache License 2.0 | 5 votes |
/** * PLEASE NOTE: This method always returns pointer within OS memory space * * @param buffer * @return */ @Override public org.bytedeco.javacpp.Pointer getHostPointer(DataBuffer buffer) { AllocationPoint dstPoint = ((BaseCudaDataBuffer) buffer).getAllocationPoint(); // return pointer with offset if needed. length is specified for constructor compatibility purposes if (dstPoint.getPointers().getHostPointer() == null) { log.info("DevicePointer: " + dstPoint.getPointers().getDevicePointer()); log.info("HostPointer: " + dstPoint.getPointers().getHostPointer()); log.info("AllocStatus: " + dstPoint.getAllocationStatus()); throw new RuntimeException("pointer is null"); } //dstPoint.tickHostWrite(); //dstPoint.tickHostRead(); //log.info("Requesting host pointer for {}", buffer); //getCudaContext().syncOldStream(); synchronizeThreadDevice(Thread.currentThread().getId(), dstPoint.getDeviceId(), dstPoint); CudaPointer p = new CudaPointer(dstPoint.getPointers().getHostPointer(), buffer.length(), (buffer.offset() * buffer.getElementSize())); switch (buffer.dataType()) { case DOUBLE: return p.asDoublePointer(); case FLOAT: return p.asFloatPointer(); case INT: return p.asIntPointer(); case HALF: return p.asShortPointer(); case LONG: return p.asLongPointer(); default: return p; } }
Example #16
Source File: TestNDArrayCreation.java From nd4j with Apache License 2.0 | 5 votes |
@Test public void testCreateNpy3() throws Exception { INDArray arrCreate = Nd4j.createFromNpyFile(new ClassPathResource("rank3.npy").getFile()); assertEquals(8, arrCreate.length()); assertEquals(3, arrCreate.rank()); Pointer pointer = NativeOpsHolder.getInstance().getDeviceNativeOps() .pointerForAddress(arrCreate.data().address()); assertEquals(arrCreate.data().address(), pointer.address()); }
Example #17
Source File: TensorBuffers.java From java with Apache License 2.0 | 5 votes |
/** * Maps tensor memory as a buffer of integers. * * @param nativeTensor native reference to the tensor * @return an int buffer */ public static IntDataBuffer toInts(TF_Tensor nativeTensor) { Pointer tensorMemory = tensorMemory(nativeTensor); if (TensorRawDataBufferFactory.canBeUsed()) { return TensorRawDataBufferFactory.mapTensorToInts(tensorMemory); } return DataBuffers.of(tensorMemory.asByteBuffer().asIntBuffer()); }
Example #18
Source File: NDArray.java From nd4j with Apache License 2.0 | 4 votes |
/** * This method does direct array copy. Impossible to use on views or mixed orders. * * PLEASE NOTE: YOU SHOULD NEVER USE THIS METHOD, UNLESS YOU 100% CLEAR ABOUT IT * * @return */ @Override public INDArray unsafeDuplication() { WorkspaceUtils.assertValidArray(this, "Cannot duplicate array"); if (isView()) return this.dup(this.ordering()); DataBuffer rb = Nd4j.getMemoryManager().getCurrentWorkspace() == null ? Nd4j.getDataBufferFactory().createSame(this.data, false) : Nd4j.getDataBufferFactory().createSame(this.data, false, Nd4j.getMemoryManager().getCurrentWorkspace()); INDArray ret = Nd4j.createArrayFromShapeBuffer(rb, this.shapeInfoDataBuffer()); val perfD = PerformanceTracker.getInstance().helperStartTransaction(); Pointer.memcpy(ret.data().addressPointer(), this.data().addressPointer(), this.data().length() * this.data().getElementSize()); PerformanceTracker.getInstance().helperRegisterTransaction(0, perfD, this.data().length() * this.data().getElementSize(), MemcpyDirection.HOST_TO_HOST); return ret; }
Example #19
Source File: PythonObject.java From deeplearning4j with Apache License 2.0 | 4 votes |
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 #20
Source File: cublasHandle_t.java From nd4j with Apache License 2.0 | 4 votes |
public cublasHandle_t(Pointer pointer) { super(pointer); }
Example #21
Source File: CpuThreshold.java From deeplearning4j with Apache License 2.0 | 4 votes |
@Override protected CompressedDataBuffer compressPointer(DataTypeEx srcType, Pointer srcPointer, int length, int elementSize) { throw new UnsupportedOperationException(); }
Example #22
Source File: DefaultOpExecutioner.java From deeplearning4j with Apache License 2.0 | 4 votes |
@Override public void registerGraph(long id, Pointer graph) { throw new UnsupportedOperationException("Not yet implemented"); }
Example #23
Source File: LayerWorkspaceMgr.java From deeplearning4j with Apache License 2.0 | 4 votes |
/** * @param helperWorkspacePointers Helper pointers - see {@link #getHelperWorkspace(String)} for details * @return Workspace manager */ public static LayerWorkspaceMgr noWorkspaces(Map<String,Pointer> helperWorkspacePointers){ LayerWorkspaceMgr wsm = noWorkspaces(); wsm.setHelperWorkspacePointers(helperWorkspacePointers); return wsm; }
Example #24
Source File: AbstractCompressor.java From deeplearning4j with Apache License 2.0 | 4 votes |
protected abstract CompressedDataBuffer compressPointer(DataTypeEx srcType, Pointer srcPointer, int length, int elementSize);
Example #25
Source File: CudaHalfDataBuffer.java From deeplearning4j with Apache License 2.0 | 4 votes |
public CudaHalfDataBuffer(Pointer pointer, Pointer specialPointer, Indexer indexer, long length){ super(pointer, specialPointer, indexer, length); }
Example #26
Source File: CudaZeroHandler.java From deeplearning4j with Apache License 2.0 | 4 votes |
/** * PLEASE NOTE: Specific implementation, on systems without special devices can return HostPointer here * * @param buffer * @return */ @Override public org.bytedeco.javacpp.Pointer getDevicePointer(DataBuffer buffer, CudaContext context) { // TODO: It would be awesome to get rid of typecasting here AllocationPoint dstPoint = ((BaseCudaDataBuffer) buffer).getAllocationPoint(); // if that's device state, we probably might want to update device memory state if (dstPoint.getAllocationStatus() == AllocationStatus.DEVICE) { if (!dstPoint.isActualOnDeviceSide()) { //relocate(AllocationStatus.HOST, AllocationStatus.DEVICE, dstPoint, dstPoint.getShape(), context); throw new UnsupportedOperationException("Pew-pew"); } } if (dstPoint.getDevicePointer() == null) return null; // return pointer. length is specified for constructor compatibility purposes. Offset is accounted at C++ side val p = new CudaPointer(dstPoint.getDevicePointer(), buffer.length(), 0); if (OpProfiler.getInstance().getConfig().isCheckLocality()) NativeOpsHolder.getInstance().getDeviceNativeOps().tryPointer(context.getOldStream(), p, 1); switch (buffer.dataType()) { case DOUBLE: return p.asDoublePointer(); case FLOAT: return p.asFloatPointer(); case UINT32: case INT: return p.asIntPointer(); case SHORT: case UINT16: case HALF: case BFLOAT16: return p.asShortPointer(); case UINT64: case LONG: return p.asLongPointer(); case UTF8: case UBYTE: case BYTE: return p.asBytePointer(); case BOOL: return p.asBooleanPointer(); default: return p; } }
Example #27
Source File: DefaultOpExecutioner.java From nd4j with Apache License 2.0 | 4 votes |
@Override public void registerGraph(long id, Pointer graph) { throw new UnsupportedOperationException("Not yet implemented"); }
Example #28
Source File: GraphOperation.java From java with Apache License 2.0 | 4 votes |
private static void requireHandle(Pointer handle) { if (handle == null || handle.isNull()) { throw new IllegalStateException("close() has been called on the Graph this Operation was a part of"); } }
Example #29
Source File: TensorRawDataBufferFactory.java From java with Apache License 2.0 | 4 votes |
static DoubleDataBuffer mapTensorToDoubles(Pointer tensorMemory) { return mapNativeDoubles(tensorMemory.address(), tensorMemory.capacity(), false); }
Example #30
Source File: AtomicAllocator.java From nd4j with Apache License 2.0 | 4 votes |
/** * This method returns actual host pointer valid for current object * * @param array */ @Override public Pointer getHostPointer(INDArray array) { synchronizeHostData(array); return memoryHandler.getHostPointer(array.data()); }