/** @see Pointer#position(long) */ @Override public CharPointer position(long position) { return super.position(position); } /** @see Pointer#limit(long) */
/** * Returns a ByteBuffer covering the memory space from this.position to this.limit. * If limit == 0, it uses position + 1 instead. The way the methods were designed * allows constructs such as {@code this.position(13).limit(42).asByteBuffer()}. * * @return the direct NIO {@link ByteBuffer} created */ public ByteBuffer asByteBuffer() { if (isNull()) { return null; } if (limit > 0 && limit < position) { throw new IllegalArgumentException("limit < position: (" + limit + " < " + position + ")"); } int size = sizeof(); Pointer p = new Pointer(); p.address = address; return p.position(size * position) .capacity(size * (limit <= 0 ? position + 1 : limit)) .asDirectBuffer().order(ByteOrder.nativeOrder()); } /**
/** * Calls in effect {@code memcpy(this.address + this.position, p.address + p.position, length)}, * where {@code length = sizeof(p) * (p.limit - p.position)}. * If limit == 0, it uses position + 1 instead. The way the methods were designed * allows constructs such as {@code this.position(0).put(p.position(13).limit(42))}. * * @param p the Pointer from which to copy memory * @return this */ public <P extends Pointer> P put(Pointer p) { if (p.limit > 0 && p.limit < p.position) { throw new IllegalArgumentException("limit < position: (" + p.limit + " < " + p.position + ")"); } int size = sizeof(); int psize = p.sizeof(); long length = psize * (p.limit <= 0 ? 1 : p.limit - p.position); position *= size; p.position *= psize; memcpy(this, p, length); position /= size; p.position /= psize; return (P)this; } /**
/** * Setup the given byte buffer * for serialization (note that this is for uncompressed INDArrays) * 4 bytes int for rank * 4 bytes for data opType * shape buffer * data buffer * * @param arr the array to setup * @param allocated the byte buffer to setup * @param rewind whether to rewind the byte buffer or nt */ public static void doByteBufferPutUnCompressed(INDArray arr, ByteBuffer allocated, boolean rewind) { // ensure we send data to host memory Nd4j.getExecutioner().commit(); Nd4j.getAffinityManager().ensureLocation(arr, AffinityManager.Location.HOST); ByteBuffer buffer = arr.data().pointer().asByteBuffer().order(ByteOrder.nativeOrder()); ByteBuffer shapeBuffer = arr.shapeInfoDataBuffer().pointer().asByteBuffer().order(ByteOrder.nativeOrder()); //2 four byte ints at the beginning allocated.putInt(arr.rank()); //put data opType next so its self describing allocated.putInt(arr.data().dataType().ordinal()); allocated.put(shapeBuffer); allocated.put(buffer); if (rewind) allocated.rewind(); }
final LayerWorkspaceMgr workspaceMgr) { INDArray finalMemCellState = Nd4j.createUninitialized( new long[] {/*numLayers * (bidirectional ? 2 : 1),*/ miniBatchSize, hiddenLayerSize}, 'c'); INDArray finalStepActivations = Nd4j.createUninitialized( new long[] {/*numLayers * (bidirectional ? 2 : 1),*/ miniBatchSize, hiddenLayerSize}, 'c'); toReturn.prevMemCell = prevMemCell; Nd4j.getExecutioner().commit(); int[] nbDims = new int[1]; int[] filterDimA = new int[3]; Pointer linLayerMat = new Pointer(); Pointer linLayerBias = new Pointer(); throw new RuntimeException(); checkCuda(cudaMemcpyAsync(linLayerMat, data.position(position * size * hiddenLayerSize * dataTypeSize), size * hiddenLayerSize * dataTypeSize, cudaMemcpyDeviceToDevice, stream)); if (linLayerID < 4) { checkCuda(cudaMemcpyAsync(linLayerBias, linBiasesData.position(position * hiddenLayerSize * dataTypeSize), hiddenLayerSize * dataTypeSize, cudaMemcpyDeviceToDevice, stream));
@Override public Map<String, INDArray> executeGraph(long id, @NonNull Map<String, INDArray> map, @NonNull Map<String, Integer> reverseMap) { Nd4j.getExecutioner().commit(); val ptrBuffers = new PointerPointer(map.size() * 2); val ptrShapes = new PointerPointer(map.size() * 2); val ptrIndices = new IntPointer(map.size()); val array = map.get(key); if (Nd4j.dataType() == DataBuffer.Type.FLOAT) { val result = (Nd4jCuda.FloatVariablesSet) nativeOps.executeStoredGraphFloat(null, id, ptrBuffers, ptrShapes, ptrIndices, map.size()); throw new ND4JIllegalStateException("Op execution failed: " + status); Pointer.memcpy(AtomicAllocator.getInstance().getHostPointer(array), buffer, ArrayUtil.prod(shapeOf) * Nd4j.sizeOfDataType()); AtomicAllocator.getInstance().getAllocationPoint(array).tickHostWrite(); throw new ND4JIllegalStateException("Op execution failed: " + status); Pointer.memcpy(AtomicAllocator.getInstance().getHostPointer(array), buffer, ArrayUtil.prod(shapeOf) * Nd4j.sizeOfDataType()); AtomicAllocator.getInstance().getAllocationPoint(array).tickHostWrite(); throw new ND4JIllegalStateException("Op execution failed: " + status); Pointer.memcpy(AtomicAllocator.getInstance().getHostPointer(array), buffer, ArrayUtil.prod(shapeOf) * Nd4j.sizeOfDataType()); AtomicAllocator.getInstance().getAllocationPoint(array).tickHostWrite();
val hiddenLayerSize = recurrentWeights.size(0); //i.e., n^L val prevLayerSize = inputWeights.size(0); //n^(L-1) val inputLayerSize = input.size(1); val miniBatchSize = epsilon.size(0); boolean is2dInput = epsilon.rank() < 3; //Edge case: T=1 may have shape [miniBatchSize,n^(L+1)], equiv. to [miniBatchSize,n^(L+1),1] INDArray prevStepActivations = toCOrder(fwdPass.prevAct); Nd4j.getExecutioner().commit(); xData.position(endIdx * dataTypeSize); dyData.position(endIdx * (BIDIRECTIONAL ? 2 : 1) * dataTypeSize); outputActivationsData.position(endIdx * (BIDIRECTIONAL ? 2 : 1) * dataTypeSize); timeSeriesLength = (int) Math.min(timeSeriesLength, tbpttBackwardLength); int[] nbDims = new int[1]; int[] filterDimA = new int[3]; Pointer linLayerMat = new Pointer(); Pointer linLayerBias = new Pointer(); throw new RuntimeException(); checkCuda(cudaMemcpyAsync(data.position(position * size * hiddenLayerSize * dataTypeSize), linLayerMat, size * hiddenLayerSize * dataTypeSize, cudaMemcpyDeviceToDevice, stream)); if (linLayerID < 4) { checkCuda(cudaMemcpyAsync(bGradientsOutData.position(position * hiddenLayerSize * dataTypeSize), linLayerBias, hiddenLayerSize * dataTypeSize, cudaMemcpyDeviceToDevice, stream));
Pointer shapeBufferPointer = nativeOps.shapeBufferForNumpy(pointer); int length = nativeOps.lengthForShapeBufferPointer(shapeBufferPointer); shapeBufferPointer.capacity(4 * length); shapeBufferPointer.limit(4 * length); shapeBufferPointer.position(0); DataBuffer shapeBuffer = Nd4j.createBuffer(shapeBufferPointer, DataBuffer.Type.INT,length, IntIndexer.create(intPointer)); dataPointer.position(0); dataPointer.limit(dataBufferElementSize * Shape.length(shapeBuffer)); dataPointer.capacity(dataBufferElementSize * Shape.length(shapeBuffer)); data = Nd4j.createBuffer(dataPointer, DataBuffer.Type.FLOAT, Shape.length(shapeBuffer), data = Nd4j.createBuffer(dataPointer, DataBuffer.Type.DOUBLE, Shape.length(shapeBuffer),
CompressedDataBuffer compressedDataBuffer = (CompressedDataBuffer) arr.data(); CompressionDescriptor descriptor = compressedDataBuffer.getCompressionDescriptor(); ByteBuffer codecByteBuffer = descriptor.toByteBuffer(); ByteBuffer buffer = arr.data().pointer().asByteBuffer().order(ByteOrder.nativeOrder()); ByteBuffer shapeBuffer = arr.shapeInfoDataBuffer().pointer().asByteBuffer().order(ByteOrder.nativeOrder()); allocated.putInt(arr.rank()); allocated.putInt(arr.data().dataType().ordinal());
Pointer pointer = TF_TensorData(tensor).capacity(length); Indexer indexer = indexerForType(nd4jType,pointer); DataBuffer d = Nd4j.createBuffer(indexer.pointer(),nd4jType,length,indexer); INDArray array = Nd4j.create(d,ndShape); Nd4j.getAffinityManager().tagLocation(array, AffinityManager.Location.HOST); return array;
for (int i = 0; i < samples.length; i++) { ByteBuffer b = (ByteBuffer)samples[i]; if (samples_in[i] instanceof BytePointer && samples_in[i].capacity() >= inputSize && b.hasArray()) { ((BytePointer)samples_in[i]).position(0).put(b.array(), b.position(), inputSize); } else { for (int i = 0; i < samples.length; i++) { ShortBuffer b = (ShortBuffer)samples[i]; if (samples_in[i] instanceof ShortPointer && samples_in[i].capacity() >= inputSize && b.hasArray()) { ((ShortPointer)samples_in[i]).position(0).put(b.array(), samples[i].position(), inputSize); } else { for (int i = 0; i < samples.length; i++) { IntBuffer b = (IntBuffer)samples[i]; if (samples_in[i] instanceof IntPointer && samples_in[i].capacity() >= inputSize && b.hasArray()) { ((IntPointer)samples_in[i]).position(0).put(b.array(), samples[i].position(), inputSize); } else { for (int i = 0; i < samples.length; i++) { FloatBuffer b = (FloatBuffer)samples[i]; if (samples_in[i] instanceof FloatPointer && samples_in[i].capacity() >= inputSize && b.hasArray()) { ((FloatPointer)samples_in[i]).position(0).put(b.array(), b.position(), inputSize); } else { for (int i = 0; i < samples.length; i++) { DoubleBuffer b = (DoubleBuffer)samples[i]; if (samples_in[i] instanceof DoublePointer && samples_in[i].capacity() >= inputSize && b.hasArray()) { ((DoublePointer)samples_in[i]).position(0).put(b.array(), b.position(), inputSize); } else {
if (!arr.isCompressed()) { ByteBuffer buffer = arr.data().pointer().asByteBuffer().order(ByteOrder.nativeOrder()); ByteBuffer shapeBuffer = arr.shapeInfoDataBuffer().pointer().asByteBuffer().order(ByteOrder.nativeOrder()); CompressionDescriptor descriptor = compressedDataBuffer.getCompressionDescriptor(); ByteBuffer codecByteBuffer = descriptor.toByteBuffer(); ByteBuffer buffer = arr.data().pointer().asByteBuffer().order(ByteOrder.nativeOrder()); ByteBuffer shapeBuffer = arr.shapeInfoDataBuffer().pointer().asByteBuffer().order(ByteOrder.nativeOrder()); int twoInts = 2 * 4; return twoInts + buffer.limit() + shapeBuffer.limit() + codecByteBuffer.limit();
@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); }
BytePointer data = image[0] instanceof ByteBuffer ? new BytePointer((ByteBuffer)image[0].position(0)) : new BytePointer(new Pointer(image[0].position(0))); av_image_fill_arrays(new PointerPointer(image_frame), image_frame.linesize(), data, pixelFormat, width, height, 1); image_frame.linesize(0, step); image_frame.format(pixelFormat);
/** @see Pointer#capacity(long) */ @Override public PointerPointer<P> capacity(long capacity) { return super.capacity(capacity); }
/** @see Pointer#limit(long) */ @Override public BoolPointer limit(long limit) { return super.limit(limit); } /** @see Pointer#capacity(long) */
@Override public void write(DataOutputStream out) throws IOException { // logger.info("Writing out CompressedDataBuffer"); // here we should mimic to usual DataBuffer array out.writeUTF(allocationMode.name()); out.writeInt((int) compressionDescriptor.getCompressedLength()); out.writeUTF(Type.COMPRESSED.name()); // at this moment we don't care about mimics anymore //ByteRawIndexer indexer = new ByteRawIndexer((BytePointer) pointer); out.writeUTF(compressionDescriptor.getCompressionAlgorithm()); out.writeLong(compressionDescriptor.getCompressedLength()); out.writeLong(compressionDescriptor.getOriginalLength()); out.writeLong(compressionDescriptor.getNumberOfElements()); // out.write(((BytePointer) pointer).getStringBytes()); for (int x = 0; x < pointer.capacity() * pointer.sizeof(); x++) { byte b = pointer.asByteBuffer().get(x); out.writeByte(b); } }
static boolean isEqual(Frame frame, PIX pix) { return pix != null && frame != null && frame.image != null && frame.image.length > 0 && frame.imageWidth == pix.w() && frame.imageHeight == pix.h() && frame.imageChannels == pix.d() / 8 && frame.imageDepth == Frame.DEPTH_UBYTE && (ByteOrder.nativeOrder().equals(ByteOrder.LITTLE_ENDIAN) || new Pointer(frame.image[0]).address() == pix.data().address()) && frame.imageStride * Math.abs(frame.imageDepth) / 8 == pix.wpl() * 4; }