@Override public int hashCode() { int result = (int) length; result = 31 * result + (referencing != null ? referencing.hashCode() : 0); result = 31 * result + (isPersist ? 1 : 0); result = 31 * result + (allocationMode != null ? allocationMode.hashCode() : 0); return result; }
protected void read(DataInputStream s) { try { ref = new WeakReference<DataBuffer>(this, Nd4j.bufferRefQueue()); referencing = Collections.synchronizedSet(new HashSet<String>()); dirty = new AtomicBoolean(false); allocationMode = AllocationMode.valueOf(s.readUTF()); length = s.readInt(); Type t = Type.valueOf(s.readUTF()); if (t == Type.DOUBLE) { if (allocationMode == AllocationMode.HEAP) { if (this.dataType() == Type.FLOAT) { // DataBuffer type // double -> float floatData = new float[length()]; } else if (this.dataType() == Type.DOUBLE) { // double -> double doubleData = new double[length()]; } else { // double -> int intData = new int[length()]; } for (int i = 0; i < length(); i++) { put(i, s.readDouble()); } } else { wrappedBuffer = ByteBuffer.allocateDirect(length() * getElementSize()); wrappedBuffer.order(ByteOrder.nativeOrder()); for (int i = 0; i < length(); i++) { put(i, s.readDouble()); } } } else { if (allocationMode == AllocationMode.HEAP) { if (this.dataType() == Type.FLOAT) { // DataBuffer type // float -> float floatData = new float[length()]; } else if (this.dataType() == Type.DOUBLE) { // float -> double doubleData = new double[length()]; } else { // float-> int intData = new int[length()]; } for (int i = 0; i < length(); i++) { put(i, s.readFloat()); } } else { wrappedBuffer = ByteBuffer.allocateDirect(length() * getElementSize()); wrappedBuffer.order(ByteOrder.nativeOrder()); for (int i = 0; i < length(); i++) { put(i, s.readFloat()); } } } } catch (Exception e) { throw new RuntimeException(e); } }
protected void write(DataOutputStream out) throws IOException { out.writeUTF(allocationMode.name()); out.writeInt(length()); out.writeUTF(dataType().name()); if (dataType() == Type.DOUBLE) { for (int i = 0; i < length(); i++) out.writeDouble(getDouble(i)); } else { for (int i = 0; i < length(); i++) out.writeFloat(getFloat(i)); } }
@Override public void write(DataOutputStream out) throws IOException { if (length() >= Integer.MAX_VALUE) throw new IllegalArgumentException( "Length of data buffer can not be >= Integer.MAX_VALUE on output"); out.writeUTF(allocationMode.name()); out.writeInt((int) length()); out.writeUTF(dataType().name()); if (dataType() == Type.DOUBLE) { for (int i = 0; i < length(); i++) out.writeDouble(getDouble(i)); } else if (dataType() == Type.INT) { for (int i = 0; i < length(); i++) out.writeInt(getInt(i)); } else { for (int i = 0; i < length(); i++) out.writeFloat(getFloat(i)); } }
@Override public void read(DataInputStream s) { try { referencing = Collections.synchronizedSet(new HashSet<String>()); dirty = new AtomicBoolean(false); allocationMode = AllocationMode.valueOf(s.readUTF()); length = s.readInt(); Type currentType = Type.valueOf(s.readUTF()); type = globalType; if (currentType == Type.DOUBLE) elementSize = 8; else if (currentType == Type.FLOAT || currentType == Type.INT) elementSize = 4; if (currentType != globalType && currentType != Type.INT) { log.warn( "Loading a data stream with type different from what is set globally. Expect precision loss"); if (globalType == Type.INT) log.warn("Int to float/double widening UNSUPPORTED!!!"); } pointerIndexerByGlobalType(currentType); if (currentType == Type.DOUBLE) { for (int i = 0; i < length(); i++) { putByGlobalType(i, s.readDouble()); } } else if (currentType == Type.FLOAT) { for (int i = 0; i < length(); i++) { putByGlobalType(i, s.readFloat()); } } else { for (int i = 0; i < length(); i++) { putByGlobalType(i, s.readInt()); } } wrappedBuffer = pointer.asByteBuffer(); } catch (Exception e) { throw new RuntimeException(e); } }