/** * * @param floatBuffer * @param order */ public BaseNDArray(DataBuffer floatBuffer, char order) { this(floatBuffer, new int[] {(int) floatBuffer.length()}, Nd4j.getStrides(new int[] {(int) floatBuffer.length()}, order), 0, order); if (floatBuffer.length() >= Integer.MAX_VALUE) throw new IllegalArgumentException("Length of buffer can not be >= Integer.MAX_VALUE"); }
/** * Remove an element of the ndarray * @param idx the index of the element to be removed * @return the ndarray * */ public INDArray removeEntry(int idx) { values = shiftLeft(values, idx + 1, 1, length()); indices = shiftLeft(indices, (int) (idx * shape.length() + shape.length()), (int) shape.length(), indices.length()); return this; }
/** * Create a compression descriptor from the given * data buffer elements * @param buffer the databuffer to create * the compression descriptor from */ public CompressionDescriptor(DataBuffer buffer) { this.originalLength = buffer.length() * buffer.getElementSize(); this.numberOfElements = buffer.length(); this.originalElementSize = buffer.getElementSize(); }
/** * Return if there is enough allocated memory space to add data of a given length in the databuffer * @param buffer a databuffer in which we want to add data * @param length the length of the data * @return a boolean if the insertion is possible * */ public boolean canInsert(DataBuffer buffer, int length) { return buffer.capacity() - buffer.length() >= length; }
@Override protected IComplexNDArray create(DataBuffer buffer) { return Nd4j.createComplex(buffer, new int[] {1, (int) buffer.length()}); }
public DataBufferLogEntry(DataBuffer buffer, String status) { this.length = buffer.length(); this.dataType = buffer.dataType() == DataBuffer.Type.DOUBLE ? "double" : "float"; this.stackTraceElements = Thread.currentThread().getStackTrace(); this.references = buffer.references(); timestamp = System.currentTimeMillis(); this.status = status; }
/** * * @param buffer */ public BaseNDArray(DataBuffer buffer) { this.data = buffer; if (buffer.length() >= Integer.MAX_VALUE) throw new IllegalArgumentException("Length of buffer can not be >= Integer.MAX_VALUE"); int[] shape = {1, (int) buffer.length()}; int[] stride = Nd4j.getStrides(shape); setShapeInformation(Nd4j.getShapeInfoProvider().createShapeInformation(shape, stride, 0, 1, Nd4j.order())); init(shape, stride); }
public BaseSparseNDArrayCOO(DataBuffer values, DataBuffer indices, int[] shape) { checkArgument(values.length() * shape.length == indices.length()); this.values = Nd4j.createBuffer(values, 0, values.length()); this.indices = indices; setShapeInformation(Nd4j.getShapeInfoProvider().createShapeInformation(shape)); init(shape); this.length = values.length(); int[] flags = new int[rank()]; long[] sparseOffsets = new long[rank()]; int[] hiddenDimension = new int[] {-1}; this.sparseInformation = Nd4j.getSparseInfoProvider().createSparseInformation(flags, sparseOffsets, hiddenDimension, rank()); }
@Override public DataBuffer dup() { DataBuffer ret = create(length); for (int i = 0; i < ret.length(); i++) ret.put(i, getDouble(i)); return ret; }
@Override public boolean equals(Object o) { // FIXME: this is BAD. it takes too long to work, and it breaks general equals contract if (o instanceof DataBuffer) { DataBuffer d = (DataBuffer) o; if (d.length() != length()) return false; for (int i = 0; i < length(); i++) { double eps = Math.abs(getDouble(i) - d.getDouble(i)); if (eps > 1e-12) return false; } } return true; }
public static void printNDArrayHeader(INDArray array) { System.out.println(array.data().dataType() + " - order=" + array.ordering() + ", offset=" + array.offset() + ", shape=" + Arrays.toString(array.shape()) + ", stride=" + Arrays.toString(array.stride()) + ", length=" + array.length() + ", data().length()=" + array.data().length()); }
private DataBuffer addAtPosition(DataBuffer buf, long dataSize, int pos, double value) { DataBuffer buffer = (buf.length() == dataSize) ? reallocate(buf) : buf; double[] tail = buffer.getDoublesAt(pos, (int) dataSize - pos); buffer.put(pos, value); for (int i = 0; i < tail.length; i++) { buffer.put(i + pos + 1, tail[i]); } return buffer; }
@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); }
@Override public boolean isView() { /* We don't really use Shape offset value anywhere And it's possible to be not a view, and have non-empty originalBuffer */ // length/data.length can be different in case of Threshold conversion return Shape.offset(javaShapeInformation) > 0 || (length() < data().length() && data.dataType() != DataBuffer.Type.INT) || data().originalDataBuffer() != null; }
@Override public DataBuffer compress(DataBuffer buffer) { CompressionDescriptor descriptor = new CompressionDescriptor(buffer, this); BytePointer ptr = new BytePointer(buffer.length() * buffer.getElementSize()); CompressedDataBuffer result = new CompressedDataBuffer(ptr, descriptor); Nd4j.getMemoryManager().memcpy(result, buffer); return result; }
public BaseSparseNDArrayCOO(DataBuffer values, DataBuffer indices, DataBuffer sparseInformation, int[] shape) { this.values = Nd4j.createBuffer(values, 0, values.length()); this.indices = indices; setShapeInformation(Nd4j.getShapeInfoProvider().createShapeInformation(shape)); init(shape); this.sparseInformation = sparseInformation; this.length = countNNZ(); }
@Override public INDArray convertToDoubles() { if (data.dataType() == DataBuffer.Type.DOUBLE) return this; val factory = Nd4j.getNDArrayFactory(); val buffer = Nd4j.createBuffer(new long[]{this.length()}, DataBuffer.Type.DOUBLE); factory.convertDataEx(convertType(data.dataType()), this.data().addressPointer(), DataBuffer.TypeEx.DOUBLE, buffer.addressPointer(), buffer.length()); return Nd4j.createArrayFromShapeBuffer(buffer, this.shapeInformation); }
@Override public INDArray convertToFloats() { if (data.dataType() == DataBuffer.Type.FLOAT) return this; val factory = Nd4j.getNDArrayFactory(); val buffer = Nd4j.createBuffer(new long[]{this.length()}, DataBuffer.Type.FLOAT); factory.convertDataEx(convertType(data.dataType()), this.data().addressPointer(), DataBuffer.TypeEx.FLOAT, buffer.addressPointer(), buffer.length()); return Nd4j.createArrayFromShapeBuffer(buffer, this.shapeInformation); }
public BaseSparseNDArrayCSR(DataBuffer data, int[] columnsPointers, int[] pointerB, int[] pointerE, int[] shape) { checkArgument(pointerB.length == pointerE.length); setShapeInformation(Nd4j.getShapeInfoProvider().createShapeInformation(shape)); init(shape); this.values = data; this.columnsPointers = Nd4j.getDataBufferFactory().createInt(data.length()); this.columnsPointers.setData(columnsPointers); this.length = columnsPointers.length; // The size of these pointers are constant int pointersSpace = rows; this.pointerB = Nd4j.getDataBufferFactory().createInt(pointersSpace); this.pointerB.setData(pointerB); this.pointerE = Nd4j.getDataBufferFactory().createInt(pointersSpace); this.pointerE.setData(pointerE); }