@Override public DataBuffer create(DataBuffer underlyingBuffer, long offset, long length) { if (underlyingBuffer.dataType() == DataBuffer.Type.DOUBLE) { return new DoubleBuffer(underlyingBuffer, length, offset); } else if (underlyingBuffer.dataType() == DataBuffer.Type.FLOAT) { return new FloatBuffer(underlyingBuffer, length, offset); } else if (underlyingBuffer.dataType() == DataBuffer.Type.INT) { return new IntBuffer(underlyingBuffer, length, offset); } else if (underlyingBuffer.dataType() == DataBuffer.Type.LONG) { return new LongBuffer(underlyingBuffer, length, offset); } return null; }
@Override public Pointer pointer() { return underlyingDataBuffer() != null && underlyingDataBuffer() != this ? underlyingDataBuffer().pointer() : pointer; }
/** * 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 public void copyAtStride(DataBuffer buf, long n, long stride, long yStride, long offset, long yOffset) { if (dataType() == Type.FLOAT) { for (int i = 0; i < n; i++) { put(offset + i * stride, buf.getFloat(yOffset + i * yStride)); } } else { for (int i = 0; i < n; i++) { put(offset + i * stride, buf.getDouble(yOffset + i * yStride)); } } }
/** * This method returns True, if this INDArray instance is attached to some Workspace. False otherwise. * * @return */ @Override public boolean isAttached() { return data.isAttached() || (data.underlyingDataBuffer() != null && data.underlyingDataBuffer().isAttached()) || (data.originalDataBuffer() != null && data.originalDataBuffer().isAttached()); }
@Override public long getGenerationId() { if(parentWorkspace != null){ return workspaceGenerationId; } else if(wrappedDataBuffer != null && wrappedDataBuffer.isAttached()){ return wrappedDataBuffer.getGenerationId(); } else if(originalBuffer != null && originalBuffer.isAttached()){ return originalBuffer.getGenerationId(); } return workspaceGenerationId; }
/** * Returns tracking point for Allocator * * PLEASE NOTE: Suitable & meaningful only for specific backends * * @return */ @Override public Long getTrackingPoint() { if (underlyingDataBuffer() != this) return underlyingDataBuffer() == null ? trackingPoint : underlyingDataBuffer().getTrackingPoint(); return trackingPoint; }
@Override public void setData(double[] data) { for (int i = 0; i < data.length; i++) { put(i, data[i]); } }
@Override public void assign(DataBuffer... buffers) { long[] offsets = new long[buffers.length]; long[] strides = new long[buffers.length]; for (int i = 0; i < strides.length; i++) strides[i] = 1; assign(offsets, strides, buffers); }
public static boolean contentEquals(long[] arr, DataBuffer other) { for (int i = 0; i < arr.length; i++) { if (other.getLong(i) != arr[i]) { return false; } } return true; }
private static int[] toIntArray(int length, DataBuffer buffer) { int[] ret = new int[length]; for (int i = 0; i < length; i++) { ret[i] = buffer.getInt(i); } return ret; }
@Override public void setData(int[] data) { for (int i = 0; i < data.length; i++) { put(i, data[i]); } }
/** * Get the size of the specified dimension. Identical to Shape.size(...), but does not perform any input validation * @param buffer The buffer to get the shape from * @param dimension The dimension to get. * @return The size of the specified dimension */ public static int sizeUnsafe(DataBuffer buffer, int dimension) { return buffer.getInt(1 + dimension); }
/** * Get the stride of the specified dimension, without any input validation * @param buffer The buffer to get the stride from * @param dimension The dimension to get. * @param rank Rank of the array * @return The stride of the specified dimension */ public static int strideUnsafe(DataBuffer buffer, int dimension, int rank) { return buffer.getInt(1 + rank + dimension); }